faraday_middleware 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -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 CHANGED
@@ -1,3 +1,3 @@
1
- source "http://rubygems.org"
2
-
3
- gemspec
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -1,11 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- faraday_middleware (0.1.0)
4
+ faraday_middleware (0.1.1)
5
5
  faraday (~> 0.4.5)
6
6
  hashie (~> 0.4.0)
7
7
  multi_json (~> 0.0.4)
8
+ multi_xml (~> 0.0.1)
8
9
  oauth2 (~> 0.0.13)
10
+ roauth (~> 0.0.3)
9
11
 
10
12
  GEM
11
13
  remote: http://rubygems.org/
@@ -17,11 +19,13 @@ GEM
17
19
  hashie (0.4.0)
18
20
  json (1.4.6)
19
21
  multi_json (0.0.4)
22
+ multi_xml (0.0.1)
20
23
  oauth2 (0.0.13)
21
24
  faraday (~> 0.4.1)
22
25
  multi_json (>= 0.0.4)
23
26
  rack (1.2.1)
24
27
  rake (0.8.7)
28
+ roauth (0.0.3)
25
29
  shoulda (2.11.3)
26
30
 
27
31
  PLATFORMS
@@ -33,6 +37,8 @@ DEPENDENCIES
33
37
  hashie (~> 0.4.0)
34
38
  json (~> 1.4.6)
35
39
  multi_json (~> 0.0.4)
40
+ multi_xml (~> 0.0.1)
36
41
  oauth2 (~> 0.0.13)
37
42
  rake (~> 0.8.7)
43
+ roauth (~> 0.0.3)
38
44
  shoulda (~> 2.11.3)
data/README.md CHANGED
@@ -1,43 +1,43 @@
1
- # Faraday Middleware
2
-
3
- Collection of [Faraday](http://github.com/technoweenie/faraday) middlewares I've been using in some of my API wrappers
4
-
5
-
6
- ## Installation
7
-
8
- sudo gem install faraday-middleware
9
-
10
-
11
- #### Some examples
12
-
13
- Let's decode the response body with [MultiJson](http://github.com/intridea/multi_json)
14
-
15
- conn = Faraday::Connection.new(:url => 'http://api.twitter.com/1') do |builder|
16
- builder.adapter Faraday.default_adapter
17
- builder.use Faraday::Response::MultiJson
18
- end
19
-
20
- resp = conn.get do |req|
21
- req.url '/users/show.json', :screen_name => 'pengwynn'
22
- end
23
-
24
- u = resp.body
25
- u['name']
26
- # => "Wynn Netherland"
27
-
28
-
29
- Want to ditch the brackets and use dot notation? [Mashify](http://github.com/intridea/hashie) it!
30
-
31
- conn = Faraday::Connection.new(:url => 'http://api.twitter.com/1') do |builder|
32
- builder.adapter Faraday.default_adapter
33
- builder.use Faraday::Response::MultiJson
34
- builder.use Faraday::Response::Mashify
35
- end
36
-
37
- resp = conn.get do |req|
38
- req.url '/users/show.json', :screen_name => 'pengwynn'
39
- end
40
-
41
- u = resp.body
42
- u.name
1
+ # Faraday Middleware
2
+
3
+ Collection of [Faraday](http://github.com/technoweenie/faraday) middlewares I've been using in some of my API wrappers
4
+
5
+
6
+ ## Installation
7
+
8
+ sudo gem install faraday_middleware
9
+
10
+
11
+ #### Some examples
12
+
13
+ Let's decode the response body with [MultiJson](http://github.com/intridea/multi_json)
14
+
15
+ conn = Faraday::Connection.new(:url => 'http://api.twitter.com/1') do |builder|
16
+ builder.adapter Faraday.default_adapter
17
+ builder.use Faraday::Response::MultiJson
18
+ end
19
+
20
+ resp = conn.get do |req|
21
+ req.url '/users/show.json', :screen_name => 'pengwynn'
22
+ end
23
+
24
+ u = resp.body
25
+ u['name']
26
+ # => "Wynn Netherland"
27
+
28
+
29
+ Want to ditch the brackets and use dot notation? [Mashify](http://github.com/intridea/hashie) it!
30
+
31
+ conn = Faraday::Connection.new(:url => 'http://api.twitter.com/1') do |builder|
32
+ builder.adapter Faraday.default_adapter
33
+ builder.use Faraday::Response::MultiJson
34
+ builder.use Faraday::Response::Mashify
35
+ end
36
+
37
+ resp = conn.get do |req|
38
+ req.url '/users/show.json', :screen_name => 'pengwynn'
39
+ end
40
+
41
+ u = resp.body
42
+ u.name
43
43
  # => "Wynn Netherland"
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
@@ -8,7 +8,9 @@ Gem::Specification.new do |s|
8
8
  s.add_runtime_dependency('faraday', ['~> 0.4.5'])
9
9
  s.add_runtime_dependency('hashie', ['~> 0.4.0'])
10
10
  s.add_runtime_dependency('multi_json', ['~> 0.0.4'])
11
+ s.add_runtime_dependency('multi_xml', ['~> 0.0.1'])
11
12
  s.add_runtime_dependency('oauth2', ['~> 0.0.13'])
13
+ s.add_runtime_dependency('roauth', ['~> 0.0.3'])
12
14
  s.authors = ["Wynn Netherland"]
13
15
  s.description = %q{Various middleware for Faraday}
14
16
  s.email = ['wynn.netherland@gmail.com']
@@ -0,0 +1,22 @@
1
+ require 'roauth'
2
+
3
+ module Faraday
4
+ class Request::OAuth < Faraday::Middleware
5
+ def initialize(app, *args)
6
+ @app = app
7
+ @oauth_params = {
8
+ :consumer_secret => args.shift,
9
+ :consumer_key => args.shift,
10
+ :access_key => args.shift,
11
+ :access_secret => args.shift
12
+ }
13
+
14
+ end
15
+
16
+ def call(env)
17
+ params = env[:url].query_values || {}
18
+ env[:request_headers].merge!('Authorization' => ROAuth.header(@oauth_params, env[:url], params))
19
+ @app.call env
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ require 'multi_xml'
2
+
3
+ module Faraday
4
+ class Response::ParseXml < Response::Middleware
5
+ def self.register_on_complete(env)
6
+ env[:response].on_complete do |response|
7
+ response[:body] = begin
8
+ MultiXml.parse(response[:body])
9
+ end
10
+ end
11
+ end
12
+
13
+ def initialize(app)
14
+ super
15
+ @parser = nil
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module FaradayMiddleware
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -12,21 +12,21 @@ class MashifyTest < Test::Unit::TestCase
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, {}, '{"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, {}, '[{"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, {}, "[123, 456]"]}
30
30
  values = @conn.get("/array/simple").body
31
31
  assert_equal 123, values.first
32
32
  assert_equal 456, values.last
@@ -4,7 +4,7 @@ class OAuth2Test < Test::Unit::TestCase
4
4
  context 'when used' do
5
5
  setup do
6
6
  @stubs = Faraday::Adapter::Test::Stubs.new
7
- @stubs.get('/me?access_token=OU812') { [200, {}, 'pengwynn'] }
7
+ @stubs.get('/me?access_token=OU812') {[200, {}, 'pengwynn']}
8
8
  end
9
9
 
10
10
  should 'add the access token to the request' do
@@ -0,0 +1,20 @@
1
+ require 'helper'
2
+
3
+ class OAuthTest < Test::Unit::TestCase
4
+ context 'when used' do
5
+ setup do
6
+ @stubs = Faraday::Adapter::Test::Stubs.new
7
+ @stubs.get('/me') { [200, {}, 'pengwynn'] }
8
+
9
+ end
10
+
11
+ should 'add the access token to the request' do
12
+ do_you = Faraday::Connection.new do |builder|
13
+ builder.use Faraday::Request::OAuth, 'OU812', 'vh5150', '8675309', '8008135'
14
+ builder.adapter :test, @stubs
15
+ end
16
+ resp = do_you.get("/me")
17
+
18
+ end
19
+ end
20
+ end
@@ -12,7 +12,7 @@ class ParseJsonTest < Test::Unit::TestCase
12
12
 
13
13
  context "when there is a JSON body" do
14
14
  setup do
15
- @stubs.get('/me') { [200, {}, "{\"name\":\"Wynn Netherland\",\"username\":\"pengwynn\"}"] }
15
+ @stubs.get('/me') {[200, {}, '{"name":"Wynn Netherland","username":"pengwynn"}']}
16
16
  end
17
17
 
18
18
  should 'parse the body as JSON' do
@@ -25,7 +25,7 @@ class ParseJsonTest < Test::Unit::TestCase
25
25
 
26
26
  context "when the JSON body is empty" do
27
27
  setup do
28
- @stubs.get('/me') { [200, {}, ""] }
28
+ @stubs.get('/me') {[200, {}, ""]}
29
29
  end
30
30
 
31
31
  should 'still have the status code' do
@@ -35,13 +35,13 @@ class ParseJsonTest < Test::Unit::TestCase
35
35
 
36
36
  should 'set body to nil' do
37
37
  response = @conn.get("/me")
38
- assert_equal response.body, nil
38
+ assert_equal nil, response.body
39
39
  end
40
40
  end
41
41
 
42
42
  context "when the JSON body is 'true'" do
43
43
  setup do
44
- @stubs.get('/me') { [200, {}, "true"] }
44
+ @stubs.get('/me') {[200, {}, "true"]}
45
45
  end
46
46
 
47
47
  should 'still have the status code' do
@@ -49,15 +49,15 @@ class ParseJsonTest < Test::Unit::TestCase
49
49
  assert_equal 200, response.status
50
50
  end
51
51
 
52
- should 'set body to nil' do
52
+ should 'set body to true' do
53
53
  response = @conn.get("/me")
54
- assert_equal response.body, true
54
+ assert_equal true, response.body
55
55
  end
56
56
  end
57
57
 
58
58
  context "when the JSON body is 'false'" do
59
59
  setup do
60
- @stubs.get('/me') { [200, {}, "false"] }
60
+ @stubs.get('/me') {[200, {}, "false"]}
61
61
  end
62
62
 
63
63
  should 'still have the status code' do
@@ -65,9 +65,9 @@ class ParseJsonTest < Test::Unit::TestCase
65
65
  assert_equal 200, response.status
66
66
  end
67
67
 
68
- should 'set body to nil' do
68
+ should 'set body to false' do
69
69
  response = @conn.get("/me")
70
- assert_equal response.body, false
70
+ assert_equal false, response.body
71
71
  end
72
72
  end
73
73
 
@@ -0,0 +1,44 @@
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
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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
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-01 00:00:00 -07:00
18
+ date: 2010-10-07 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -131,9 +131,25 @@ dependencies:
131
131
  type: :runtime
132
132
  version_requirements: *id007
133
133
  - !ruby/object:Gem::Dependency
134
- name: oauth2
134
+ name: multi_xml
135
135
  prerelease: false
136
136
  requirement: &id008 !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ hash: 29
142
+ segments:
143
+ - 0
144
+ - 0
145
+ - 1
146
+ version: 0.0.1
147
+ type: :runtime
148
+ version_requirements: *id008
149
+ - !ruby/object:Gem::Dependency
150
+ name: oauth2
151
+ prerelease: false
152
+ requirement: &id009 !ruby/object:Gem::Requirement
137
153
  none: false
138
154
  requirements:
139
155
  - - ~>
@@ -145,7 +161,23 @@ dependencies:
145
161
  - 13
146
162
  version: 0.0.13
147
163
  type: :runtime
148
- version_requirements: *id008
164
+ version_requirements: *id009
165
+ - !ruby/object:Gem::Dependency
166
+ name: roauth
167
+ prerelease: false
168
+ requirement: &id010 !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ~>
172
+ - !ruby/object:Gem::Version
173
+ hash: 25
174
+ segments:
175
+ - 0
176
+ - 0
177
+ - 3
178
+ version: 0.0.3
179
+ type: :runtime
180
+ version_requirements: *id010
149
181
  description: Various middleware for Faraday
150
182
  email:
151
183
  - wynn.netherland@gmail.com
@@ -164,14 +196,18 @@ files:
164
196
  - Rakefile
165
197
  - faraday_middleware.gemspec
166
198
  - lib/faraday/mashify.rb
199
+ - lib/faraday/oauth.rb
167
200
  - lib/faraday/oauth2.rb
168
201
  - lib/faraday/parse_json.rb
202
+ - lib/faraday/parse_xml.rb
169
203
  - lib/faraday_middleware.rb
170
204
  - lib/faraday_middleware/version.rb
171
205
  - test/helper.rb
172
206
  - test/mashify_test.rb
173
- - test/multi_json_test.rb
174
207
  - test/oauth2_test.rb
208
+ - test/oauth_test.rb
209
+ - test/parse_json_test.rb
210
+ - test/parse_xml_test.rb
175
211
  has_rdoc: true
176
212
  homepage: http://wynnnetherland.com/projects/faraday-middleware/
177
213
  licenses: []
@@ -211,5 +247,7 @@ summary: Various middleware for Faraday
211
247
  test_files:
212
248
  - test/helper.rb
213
249
  - test/mashify_test.rb
214
- - test/multi_json_test.rb
215
250
  - test/oauth2_test.rb
251
+ - test/oauth_test.rb
252
+ - test/parse_json_test.rb
253
+ - test/parse_xml_test.rb