faraday 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -0
- data/Gemfile.lock +11 -2
- data/README.md +10 -8
- data/faraday.gemspec +3 -2
- data/lib/faraday.rb +1 -1
- data/lib/faraday/adapter.rb +9 -3
- data/lib/faraday/adapter/em_synchrony.rb +78 -0
- data/test/adapters/live_test.rb +15 -0
- data/test/form_post_test.rb +16 -0
- metadata +4 -11
data/Gemfile
CHANGED
@@ -7,6 +7,9 @@ group :development, :test do
|
|
7
7
|
gem 'patron', '~> 0.4'
|
8
8
|
gem 'sinatra', '~> 1.1'
|
9
9
|
gem 'typhoeus', '~> 0.1'
|
10
|
+
gem 'eventmachine', '~> 0.12'
|
11
|
+
gem 'em-http-request', '~> 0.2', :require => 'em-http'
|
12
|
+
gem 'em-synchrony', '~> 0.2', :require => ['em-synchrony', 'em-synchrony/em-http']
|
10
13
|
end
|
11
14
|
|
12
15
|
gemspec
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
faraday (0.5.
|
4
|
+
faraday (0.5.3)
|
5
5
|
addressable (~> 2.2.2)
|
6
6
|
multipart-post (~> 1.0.1)
|
7
7
|
rack (>= 1.1.0, < 2)
|
@@ -10,8 +10,14 @@ GEM
|
|
10
10
|
remote: http://rubygems.org/
|
11
11
|
specs:
|
12
12
|
addressable (2.2.2)
|
13
|
+
em-http-request (0.2.14)
|
14
|
+
addressable (>= 2.0.0)
|
15
|
+
eventmachine (>= 0.12.9)
|
16
|
+
em-synchrony (0.2.0)
|
17
|
+
eventmachine (>= 0.12.9)
|
18
|
+
eventmachine (0.12.10)
|
13
19
|
multipart-post (1.0.1)
|
14
|
-
patron (0.4.
|
20
|
+
patron (0.4.10)
|
15
21
|
rack (1.2.1)
|
16
22
|
rake (0.8.7)
|
17
23
|
sinatra (1.1.0)
|
@@ -26,6 +32,9 @@ PLATFORMS
|
|
26
32
|
|
27
33
|
DEPENDENCIES
|
28
34
|
addressable (~> 2.2.2)
|
35
|
+
em-http-request (~> 0.2)
|
36
|
+
em-synchrony (~> 0.2)
|
37
|
+
eventmachine (~> 0.12)
|
29
38
|
faraday!
|
30
39
|
multipart-post (~> 1.0.1)
|
31
40
|
patron (~> 0.4)
|
data/README.md
CHANGED
@@ -7,16 +7,18 @@ This mess is gonna get raw, like sushi. So, haters to the left.
|
|
7
7
|
## Usage
|
8
8
|
|
9
9
|
conn = Faraday::Connection.new(:url => 'http://sushi.com') do |builder|
|
10
|
-
builder.use Faraday::Request::Yajl
|
11
|
-
builder.use Faraday::Adapter::Logger
|
12
|
-
builder.use Faraday::Adapter::Typhoeus
|
13
|
-
builder.use Faraday::
|
10
|
+
builder.use Faraday::Request::Yajl # convert body to json with Yajl lib
|
11
|
+
builder.use Faraday::Adapter::Logger # log the request somewhere?
|
12
|
+
builder.use Faraday::Adapter::Typhoeus # make http request with typhoeus
|
13
|
+
builder.use Faraday::Adapter::EMSynchrony # make http request with eventmachine and synchrony
|
14
|
+
builder.use Faraday::Response::Yajl # parse body with yajl
|
14
15
|
|
15
16
|
# or use shortcuts
|
16
|
-
builder.request :yajl
|
17
|
-
builder.adapter :logger
|
18
|
-
builder.adapter :typhoeus
|
19
|
-
builder.
|
17
|
+
builder.request :yajl # Faraday::Request::Yajl
|
18
|
+
builder.adapter :logger # Faraday::Adapter::Logger
|
19
|
+
builder.adapter :typhoeus # Faraday::Adapter::Typhoeus
|
20
|
+
builder.adapter :em_synchrony # Faraday::Adapter::EMSynchrony
|
21
|
+
builder.response :yajl # Faraday::Response::Yajl
|
20
22
|
end
|
21
23
|
|
22
24
|
resp1 = conn.get '/nigiri/sake.json'
|
data/faraday.gemspec
CHANGED
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
|
|
12
12
|
## If your rubyforge_project name is different, then edit it and comment out
|
13
13
|
## the sub! line in the Rakefile
|
14
14
|
s.name = 'faraday'
|
15
|
-
s.version = '0.5.
|
16
|
-
s.date = '2010-
|
15
|
+
s.version = '0.5.3'
|
16
|
+
s.date = '2010-11-09'
|
17
17
|
s.rubyforge_project = 'faraday'
|
18
18
|
|
19
19
|
## Make sure your summary is short. The description may be as long
|
@@ -51,6 +51,7 @@ Gem::Specification.new do |s|
|
|
51
51
|
lib/faraday.rb
|
52
52
|
lib/faraday/adapter.rb
|
53
53
|
lib/faraday/adapter/action_dispatch.rb
|
54
|
+
lib/faraday/adapter/em_synchrony.rb
|
54
55
|
lib/faraday/adapter/net_http.rb
|
55
56
|
lib/faraday/adapter/patron.rb
|
56
57
|
lib/faraday/adapter/test.rb
|
data/lib/faraday.rb
CHANGED
data/lib/faraday/adapter.rb
CHANGED
@@ -10,6 +10,7 @@ module Faraday
|
|
10
10
|
:ActionDispatch => 'action_dispatch',
|
11
11
|
:NetHttp => 'net_http',
|
12
12
|
:Typhoeus => 'typhoeus',
|
13
|
+
:EMSynchrony => 'em_synchrony',
|
13
14
|
:Patron => 'patron',
|
14
15
|
:Test => 'test'
|
15
16
|
|
@@ -19,7 +20,7 @@ module Faraday
|
|
19
20
|
:net_http => :NetHttp,
|
20
21
|
:typhoeus => :Typhoeus,
|
21
22
|
:patron => :Patron,
|
22
|
-
:
|
23
|
+
:em_synchrnoy => :EMSynchrony
|
23
24
|
|
24
25
|
def call(env)
|
25
26
|
process_body_for_request(env)
|
@@ -77,9 +78,14 @@ module Faraday
|
|
77
78
|
end
|
78
79
|
|
79
80
|
def process_to_params(pieces, params, base = nil, &block)
|
80
|
-
params.each do |key, value|
|
81
|
+
params.to_a.each do |key, value|
|
81
82
|
key_str = base ? "#{base}[#{key}]" : key
|
82
|
-
|
83
|
+
|
84
|
+
case value
|
85
|
+
when Array
|
86
|
+
values = value.inject([]) { |a,v| a << [nil, v] }
|
87
|
+
process_to_params(pieces, values, key_str, &block)
|
88
|
+
when Hash
|
83
89
|
process_to_params(pieces, value, key_str, &block)
|
84
90
|
else
|
85
91
|
pieces << block.call(key_str, value)
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'em-synchrony/em-http'
|
2
|
+
require 'fiber'
|
3
|
+
|
4
|
+
module Faraday
|
5
|
+
class Adapter
|
6
|
+
class EMSynchrony < Faraday::Adapter
|
7
|
+
|
8
|
+
class Header
|
9
|
+
include Net::HTTPHeader
|
10
|
+
def initialize response
|
11
|
+
@header = {}
|
12
|
+
response.response_header.each do |key, value|
|
13
|
+
case key
|
14
|
+
when "CONTENT_TYPE"; self.content_type = value
|
15
|
+
when "CONTENT_LENGTH"; self.content_length = value
|
16
|
+
else; self[key] = value
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def call(env)
|
23
|
+
process_body_for_request(env)
|
24
|
+
|
25
|
+
request = EventMachine::HttpRequest.new(URI::parse(env[:url].to_s))
|
26
|
+
|
27
|
+
options = {:head => env[:request_headers]}
|
28
|
+
|
29
|
+
if env[:body]
|
30
|
+
if env[:body].respond_to? :read
|
31
|
+
options[:body] = env[:body].read
|
32
|
+
else
|
33
|
+
options[:body] = env[:body]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
if req = env[:request]
|
38
|
+
if proxy = req[:proxy]
|
39
|
+
uri = Addressable::URI.parse(proxy[:uri])
|
40
|
+
options[:proxy] = {
|
41
|
+
:host => uri.host,
|
42
|
+
:port => uri.port
|
43
|
+
}
|
44
|
+
if proxy[:username] && proxy[:password]
|
45
|
+
options[:proxy][:authorization] = [proxy[:username], proxy[:password]]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# only one timeout currently supported by em http request
|
50
|
+
if req[:timeout] or req[:open_timeout]
|
51
|
+
options[:timeout] = [req[:timeout] || 0, req[:open_timeout] || 0].max
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
client = nil
|
56
|
+
block = lambda { request.send env[:method].to_s.downcase.to_sym, options }
|
57
|
+
if !EM.reactor_running?
|
58
|
+
EM.run {
|
59
|
+
Fiber.new do
|
60
|
+
client = block.call
|
61
|
+
EM.stop
|
62
|
+
end.resume
|
63
|
+
}
|
64
|
+
else
|
65
|
+
client = block.call
|
66
|
+
end
|
67
|
+
|
68
|
+
env.update(:status => client.response_header.http_status.to_i,
|
69
|
+
:response_headers => Header.new(client),
|
70
|
+
:body => client.response)
|
71
|
+
|
72
|
+
@app.call env
|
73
|
+
rescue Errno::ECONNREFUSED
|
74
|
+
raise Error::ConnectionFailed, "connection refused"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/test/adapters/live_test.rb
CHANGED
@@ -124,6 +124,21 @@ if Faraday::TestCase::LIVE_SERVER
|
|
124
124
|
assert_equal '[1,2,3]', resp1.body
|
125
125
|
assert_equal '[1,2,3]', resp2.body
|
126
126
|
end
|
127
|
+
|
128
|
+
if adapter.to_s == "Faraday::Adapter::EMSynchrony"
|
129
|
+
instance_methods.grep(%r{Faraday::Adapter::EMSynchrony}).each do |method|
|
130
|
+
em = method.to_s.sub %r{^test_}, "test_under_em_"
|
131
|
+
define_method em do
|
132
|
+
EM.run do
|
133
|
+
Fiber.new do
|
134
|
+
self.send method
|
135
|
+
EM.stop
|
136
|
+
end.resume
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
127
142
|
end
|
128
143
|
|
129
144
|
def create_connection(adapter)
|
data/test/form_post_test.rb
CHANGED
@@ -39,4 +39,20 @@ class FormPostTest < Faraday::TestCase
|
|
39
39
|
@app.process_body_for_request @env
|
40
40
|
assert_equal 'abc', @env[:body]
|
41
41
|
end
|
42
|
+
|
43
|
+
def test_processes_array_values
|
44
|
+
@env[:body] = {:a => [:b, 1]}
|
45
|
+
@app.process_body_for_request @env
|
46
|
+
assert_equal 'a[]=b&a[]=1', @env[:body]
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_processes_nested_array_values
|
50
|
+
@env[:body] = {:a => [:b, {:c => :d}, [:e]]}
|
51
|
+
@app.process_body_for_request @env
|
52
|
+
|
53
|
+
# a[]=b&a[][c]=d&a[][]=e
|
54
|
+
assert_match /a\[\]=b/, @env[:body]
|
55
|
+
assert_match /a\[\]\[c\]=d/, @env[:body]
|
56
|
+
assert_match /a\[\]\[\]=e/, @env[:body]
|
57
|
+
end
|
42
58
|
end
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 15
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 5
|
9
|
-
-
|
10
|
-
version: 0.5.
|
8
|
+
- 3
|
9
|
+
version: 0.5.3
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Rick Olson
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-
|
17
|
+
date: 2010-11-09 00:00:00 -08:00
|
19
18
|
default_executable:
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
@@ -26,7 +25,6 @@ dependencies:
|
|
26
25
|
requirements:
|
27
26
|
- - ~>
|
28
27
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 27
|
30
28
|
segments:
|
31
29
|
- 0
|
32
30
|
- 8
|
@@ -41,7 +39,6 @@ dependencies:
|
|
41
39
|
requirements:
|
42
40
|
- - ~>
|
43
41
|
- !ruby/object:Gem::Version
|
44
|
-
hash: 3
|
45
42
|
segments:
|
46
43
|
- 2
|
47
44
|
- 2
|
@@ -57,7 +54,6 @@ dependencies:
|
|
57
54
|
requirements:
|
58
55
|
- - ~>
|
59
56
|
- !ruby/object:Gem::Version
|
60
|
-
hash: 21
|
61
57
|
segments:
|
62
58
|
- 1
|
63
59
|
- 0
|
@@ -73,7 +69,6 @@ dependencies:
|
|
73
69
|
requirements:
|
74
70
|
- - ">="
|
75
71
|
- !ruby/object:Gem::Version
|
76
|
-
hash: 19
|
77
72
|
segments:
|
78
73
|
- 1
|
79
74
|
- 1
|
@@ -81,7 +76,6 @@ dependencies:
|
|
81
76
|
version: 1.1.0
|
82
77
|
- - <
|
83
78
|
- !ruby/object:Gem::Version
|
84
|
-
hash: 7
|
85
79
|
segments:
|
86
80
|
- 2
|
87
81
|
version: "2"
|
@@ -105,6 +99,7 @@ files:
|
|
105
99
|
- lib/faraday.rb
|
106
100
|
- lib/faraday/adapter.rb
|
107
101
|
- lib/faraday/adapter/action_dispatch.rb
|
102
|
+
- lib/faraday/adapter/em_synchrony.rb
|
108
103
|
- lib/faraday/adapter/net_http.rb
|
109
104
|
- lib/faraday/adapter/patron.rb
|
110
105
|
- lib/faraday/adapter/test.rb
|
@@ -147,7 +142,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
147
142
|
requirements:
|
148
143
|
- - ">="
|
149
144
|
- !ruby/object:Gem::Version
|
150
|
-
hash: 3
|
151
145
|
segments:
|
152
146
|
- 0
|
153
147
|
version: "0"
|
@@ -156,7 +150,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
150
|
requirements:
|
157
151
|
- - ">="
|
158
152
|
- !ruby/object:Gem::Version
|
159
|
-
hash: 23
|
160
153
|
segments:
|
161
154
|
- 1
|
162
155
|
- 3
|