Skip to content

Instantly share code, notes, and snippets.

@takeru
Last active January 17, 2016 14:56
Show Gist options
  • Save takeru/5315814 to your computer and use it in GitHub Desktop.
Save takeru/5315814 to your computer and use it in GitHub Desktop.
Faraday async
require "rubygems"
require "bundler/setup"
require 'faraday'
require 'faraday_middleware'
require 'typhoeus/adapters/faraday'
require 'pp'
def reset
$start_time = Time.now
end
def log(s)
ms = (Time.now-$start_time)*1000
puts "%6dms: %s" % [ms, s]
end
reset
log "start: A"
conn = Faraday::Connection.new(:url => 'http://b.hatena.ne.jp') do |c|
c.adapter :net_http
end
resp = conn.get do |req|
req.url "/miyagawa/rss"
end
log resp.body.class
log "end: A"
puts
reset
log "start: B"
conn = Faraday::Connection.new(:url => 'http://b.hatena.ne.jp') do |c|
c.response :xml, :content_type => /\bxml$/
c.adapter :em_synchrony
end
resp = conn.get do |req|
req.url "/miyagawa/rss"
end
log resp.body.class
log "end: B"
puts
reset
log "start: C"
conn = Faraday::Connection.new(:url => 'http://b.hatena.ne.jp') do |c|
c.response :mashify
c.response :xml, :content_type => /\bxml$/
c.adapter :em_synchrony
end
resp = conn.get do |req|
req.url "/miyagawa/rss"
end
log resp.body.class
log resp.body['RDF'].channel.title
#resp.body['RDF'].item.each do |item|
# puts item.link, item.title
#end
log "end: C"
puts
# https://github.com/lostisland/faraday/wiki/Parallel-requests
reset
log "start: D"
conn = Faraday::Connection.new(:url => 'http://b.hatena.ne.jp') do |c|
c.response :mashify
c.response :xml, :content_type => /\bxml$/
c.adapter :em_synchrony
end
resp1 = nil
resp2 = nil
conn.in_parallel do
resp1 = conn.get do |req|
req.url "/naoya/rss"
end
resp2 = conn.get do |req|
req.url "/miyagawa/rss"
end
log "in resp1.class : #{resp1.body.class}"
log "in resp2.class : #{resp2.body.class}"
end
log "out resp1.class : #{resp1.body.class} #{resp1.body['RDF'].channel.title}"
log "out resp2.class : #{resp2.body.class} #{resp2.body['RDF'].channel.title}"
log "end: D"
puts
reset
log "start: E"
conn = Faraday::Connection.new(:url => 'http://b.hatena.ne.jp') do |c|
c.response :mashify
c.response :xml, :content_type => /\bxml$/
c.adapter :net_http
end
resps = []
#conn.in_parallel do
%w(naoya miyagawa mala lestrrat kakutani takahashim otsune coji).each_with_index do |name,i|
r = conn.get do |req|
req.url "/#{name}/rss"
end
log "in #{i} #{r.body.class}"
resps << r
end
#end
log "out"
resps.each_with_index do |r,i|
log "out #{i} #{r.body.class} #{r.body['RDF'].channel.title}"
end
log "end: E"
puts
reset
log "start: F"
conn = Faraday::Connection.new(:url => 'http://b.hatena.ne.jp') do |c|
c.response :mashify
c.response :xml, :content_type => /\bxml$/
c.adapter :typhoeus
end
resps = []
conn.in_parallel do
%w(naoya miyagawa mala lestrrat kakutani takahashim otsune coji).each_with_index do |name,i|
r = conn.get do |req|
req.url "/#{name}/rss"
end
log "in #{i} #{r.body.class}"
resps << r
end
end
log "out"
resps.each_with_index do |r,i|
log "out #{i} #{r.body.class} #{r.body['RDF'].channel.title}"
end
log "end: F"
puts
# A sample Gemfile
source "https://rubygems.org"
# gem "rails"
gem 'faraday'
gem 'faraday_middleware'
gem 'em-synchrony'
gem 'em-http-request'
gem 'multi_xml'
gem 'hashie'
gem 'typhoeus'
GEM
remote: https://rubygems.org/
specs:
addressable (2.3.3)
cookiejar (0.3.0)
em-http-request (1.0.3)
addressable (>= 2.2.3)
cookiejar
em-socksify
eventmachine (>= 1.0.0.beta.4)
http_parser.rb (>= 0.5.3)
em-socksify (0.2.1)
eventmachine (>= 1.0.0.beta.4)
em-synchrony (1.0.3)
eventmachine (>= 1.0.0.beta.1)
ethon (0.5.10)
ffi (~> 1.3.0)
mime-types (~> 1.18)
eventmachine (1.0.3)
faraday (0.8.7)
multipart-post (~> 1.1)
faraday_middleware (0.9.0)
faraday (>= 0.7.4, < 0.9)
ffi (1.3.1)
hashie (2.0.3)
http_parser.rb (0.5.3)
mime-types (1.22)
multi_xml (0.5.3)
multipart-post (1.2.0)
typhoeus (0.6.2)
ethon (~> 0.5.10)
PLATFORMS
ruby
DEPENDENCIES
em-http-request
em-synchrony
faraday
faraday_middleware
hashie
multi_xml
typhoeus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment