agile web posting with ruby (lang:ja)
DESCRIPTION
第26回 Ruby/Rails勉強会@関西で発表したものTRANSCRIPT
![Page 1: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/1.jpg)
Rubyで快適に連投する11の方法
Agile Web Posting with Ruby 2008 UJIHISA, Tatsuhiro
1
![Page 2: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/2.jpg)
2008 UJIHISA, Tatsuhiro
2
![Page 3: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/3.jpg)
Agenda• 背景と動機 (bg and motivation)
• 基礎 (basis)
• 手法 (methods)
• やりかた (howto)
• デモ (demonstration)
• まとめ (conclusion)3
![Page 4: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/4.jpg)
連投
4
![Page 5: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/5.jpg)
連投
4
![Page 6: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/6.jpg)
連投
4
![Page 7: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/7.jpg)
連投
4
![Page 8: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/8.jpg)
連投
4
![Page 9: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/9.jpg)
連投
4
![Page 10: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/10.jpg)
連投
4
![Page 11: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/11.jpg)
Agile Web Posting
• 書いたばかりの文書をなんらかのWebサービスにPostする行為を最適化する。outputの最適化
• c.f. ネットコミュニケーション2.0 at デブサミ
inputの最適化
5
![Page 12: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/12.jpg)
前提• Rubyはそれなりに使えるけど、他はあまり… というふつうの人が今回の話の対象
• まじめに対応するより、とにかく当面の問題が解決されればそれでよい
6
![Page 13: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/13.jpg)
あなたが普段使っているWebサービスは?
7
![Page 14: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/14.jpg)
あなたが普段使っているWebサービスは?
7
![Page 15: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/15.jpg)
あなたが普段使っているWebサービスは?
7
![Page 16: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/16.jpg)
あなたが普段使っているWebサービスは?
7
![Page 17: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/17.jpg)
どのように楽をしている?• ログインはブラウザがAutofill
• bookmarkletで
• greasemonkeyで
• add-onで
• 専用クライアントで
8
![Page 18: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/18.jpg)
Webサービスを考える• 入力、加工、出力• (入力, 出力): インタフェース
• 汎用的で便利な出力: RSS
• 無理矢理RSSに:
• Yahoo! Pipes, p[lr]agger,PatchService
9
![Page 19: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/19.jpg)
数秒の手間を惜しむ• ...ブラウザのタブを開いて、ブックマークからサイトを開いて、ログインして、投稿ページに行って、本文を書いて、確認画面を経由して、投稿する。投稿後、URIをコピーする
• “本文を書いて投稿する。” でOK
• 開発時間と使用時間の和を最小化10
![Page 20: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/20.jpg)
例えば、ブラウザを避ける
11
![Page 21: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/21.jpg)
Web
Rubyでラクをする
ユーザ入力
ユーザ出力Ruby script
12
![Page 22: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/22.jpg)
RubyのWeb関係ライブラリ• net/http
• open-uri
• Hpricot (HTML操作)
• WWW::Mechanize
13
![Page 23: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/23.jpg)
WWW::Mechanize
RubyでWebに対して入出力
14
![Page 24: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/24.jpg)
agent = WWW::Mechanize.newpage = agent.get 'http://mixi.jp/'form = page.forms[0]form.email = 'YOUR_EMAIL'form.password = 'YOUR_PASSWORD'agent.submit formpage = agent.get "http://mixi.jp/home.pl"p page.links.map {¦i¦ i.uri }
$ sudo gem install mechanize
15
![Page 25: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/25.jpg)
Webユーザ入力
ユーザ出力Ruby script
インタフェースは?
16
![Page 26: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/26.jpg)
$ cat my_diary.txt ¦ ruby mixi.rb[mixi] 投稿テストhttp://mixi.jp/view_diary.pl?id=*********&owner_id=*******$ echo "投稿テスト2\nこれでいけるかな" ¦ ruby mixi.rb[mixi] 投稿テスト2http://mixi.jp/view_diary.pl?id=*********&owner_id=*******
インタフェースは?
17
![Page 27: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/27.jpg)
考えられる方法• スクリプトに引数で渡した文字列で
POST
• スクリプトに標準入力で渡した文字列でPOST
• スクリプト起動するとEDITORが立ち上がって、保存して終了するとPOST
(svn ci方式)18
![Page 28: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/28.jpg)
それVimで
• メモ、コード、etc
• その画面からそのままPOSTしたい
• →vimのコマンドを追加する
「普段の作業のほとんどはvimの中」(vimユーザ)
19
![Page 29: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/29.jpg)
vimscript
20
![Page 30: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/30.jpg)
..." Setup the autocommands that handle buffer MRU ordering.augroup LustyExplorer autocmd! autocmd BufEnter * ruby Window.buffer_stack.push autocmd BufDelete * ruby Window.buffer_stack.pop autocmd BufWipeout * ruby Window.buffer_stack.popaugroup End
ruby << EOFrequire 'pathname'
class String def ends_with?(s) tail = self[-s.length, s.length] tail == s end
def starts_with?(s) head = self[0, s.length] head == s endend...
lusty-explorer.vim
21
![Page 31: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/31.jpg)
..." Setup the autocommands that handle buffer MRU ordering.augroup LustyExplorer autocmd! autocmd BufEnter * ruby Window.buffer_stack.push autocmd BufDelete * ruby Window.buffer_stack.pop autocmd BufWipeout * ruby Window.buffer_stack.popaugroup End
ruby << EOFrequire 'pathname'
class String def ends_with?(s) tail = self[-s.length, s.length] tail == s end
def starts_with?(s) head = self[0, s.length] head == s endend...
lusty-explorer.vim
ruby...?
21
![Page 32: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/32.jpg)
vimのruby拡張• vimscriptの中に文字列でrubyのコードを書く
• vimscript側でvimのコマンドとruby
のメソッドを対応づける
• ruby側でvimscriptをevalする
22
![Page 33: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/33.jpg)
$ vim --versionVIM - Vi IMproved 7.1 (2007 May 12, compiled May 8 2008 10:49:50)MacOS X (unix) versionIncluded patches: 1-293Compiled by [email protected] version without GUI. Features included (+) or not (-):+arabic +autocmd -balloon_eval -browse ++builtin_terms +byte_offset +cindent -clientserver -clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments +cryptv +cscope +cursorshape +dialog_con +diff +digraphs -dnd -ebcdic +emacs_tags +eval +ex_extra +extra_search +farsi +file_in_path +find_in_path +folding -footer +fork() -gettext -hangul_input +iconv +insert_expand +jumplist +keymap +langmap +libcall +linebreak +lispindent +listcmds +localmap +menu +mksession +modify_fname +mouse -mouseshape +mouse_dec -mouse_gpm -mouse_jsbterm +mouse_netterm +mouse_xterm +multi_byte +multi_lang -mzscheme -netbeans_intg -osfiletype +path_extra -perl +postscript +printer +profile +python +quickfix +reltime +rightleft +ruby +scrollbind +signs +smartindent ...
23
![Page 34: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/34.jpg)
command Mixi :call <SID>MixiStart()
function! s:MixiStart() ruby mixi_runendfunction
ruby << EOFdef mixi_run 処理endEOF
24
![Page 35: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/35.jpg)
def mixi_run return if VIM.evaluate('confirm("really?")') == 0
endline = VIM.evaluate %[line("$")] title = VIM.evaluate %[getline(1)] body = VIM.evaluate %[join(getline(2, #{endline}), "\n")]
m = Mixi.new 'YOUR_EMAIL', 'YOUR_PASSWORD' # if you are mixi premium member: # m = Mixi.new 'YOUR_EMAIL', 'YOUR_PASSWORD', true m.post title.toeuc, body.toeuc m.get_latest.each do ¦line¦ VIM.evaluate %[append(#{endline}, \"#{line.chomp}\")] endend
25
![Page 36: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/36.jpg)
使い方• ~/.vim/plugin/以下に配置
• あとはvimを起動して:Mixiなど
• コマンド実行されるまで処理させないことが大事require ‘mechanize’などは重い
26
![Page 37: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/37.jpg)
これだけ覚える• おまじない (コピペでOK)
• VIM.evaluate
• CodeReposのlang/vim以下を見ればたくさんのサンプルが。http://coderepos.org/share/browser/lang/vim/
27
![Page 38: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/38.jpg)
公開しているvimscript例
28
![Page 39: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/39.jpg)
mixi.vim/nowa.vim
• ファイルに保存する気分でエントリをPOSTしてしまう
• :Mixi, :Nowa一行目がsubject, 二行目以降が本文
• http://coderepos.org/share/browser/lang/vim/misc/{mixi|nowa}.vim
• 要認証用パスワード29
![Page 40: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/40.jpg)
絶賛の声
30
![Page 41: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/41.jpg)
絶賛の声
30
![Page 42: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/42.jpg)
絶賛の声
30
![Page 43: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/43.jpg)
絶賛の声
30
![Page 44: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/44.jpg)
絶賛の声
30
![Page 45: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/45.jpg)
cotocoto.vim
• 勉強会開催を連投できる• :Cotocotoでbufferにテンプレ
:CotocotoPostで投稿
• 構文解析はyamlに丸投げ
• 設定不要
31
![Page 46: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/46.jpg)
実演
32
![Page 47: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/47.jpg)
インタフェース再考• vim/emacsにこだわる必要はない
• 最適な入力環境(編集, 入力支援, 参照)
という意味ではvim/emacsは良い
• “既に起動しているもの”
33
![Page 48: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/48.jpg)
• rubyで書いて外部コマンド化することで汎用性を高める
• vimscriptとelispそれぞれで書くよりvimscriptとelispがそれぞれrubyを呼べばよい
• mechanize強力
34
![Page 49: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/49.jpg)
vimscriptを公開しよう!• CodeRepos
lang/vim以下
• パスワードが漏れないよう気をつけてね!!
35
![Page 50: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/50.jpg)
vimscriptupdater#!/usr/bin/env ruby
class Vimscriptupdater @@from = '/Users/ujihisa/Documents/uvn/repos_vim/misc/' @@to = '/Users/ujihisa/.vim/plugin/'
def self.mixi_vim(email, password) file = File.open("#{@@from}mixi.vim") {¦io¦ io.read } File.open("#{@@to}mixi.vim", 'w') {¦io¦ io.write file.gsub('YOUR_EMAIL', email).gsub('YOUR_PASSWORD', password) } end
def self.nowa_vim(email, password) file = File.open("#{@@from}nowa.vim") {¦io¦ io.read } File.open("#{@@to}nowa.vim", 'w') {¦io¦ io.write file.gsub('YOUR_NOWA_ID', email).gsub('YOUR_PASSWORD', password) } end end
if __FILE__ == $0 Vimscriptupdater.mixi_vim '[email protected]', '*******' Vimscriptupdater.nowa_vim 'ujihisa', '*******'end
36
![Page 51: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/51.jpg)
エディタの拡張• 大抵のエディタはなんらかの拡張が可能なはず
• 外部コマンドを実行できるならもはやなんでもあり
• vimのためにrubyで書いたものが再利用できる!
37
![Page 52: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/52.jpg)
適材適所• twitterもvimで閲覧&投稿!
• timelineを取得→アイコンが見れない
• 使い物にならない
38
![Page 53: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/53.jpg)
おわり39
![Page 54: Agile Web Posting with Ruby (lang:ja)](https://reader033.vdocument.in/reader033/viewer/2022060106/55493fdfb4c9050a4d8b4ff3/html5/thumbnails/54.jpg)
演習• 11種類のWebサービスの利用を快適にするスクリプトを書く。
• 簡単なチュートリアルを添えて、それを公開する。
• 公開先の例: CodeReposhttp://coderepos.org
40