⇒ composer globalrequire"laravel/installer" Changed current directory to /Users/PMAC025S/.composer
Using version ^1.3for laravel/installer ./composer.json has been created Loading composer repositories with package information Updating dependencies (including require-dev) - Installing symfony/process (v3.0.4) Downloading: 100%
- Installing symfony/polyfill-mbstring (v1.1.1) Loading from cache
Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands: help Displays help foracommand list Lists commands new Create anew Laravel application.
$ ruby server.rb == Sinatra (v1.4.7) has taken the stage on 4567 for development with backup from Thin Thin web server (v1.6.4 codename Gob Bluth) Maximum connections set to 1024 Listening on 0.0.0.0:4567, CTRL+C to stop
APIを実行
1 2 3
$ curl http://localhost:4567 -X POST -d "url=http://www.yahoo.co.jp/" => {"url":"http://www.yahoo.co.jp/","title":"Yahoo! JAPAN","description":"日本最大級のポータルサイト。検索、オークション、ニュース、メール、コミュニティ、ショッピング、など80以上のサービスを展開。あなたの生活をより豊かにする「ライフ・エンジン」を目指していきます。","robots":"noodp","google-site-verification":"fsLMOiigp5fIpCDMEVodQnQC7jIY1K3UXW5QkQcBmVs"}
その他
内部的にはNokogiriを使ってhtmlをパースしています。
文字化けして内容がみれない場合があったので、以下のワークアラウンドをいれました。
1 2 3
html = URI.parse(url).read node = Nokogiri::HTML(html.toutf8, nil, 'utf-8')
Status Codeを設定するstatusヘルパー、headerを設定するheadersヘルパーも用意されています。
1 2 3 4 5 6 7 8
get'/foo'do status 418 headers \ "Allow" => "BREW, POST, GET, PROPFIND, WHEN", "Refresh" => "Refresh: 20; http://www.ietf.org/rfc/rfc2324.txt" body "I'm a tea pot!" end
get '/subscribe'do # register a client's interest in server events stream(:keep_open) do |out| connections << out # purge dead connections connections.reject!(&:closed?) end end
post '/message'do connections.each do |out| # notify client that a new message has arrived out << params['message'] << "\n"