faraday_middleware 0.1.7 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +24 -24
- data/CHANGELOG.md +9 -9
- data/Gemfile.lock +4 -2
- data/Rakefile +14 -14
- data/faraday_middleware.gemspec +1 -0
- data/lib/faraday/mashify.rb +6 -6
- data/lib/faraday/oauth2.rb +1 -1
- data/lib/faraday/parse.rb +39 -0
- data/lib/faraday_middleware/version.rb +1 -1
- data/test/mashify_test.rb +4 -4
- data/test/{parse_json_test.rb → parse_test.rb} +36 -6
- metadata +25 -13
- data/lib/faraday/parse_json.rb +0 -31
- data/lib/faraday/parse_xml.rb +0 -22
- data/test/parse_xml_test.rb +0 -44
data/.gitignore
CHANGED
@@ -1,24 +1,24 @@
|
|
1
|
-
## MAC OS
|
2
|
-
.DS_Store
|
3
|
-
|
4
|
-
## TEXTMATE
|
5
|
-
*.tmproj
|
6
|
-
tmtags
|
7
|
-
|
8
|
-
## EMACS
|
9
|
-
*~
|
10
|
-
\#*
|
11
|
-
.\#*
|
12
|
-
|
13
|
-
## VIM
|
14
|
-
*.swp
|
15
|
-
|
16
|
-
## PROJECT::GENERAL
|
17
|
-
coverage
|
18
|
-
rdoc
|
19
|
-
pkg
|
20
|
-
*.gem
|
21
|
-
.yardopts
|
22
|
-
.yardoc
|
23
|
-
|
24
|
-
## PROJECT::SPECIFIC
|
1
|
+
## MAC OS
|
2
|
+
.DS_Store
|
3
|
+
|
4
|
+
## TEXTMATE
|
5
|
+
*.tmproj
|
6
|
+
tmtags
|
7
|
+
|
8
|
+
## EMACS
|
9
|
+
*~
|
10
|
+
\#*
|
11
|
+
.\#*
|
12
|
+
|
13
|
+
## VIM
|
14
|
+
*.swp
|
15
|
+
|
16
|
+
## PROJECT::GENERAL
|
17
|
+
coverage
|
18
|
+
rdoc
|
19
|
+
pkg
|
20
|
+
*.gem
|
21
|
+
.yardopts
|
22
|
+
.yardoc
|
23
|
+
|
24
|
+
## PROJECT::SPECIFIC
|
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
# Changelog
|
2
|
-
|
3
|
-
### 0.0.2 September 25, 2010
|
4
|
-
|
5
|
-
* Mashify now handles arrays of non-hashes
|
6
|
-
|
7
|
-
### 0.0.1 June 27, 2010
|
8
|
-
|
9
|
-
* MultiJSON
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
### 0.0.2 September 25, 2010
|
4
|
+
|
5
|
+
* Mashify now handles arrays of non-hashes
|
6
|
+
|
7
|
+
### 0.0.1 June 27, 2010
|
8
|
+
|
9
|
+
* MultiJSON
|
10
10
|
* Mashify
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
faraday_middleware (0.
|
4
|
+
faraday_middleware (0.2.0)
|
5
5
|
faraday (~> 0.5.1)
|
6
6
|
|
7
7
|
GEM
|
@@ -15,7 +15,7 @@ GEM
|
|
15
15
|
hashie (0.4.0)
|
16
16
|
json (1.4.6)
|
17
17
|
multi_json (0.0.4)
|
18
|
-
multi_xml (0.1.
|
18
|
+
multi_xml (0.1.4)
|
19
19
|
multipart-post (1.0.1)
|
20
20
|
oauth2 (0.1.0)
|
21
21
|
faraday (~> 0.5.0)
|
@@ -23,6 +23,7 @@ GEM
|
|
23
23
|
rack (1.2.1)
|
24
24
|
rake (0.8.7)
|
25
25
|
shoulda (2.11.3)
|
26
|
+
test-unit (2.1.1)
|
26
27
|
|
27
28
|
PLATFORMS
|
28
29
|
ruby
|
@@ -37,3 +38,4 @@ DEPENDENCIES
|
|
37
38
|
oauth2 (~> 0.1)
|
38
39
|
rake (~> 0.8)
|
39
40
|
shoulda (~> 2.11)
|
41
|
+
test-unit (~> 2.1)
|
data/Rakefile
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
2
|
-
require 'rake'
|
3
|
-
require 'rake/testtask'
|
4
|
-
require 'bundler'
|
5
|
-
|
6
|
-
Bundler::GemHelper.install_tasks
|
7
|
-
|
8
|
-
Rake::TestTask.new(:test) do |test|
|
9
|
-
test.ruby_opts = ["-rubygems"] if defined? Gem
|
10
|
-
test.libs << "lib" << "test"
|
11
|
-
test.pattern = "test/**/*_test.rb"
|
12
|
-
end
|
13
|
-
|
14
|
-
task :default => :test
|
1
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'bundler'
|
5
|
+
|
6
|
+
Bundler::GemHelper.install_tasks
|
7
|
+
|
8
|
+
Rake::TestTask.new(:test) do |test|
|
9
|
+
test.ruby_opts = ["-rubygems"] if defined? Gem
|
10
|
+
test.libs << "lib" << "test"
|
11
|
+
test.pattern = "test/**/*_test.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
task :default => :test
|
data/faraday_middleware.gemspec
CHANGED
@@ -8,6 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.add_development_dependency('oauth2', '~> 0.1')
|
9
9
|
s.add_development_dependency('rake', '~> 0.8')
|
10
10
|
s.add_development_dependency('shoulda', '~> 2.11')
|
11
|
+
s.add_development_dependency("test-unit", "~> 2.1")
|
11
12
|
s.add_runtime_dependency('faraday', '~> 0.5.1')
|
12
13
|
s.authors = ["Wynn Netherland"]
|
13
14
|
s.description = %q{Various middleware for Faraday}
|
data/lib/faraday/mashify.rb
CHANGED
@@ -4,15 +4,15 @@ module Faraday
|
|
4
4
|
require 'hashie'
|
5
5
|
rescue LoadError, NameError => error
|
6
6
|
self.load_error = error
|
7
|
-
end
|
7
|
+
end
|
8
8
|
|
9
9
|
def self.register_on_complete(env)
|
10
10
|
env[:response].on_complete do |response|
|
11
|
-
|
12
|
-
if
|
13
|
-
response[:body] = ::Hashie::Mash.new(
|
14
|
-
elsif
|
15
|
-
response[:body] =
|
11
|
+
response_body = response[:body]
|
12
|
+
if response_body.is_a?(Hash)
|
13
|
+
response[:body] = ::Hashie::Mash.new(response_body)
|
14
|
+
elsif response_body.is_a?(Array) and response_body.first.is_a?(Hash)
|
15
|
+
response[:body] = response_body.map{|item| ::Hashie::Mash.new(item)}
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
data/lib/faraday/oauth2.rb
CHANGED
@@ -0,0 +1,39 @@
|
|
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]['content-type']
|
14
|
+
when /application\/json/
|
15
|
+
case response[:body]
|
16
|
+
when ''
|
17
|
+
nil
|
18
|
+
when 'true'
|
19
|
+
true
|
20
|
+
when 'false'
|
21
|
+
false
|
22
|
+
else
|
23
|
+
::MultiJson.decode(response[:body])
|
24
|
+
end
|
25
|
+
when /application\/xml/
|
26
|
+
::MultiXml.parse(response[:body])
|
27
|
+
else
|
28
|
+
''
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def initialize(app)
|
35
|
+
super
|
36
|
+
@parser = nil
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/test/mashify_test.rb
CHANGED
@@ -6,27 +6,27 @@ 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::Parse
|
10
10
|
builder.use Faraday::Response::Mashify
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
should 'create a Hashie::Mash from the body' do
|
15
|
-
@stubs.get('/hash') {[200, {}, '{"name":"Wynn Netherland","username":"pengwynn"}']}
|
15
|
+
@stubs.get('/hash') {[200, {'content-type' => 'application/json; charset=utf-8'}, '{"name":"Wynn Netherland","username":"pengwynn"}']}
|
16
16
|
me = @conn.get("/hash").body
|
17
17
|
assert_equal 'Wynn Netherland', me.name
|
18
18
|
assert_equal 'pengwynn', me.username
|
19
19
|
end
|
20
20
|
|
21
21
|
should 'handle arrays' do
|
22
|
-
@stubs.get('/array') {[200, {}, '[{"username":"pengwynn"},{"username":"jnunemaker"}]']}
|
22
|
+
@stubs.get('/array') {[200, {'content-type' => 'application/json; charset=utf-8'}, '[{"username":"pengwynn"},{"username":"jnunemaker"}]']}
|
23
23
|
us = @conn.get("/array").body
|
24
24
|
assert_equal 'pengwynn', us.first.username
|
25
25
|
assert_equal 'jnunemaker', us.last.username
|
26
26
|
end
|
27
27
|
|
28
28
|
should 'handle arrays of non-hashes' do
|
29
|
-
@stubs.get('/array/simple') {[200, {}, "[123, 456]"]}
|
29
|
+
@stubs.get('/array/simple') {[200, {'content-type' => 'application/json; charset=utf-8'}, "[123, 456]"]}
|
30
30
|
values = @conn.get("/array/simple").body
|
31
31
|
assert_equal 123, values.first
|
32
32
|
assert_equal 456, values.last
|
@@ -1,18 +1,19 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
-
class
|
3
|
+
class ParseTest < 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
|
-
|
9
|
+
require 'rexml/document'
|
10
|
+
builder.use Faraday::Response::Parse
|
10
11
|
end
|
11
12
|
end
|
12
13
|
|
13
14
|
context "when there is a JSON body" do
|
14
15
|
setup do
|
15
|
-
@stubs.get('/me') {[200, {}, '{"name":"Wynn Netherland","username":"pengwynn"}']}
|
16
|
+
@stubs.get('/me') {[200, {'content-type' => 'application/json; charset=utf-8'}, '{"name":"Wynn Netherland","username":"pengwynn"}']}
|
16
17
|
end
|
17
18
|
|
18
19
|
should 'parse the body as JSON' do
|
@@ -25,7 +26,7 @@ class ParseJsonTest < Test::Unit::TestCase
|
|
25
26
|
|
26
27
|
context "when the JSON body is empty" do
|
27
28
|
setup do
|
28
|
-
@stubs.get('/me') {[200, {}, ""]}
|
29
|
+
@stubs.get('/me') {[200, {'content-type' => 'application/json; charset=utf-8'}, ""]}
|
29
30
|
end
|
30
31
|
|
31
32
|
should 'still have the status code' do
|
@@ -41,7 +42,7 @@ class ParseJsonTest < Test::Unit::TestCase
|
|
41
42
|
|
42
43
|
context "when the JSON body is 'true'" do
|
43
44
|
setup do
|
44
|
-
@stubs.get('/me') {[200, {}, "true"]}
|
45
|
+
@stubs.get('/me') {[200, {'content-type' => 'application/json; charset=utf-8'}, "true"]}
|
45
46
|
end
|
46
47
|
|
47
48
|
should 'still have the status code' do
|
@@ -57,7 +58,7 @@ class ParseJsonTest < Test::Unit::TestCase
|
|
57
58
|
|
58
59
|
context "when the JSON body is 'false'" do
|
59
60
|
setup do
|
60
|
-
@stubs.get('/me') {[200, {}, "false"]}
|
61
|
+
@stubs.get('/me') {[200, {'content-type' => 'application/json; charset=utf-8'}, "false"]}
|
61
62
|
end
|
62
63
|
|
63
64
|
should 'still have the status code' do
|
@@ -71,5 +72,34 @@ class ParseJsonTest < Test::Unit::TestCase
|
|
71
72
|
end
|
72
73
|
end
|
73
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
|
+
|
74
104
|
end
|
75
105
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday_middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Wynn Netherland
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-20 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -124,9 +124,24 @@ dependencies:
|
|
124
124
|
type: :development
|
125
125
|
version_requirements: *id007
|
126
126
|
- !ruby/object:Gem::Dependency
|
127
|
-
name:
|
127
|
+
name: test-unit
|
128
128
|
prerelease: false
|
129
129
|
requirement: &id008 !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
131
|
+
requirements:
|
132
|
+
- - ~>
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
hash: 1
|
135
|
+
segments:
|
136
|
+
- 2
|
137
|
+
- 1
|
138
|
+
version: "2.1"
|
139
|
+
type: :development
|
140
|
+
version_requirements: *id008
|
141
|
+
- !ruby/object:Gem::Dependency
|
142
|
+
name: faraday
|
143
|
+
prerelease: false
|
144
|
+
requirement: &id009 !ruby/object:Gem::Requirement
|
130
145
|
none: false
|
131
146
|
requirements:
|
132
147
|
- - ~>
|
@@ -138,7 +153,7 @@ dependencies:
|
|
138
153
|
- 1
|
139
154
|
version: 0.5.1
|
140
155
|
type: :runtime
|
141
|
-
version_requirements: *
|
156
|
+
version_requirements: *id009
|
142
157
|
description: Various middleware for Faraday
|
143
158
|
email:
|
144
159
|
- wynn.netherland@gmail.com
|
@@ -158,15 +173,13 @@ files:
|
|
158
173
|
- faraday_middleware.gemspec
|
159
174
|
- lib/faraday/mashify.rb
|
160
175
|
- lib/faraday/oauth2.rb
|
161
|
-
- lib/faraday/
|
162
|
-
- lib/faraday/parse_xml.rb
|
176
|
+
- lib/faraday/parse.rb
|
163
177
|
- lib/faraday_middleware.rb
|
164
178
|
- lib/faraday_middleware/version.rb
|
165
179
|
- test/helper.rb
|
166
180
|
- test/mashify_test.rb
|
167
181
|
- test/oauth2_test.rb
|
168
|
-
- test/
|
169
|
-
- test/parse_xml_test.rb
|
182
|
+
- test/parse_test.rb
|
170
183
|
has_rdoc: true
|
171
184
|
homepage: http://wynnnetherland.com/projects/faraday-middleware/
|
172
185
|
licenses: []
|
@@ -207,5 +220,4 @@ test_files:
|
|
207
220
|
- test/helper.rb
|
208
221
|
- test/mashify_test.rb
|
209
222
|
- test/oauth2_test.rb
|
210
|
-
- test/
|
211
|
-
- test/parse_xml_test.rb
|
223
|
+
- test/parse_test.rb
|
data/lib/faraday/parse_json.rb
DELETED
@@ -1,31 +0,0 @@
|
|
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
|
data/lib/faraday/parse_xml.rb
DELETED
@@ -1,22 +0,0 @@
|
|
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/parse_xml_test.rb
DELETED
@@ -1,44 +0,0 @@
|
|
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
|
-
require 'rexml/document'
|
10
|
-
builder.use Faraday::Response::ParseXml
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
context "when there is a XML body" do
|
15
|
-
setup do
|
16
|
-
@stubs.get('/me') {[200, {}, '<user><name>Erik Michaels-Ober</name><username>sferik</username></user>']}
|
17
|
-
end
|
18
|
-
|
19
|
-
should 'parse the body as XML' do
|
20
|
-
me = @conn.get("/me").body['user']
|
21
|
-
assert me.is_a?(Hash)
|
22
|
-
assert_equal 'Erik Michaels-Ober', me['name']
|
23
|
-
assert_equal 'sferik', me['username']
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
context "when the XML body is empty" do
|
28
|
-
setup do
|
29
|
-
@stubs.get('/me') {[200, {}, ""]}
|
30
|
-
end
|
31
|
-
|
32
|
-
should 'still have the status code' do
|
33
|
-
response = @conn.get("/me")
|
34
|
-
assert_equal 200, response.status
|
35
|
-
end
|
36
|
-
|
37
|
-
should 'set body to nil' do
|
38
|
-
response = @conn.get("/me")
|
39
|
-
assert_equal Hash.new, response.body
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|