faraday_middleware 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +4 -4
- data/README.md +2 -2
- data/faraday_middleware.gemspec +1 -1
- data/lib/faraday/parse_json.rb +31 -0
- data/lib/faraday/parse_xml.rb +22 -0
- data/lib/faraday_middleware/version.rb +1 -1
- data/test/mashify_test.rb +1 -1
- data/test/{parse_test.rb → parse_json_test.rb} +2 -33
- data/test/parse_xml_test.rb +56 -0
- metadata +11 -20
- data/lib/faraday/parse.rb +0 -49
data/Gemfile.lock
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
faraday_middleware (0.
|
5
|
-
faraday (~> 0.5.
|
4
|
+
faraday_middleware (0.3.0)
|
5
|
+
faraday (~> 0.5.3)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
10
|
addressable (2.2.2)
|
11
|
-
faraday (0.5.
|
11
|
+
faraday (0.5.3)
|
12
12
|
addressable (~> 2.2.2)
|
13
13
|
multipart-post (~> 1.0.1)
|
14
14
|
rack (>= 1.1.0, < 2)
|
@@ -29,7 +29,7 @@ PLATFORMS
|
|
29
29
|
ruby
|
30
30
|
|
31
31
|
DEPENDENCIES
|
32
|
-
faraday (~> 0.5.
|
32
|
+
faraday (~> 0.5.3)
|
33
33
|
faraday_middleware!
|
34
34
|
hashie (~> 0.4)
|
35
35
|
json (~> 1.4)
|
data/README.md
CHANGED
@@ -14,7 +14,7 @@ Let's decode the response body with [MultiJson](http://github.com/intridea/multi
|
|
14
14
|
|
15
15
|
conn = Faraday::Connection.new(:url => 'http://api.twitter.com/1') do |builder|
|
16
16
|
builder.adapter Faraday.default_adapter
|
17
|
-
builder.use Faraday::Response::
|
17
|
+
builder.use Faraday::Response::ParseJson
|
18
18
|
end
|
19
19
|
|
20
20
|
resp = conn.get do |req|
|
@@ -30,7 +30,7 @@ Want to ditch the brackets and use dot notation? [Mashify](http://github.com/int
|
|
30
30
|
|
31
31
|
conn = Faraday::Connection.new(:url => 'http://api.twitter.com/1') do |builder|
|
32
32
|
builder.adapter Faraday.default_adapter
|
33
|
-
builder.use Faraday::Response::
|
33
|
+
builder.use Faraday::Response::ParseJson
|
34
34
|
builder.use Faraday::Response::Mashify
|
35
35
|
end
|
36
36
|
|
data/faraday_middleware.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.add_development_dependency('rake', '~> 0.8')
|
10
10
|
s.add_development_dependency('shoulda', '~> 2.11')
|
11
11
|
s.add_development_dependency('test-unit', '~> 2.1')
|
12
|
-
s.add_runtime_dependency('faraday', '~> 0.5.
|
12
|
+
s.add_runtime_dependency('faraday', '~> 0.5.3')
|
13
13
|
s.authors = ["Wynn Netherland"]
|
14
14
|
s.description = %q{Various middleware for Faraday}
|
15
15
|
s.email = ['wynn.netherland@gmail.com']
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Faraday
|
2
|
+
class Response::ParseJson < Response::Middleware
|
3
|
+
begin
|
4
|
+
require 'multi_json'
|
5
|
+
rescue LoadError, NameError => error
|
6
|
+
self.load_error = error
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.register_on_complete(env)
|
10
|
+
env[:response].on_complete do |response|
|
11
|
+
response[:body] = begin
|
12
|
+
case response[:body]
|
13
|
+
when ''
|
14
|
+
nil
|
15
|
+
when 'true'
|
16
|
+
true
|
17
|
+
when 'false'
|
18
|
+
false
|
19
|
+
else
|
20
|
+
::MultiJson.decode(response[:body])
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(app)
|
27
|
+
super
|
28
|
+
@parser = nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Faraday
|
2
|
+
class Response::ParseXml < Response::Middleware
|
3
|
+
begin
|
4
|
+
require 'multi_xml'
|
5
|
+
rescue LoadError, NameError => error
|
6
|
+
self.load_error = error
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.register_on_complete(env)
|
10
|
+
env[:response].on_complete do |response|
|
11
|
+
response[:body] = begin
|
12
|
+
::MultiXml.parse(response[:body])
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(app)
|
18
|
+
super
|
19
|
+
@parser = nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/test/mashify_test.rb
CHANGED
@@ -6,7 +6,7 @@ class MashifyTest < Test::Unit::TestCase
|
|
6
6
|
@stubs = Faraday::Adapter::Test::Stubs.new
|
7
7
|
@conn = Faraday::Connection.new do |builder|
|
8
8
|
builder.adapter :test, @stubs
|
9
|
-
builder.use Faraday::Response::
|
9
|
+
builder.use Faraday::Response::ParseJson
|
10
10
|
builder.use Faraday::Response::Mashify
|
11
11
|
end
|
12
12
|
end
|
@@ -1,13 +1,12 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
-
class
|
3
|
+
class ParseJsonTest < Test::Unit::TestCase
|
4
4
|
context 'when used' do
|
5
5
|
setup do
|
6
6
|
@stubs = Faraday::Adapter::Test::Stubs.new
|
7
7
|
@conn = Faraday::Connection.new do |builder|
|
8
8
|
builder.adapter :test, @stubs
|
9
|
-
|
10
|
-
builder.use Faraday::Response::Parse
|
9
|
+
builder.use Faraday::Response::ParseJson
|
11
10
|
end
|
12
11
|
end
|
13
12
|
|
@@ -71,35 +70,5 @@ class ParseTest < Test::Unit::TestCase
|
|
71
70
|
assert_equal false, response.body
|
72
71
|
end
|
73
72
|
end
|
74
|
-
|
75
|
-
context "when there is a XML body" do
|
76
|
-
setup do
|
77
|
-
@stubs.get('/me') {[200, {'content-type' => 'application/xml; charset=utf-8'}, '<user><name>Erik Michaels-Ober</name><username>sferik</username></user>']}
|
78
|
-
end
|
79
|
-
|
80
|
-
should 'parse the body as XML' do
|
81
|
-
me = @conn.get("/me").body['user']
|
82
|
-
assert me.is_a?(Hash)
|
83
|
-
assert_equal 'Erik Michaels-Ober', me['name']
|
84
|
-
assert_equal 'sferik', me['username']
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
context "when the XML body is empty" do
|
89
|
-
setup do
|
90
|
-
@stubs.get('/me') {[200, {'content-type' => 'application/xml; charset=utf-8'}, ""]}
|
91
|
-
end
|
92
|
-
|
93
|
-
should 'still have the status code' do
|
94
|
-
response = @conn.get("/me")
|
95
|
-
assert_equal 200, response.status
|
96
|
-
end
|
97
|
-
|
98
|
-
should 'set body to nil' do
|
99
|
-
response = @conn.get("/me")
|
100
|
-
assert_equal Hash.new, response.body
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
73
|
end
|
105
74
|
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class ParseXmlTest < Test::Unit::TestCase
|
4
|
+
context 'when used' do
|
5
|
+
setup do
|
6
|
+
@stubs = Faraday::Adapter::Test::Stubs.new
|
7
|
+
@conn = Faraday::Connection.new do |builder|
|
8
|
+
builder.adapter :test, @stubs
|
9
|
+
builder.use Faraday::Response::ParseXml
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "when there is a XML body" do
|
14
|
+
setup do
|
15
|
+
@stubs.get('/me') {[200, {'content-type' => 'application/xml; charset=utf-8'}, '<user><name>Erik Michaels-Ober</name><username>sferik</username></user>']}
|
16
|
+
end
|
17
|
+
|
18
|
+
should 'parse the body as XML' do
|
19
|
+
me = @conn.get("/me").body['user']
|
20
|
+
assert me.is_a?(Hash)
|
21
|
+
assert_equal 'Erik Michaels-Ober', me['name']
|
22
|
+
assert_equal 'sferik', me['username']
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when there is a ATOM body" do
|
27
|
+
setup do
|
28
|
+
@stubs.get('/me') {[200, {'content-type' => 'application/atom+xml; charset=utf-8'}, '<user><name>Erik Michaels-Ober</name><username>sferik</username></user>']}
|
29
|
+
end
|
30
|
+
|
31
|
+
should 'parse the body as XML' do
|
32
|
+
me = @conn.get("/me").body['user']
|
33
|
+
assert me.is_a?(Hash)
|
34
|
+
assert_equal 'Erik Michaels-Ober', me['name']
|
35
|
+
assert_equal 'sferik', me['username']
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "when the XML body is empty" do
|
40
|
+
setup do
|
41
|
+
@stubs.get('/me') {[200, {'content-type' => 'application/xml; charset=utf-8'}, ""]}
|
42
|
+
end
|
43
|
+
|
44
|
+
should 'still have the status code' do
|
45
|
+
response = @conn.get("/me")
|
46
|
+
assert_equal 200, response.status
|
47
|
+
end
|
48
|
+
|
49
|
+
should 'set body to nil' do
|
50
|
+
response = @conn.get("/me")
|
51
|
+
assert_equal Hash.new, response.body
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday_middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 17
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
|
-
- 2
|
9
7
|
- 3
|
10
|
-
|
8
|
+
- 0
|
9
|
+
version: 0.3.0
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Wynn Netherland
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-11-
|
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: 3
|
30
28
|
segments:
|
31
29
|
- 0
|
32
30
|
- 4
|
@@ -41,7 +39,6 @@ dependencies:
|
|
41
39
|
requirements:
|
42
40
|
- - ~>
|
43
41
|
- !ruby/object:Gem::Version
|
44
|
-
hash: 7
|
45
42
|
segments:
|
46
43
|
- 1
|
47
44
|
- 4
|
@@ -56,7 +53,6 @@ dependencies:
|
|
56
53
|
requirements:
|
57
54
|
- - ~>
|
58
55
|
- !ruby/object:Gem::Version
|
59
|
-
hash: 11
|
60
56
|
segments:
|
61
57
|
- 0
|
62
58
|
- 0
|
@@ -71,7 +67,6 @@ dependencies:
|
|
71
67
|
requirements:
|
72
68
|
- - ~>
|
73
69
|
- !ruby/object:Gem::Version
|
74
|
-
hash: 15
|
75
70
|
segments:
|
76
71
|
- 0
|
77
72
|
- 2
|
@@ -86,7 +81,6 @@ dependencies:
|
|
86
81
|
requirements:
|
87
82
|
- - ~>
|
88
83
|
- !ruby/object:Gem::Version
|
89
|
-
hash: 9
|
90
84
|
segments:
|
91
85
|
- 0
|
92
86
|
- 1
|
@@ -101,7 +95,6 @@ dependencies:
|
|
101
95
|
requirements:
|
102
96
|
- - ~>
|
103
97
|
- !ruby/object:Gem::Version
|
104
|
-
hash: 27
|
105
98
|
segments:
|
106
99
|
- 0
|
107
100
|
- 8
|
@@ -116,7 +109,6 @@ dependencies:
|
|
116
109
|
requirements:
|
117
110
|
- - ~>
|
118
111
|
- !ruby/object:Gem::Version
|
119
|
-
hash: 21
|
120
112
|
segments:
|
121
113
|
- 2
|
122
114
|
- 11
|
@@ -131,7 +123,6 @@ dependencies:
|
|
131
123
|
requirements:
|
132
124
|
- - ~>
|
133
125
|
- !ruby/object:Gem::Version
|
134
|
-
hash: 1
|
135
126
|
segments:
|
136
127
|
- 2
|
137
128
|
- 1
|
@@ -146,12 +137,11 @@ dependencies:
|
|
146
137
|
requirements:
|
147
138
|
- - ~>
|
148
139
|
- !ruby/object:Gem::Version
|
149
|
-
hash: 15
|
150
140
|
segments:
|
151
141
|
- 0
|
152
142
|
- 5
|
153
|
-
-
|
154
|
-
version: 0.5.
|
143
|
+
- 3
|
144
|
+
version: 0.5.3
|
155
145
|
type: :runtime
|
156
146
|
version_requirements: *id009
|
157
147
|
description: Various middleware for Faraday
|
@@ -173,13 +163,15 @@ files:
|
|
173
163
|
- faraday_middleware.gemspec
|
174
164
|
- lib/faraday/mashify.rb
|
175
165
|
- lib/faraday/oauth2.rb
|
176
|
-
- lib/faraday/
|
166
|
+
- lib/faraday/parse_json.rb
|
167
|
+
- lib/faraday/parse_xml.rb
|
177
168
|
- lib/faraday_middleware.rb
|
178
169
|
- lib/faraday_middleware/version.rb
|
179
170
|
- test/helper.rb
|
180
171
|
- test/mashify_test.rb
|
181
172
|
- test/oauth2_test.rb
|
182
|
-
- test/
|
173
|
+
- test/parse_json_test.rb
|
174
|
+
- test/parse_xml_test.rb
|
183
175
|
has_rdoc: true
|
184
176
|
homepage: http://wynnnetherland.com/projects/faraday-middleware/
|
185
177
|
licenses: []
|
@@ -194,7 +186,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
194
186
|
requirements:
|
195
187
|
- - ">="
|
196
188
|
- !ruby/object:Gem::Version
|
197
|
-
hash: 3
|
198
189
|
segments:
|
199
190
|
- 0
|
200
191
|
version: "0"
|
@@ -203,7 +194,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
203
194
|
requirements:
|
204
195
|
- - ">="
|
205
196
|
- !ruby/object:Gem::Version
|
206
|
-
hash: 23
|
207
197
|
segments:
|
208
198
|
- 1
|
209
199
|
- 3
|
@@ -220,4 +210,5 @@ test_files:
|
|
220
210
|
- test/helper.rb
|
221
211
|
- test/mashify_test.rb
|
222
212
|
- test/oauth2_test.rb
|
223
|
-
- test/
|
213
|
+
- test/parse_json_test.rb
|
214
|
+
- test/parse_xml_test.rb
|
data/lib/faraday/parse.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
class Response::Parse < Response::Middleware
|
3
|
-
begin
|
4
|
-
require 'multi_json'
|
5
|
-
require 'multi_xml'
|
6
|
-
rescue LoadError, NameError => error
|
7
|
-
self.load_error = error
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.register_on_complete(env)
|
11
|
-
env[:response].on_complete do |response|
|
12
|
-
response[:body] = begin
|
13
|
-
case response[:response_headers].values_at('content-type', 'Content-Type').first
|
14
|
-
when /\/json/
|
15
|
-
parse_json(response[:body])
|
16
|
-
when /\/xml/
|
17
|
-
parse_xml(response[:body])
|
18
|
-
else
|
19
|
-
''
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def initialize(app)
|
26
|
-
super
|
27
|
-
@parser = nil
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
def self.parse_json(response_body)
|
33
|
-
case response_body
|
34
|
-
when ''
|
35
|
-
nil
|
36
|
-
when 'true'
|
37
|
-
true
|
38
|
-
when 'false'
|
39
|
-
false
|
40
|
-
else
|
41
|
-
::MultiJson.decode(response_body)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def self.parse_xml(response_body)
|
46
|
-
::MultiXml.parse(response_body)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|