Skip to content

Instantly share code, notes, and snippets.

@satoryu
Created October 30, 2012 18:25
Show Gist options
  • Save satoryu/3982043 to your computer and use it in GitHub Desktop.
Save satoryu/3982043 to your computer and use it in GitHub Desktop.
class ItemsController < ApplicationController
RWS_URL = 'https://app.rakuten.co.jp'
def index
items = http_get({'keyword' => params['keyword']})
render :json => items
end
private
def http_get(query)
response = nil
query = query.merge({'applicationId' => ENV['APPLICATION_ID'],
'format' => 'json'})
http = EM::HttpRequest.new(RWSC_URL).get(
:path => '/services/api/IchibaItem/Search/20120723',
:query => query)
http.callback { response = http.response }
http.errback { logger.error http.error }
return response
end
end
$ bundle exec rails s
=> Booting Thin
=> Rails 3.2.8 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
>> Thin web server (v1.4.1 codename Chromeo)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
Started GET "/items?keyword=abc" for 127.0.0.1 at 2012-10-31 03:21:25 +0900
Processing by ItemsController#index as HTML
Parameters: {"keyword"=>"abc"}
Completed 200 OK in 23ms (Views: 0.2ms)
require 'fiber'
class ItemsController < ApplicationController
RWS_URL = 'https://app.rakuten.co.jp'
def index
items = http_get({'keyword' => params['keyword']})
render :json => items
end
private
def http_get(query)
f = Fiber.current
query = query.merge({'applicationId' => ENV['APPLICATION_ID'],
'format' => 'json'})
http = EM::HttpRequest.new(RWSC_URL).get(
:path => '/services/api/IchibaItem/Search/20120723',
:query => query)
http.callback { f.resume http.response }
http.errback { f.resume nil }
return Fiber.yield
end
end
require 'eventmachine'
require 'em-http-request'
RWS_URL = 'https://app.rakuten.co.jp'
def http_get(query)
response = 'init'
query = query.merge('applicationId' => ENV['APPLICATION_ID'],
'format' => 'json')
http = EM::HttpRequest.new(RWS_URL).get(
:path => '/services/api/IchibaItem/Search/20120723',
:query => query)
http.callback { response = http.response }
return response
end
keyword = ARGV.shift
EM.run do
puts http_get('keyword' => keyword)
EM.stop
end
require 'eventmachine'
require 'em-http-request'
RWS_URL = 'https://app.rakuten.co.jp'
def http_get(query, &block)
response = 'init'
query = query.merge('applicationId' => ENV['APPLICATION_ID'],
'format' => 'json')
http = EM::HttpRequest.new(RWS_URL).get(
:path => '/services/api/IchibaItem/Search/20120723',
:query => query)
call_back = proc do
case block.arity
when 0 then block.call
when 1 then block.call(http.response)
else block.call(http.response, http.error)
end
end
http.callback(&call_back)
http.errback(&call_back)
end
keyword = ARGV.shift
EM.run do
http_get('keyword' => keyword) do |response|
puts response
EM.stop
end
end
require 'eventmachine'
require 'em-http-request'
require 'fiber'
RWS_URL = 'https://app.rakuten.co.jp'
def http_get(query, &block)
response = 'init'
query = query.merge('applicationId' => ENV['APPLICATION_ID'],
'format' => 'json')
f = Fiber.current
http = EM::HttpRequest.new(RWS_URL).get(
:path => '/services/api/IchibaItem/Search/20120723',
:query => query)
call_back = proc do
f.resume http.response # <= (3)
end
http.callback(&call_back)
http.errback(&call_back)
return Fiber.yield # <= (2)
end
keyword = ARGV.shift
EM.run do
Fiber.new do
response = http_get('keyword' => keyword)
puts response
EM.stop
end.resume # <= (1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment