faraday_middleware 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +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
data/CHANGELOG.md ADDED
@@ -0,0 +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
10
+ * Mashify
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ faraday_middleware (0.0.3)
5
+ faraday (~> 0.4.5)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ addressable (2.2.1)
11
+ faraday (0.4.6)
12
+ addressable (>= 2.1.1)
13
+ rack (>= 1.0.1)
14
+ hashie (0.4.0)
15
+ mcmire-matchy (0.5.2)
16
+ multi_json (0.0.4)
17
+ rack (1.2.1)
18
+ shoulda (2.11.3)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ faraday (~> 0.4.5)
25
+ faraday_middleware!
26
+ hashie (~> 0.4.0)
27
+ mcmire-matchy (~> 0.5.2)
28
+ multi_json (~> 0.0.4)
29
+ shoulda (~> 2.11.3)
data/README.md ADDED
@@ -0,0 +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
43
+ # => "Wynn Netherland"
data/Rakefile ADDED
@@ -0,0 +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
@@ -0,0 +1,24 @@
1
+ require File.expand_path("../lib/faraday_middleware/version", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.add_development_dependency('hashie', ['~> 0.4.0'])
5
+ s.add_development_dependency('mcmire-matchy', ['~> 0.5.2'])
6
+ s.add_development_dependency('multi_json', ['~> 0.0.4'])
7
+ s.add_development_dependency('shoulda', ['~> 2.11.3'])
8
+ s.add_runtime_dependency('faraday', ['~> 0.4.5'])
9
+ s.authors = ["Wynn Netherland"]
10
+ s.description = %q{Various middleware for Faraday}
11
+ s.email = ['wynn.netherland@gmail.com']
12
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
13
+ s.extra_rdoc_files = ["README.md"]
14
+ s.files = `git ls-files`.split("\n")
15
+ s.homepage = 'http://wynnnetherland.com/projects/faraday-middleware/'
16
+ s.name = 'faraday_middleware'
17
+ s.platform = Gem::Platform::RUBY
18
+ s.rdoc_options = ["--charset=UTF-8"]
19
+ s.require_paths = ["lib"]
20
+ s.required_rubygems_version = ">= 1.3.6"
21
+ s.summary = %q{Various middleware for Faraday}
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.version = FaradayMiddleware::VERSION
24
+ end
@@ -0,0 +1,25 @@
1
+ module Faraday
2
+ class Response::Mashify < Response::Middleware
3
+ begin
4
+ require 'hashie'
5
+
6
+ def self.register_on_complete(env)
7
+ env[:response].on_complete do |finished_env|
8
+ json = finished_env[:body]
9
+ if json.is_a?(Hash)
10
+ finished_env[:body] = Hashie::Mash.new(json)
11
+ elsif json.is_a?(Array) and json.first.is_a?(Hash)
12
+ finished_env[:body] = json.map{|item| Hashie::Mash.new(item) }
13
+ end
14
+ end
15
+ end
16
+ rescue LoadError, NameError => e
17
+ self.load_error = e
18
+ end
19
+
20
+ def initialize(app)
21
+ super
22
+ @parser = nil
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ module Faraday
2
+ class Response::MultiJson < Response::Middleware
3
+ begin
4
+ require 'multi_json'
5
+
6
+ def self.register_on_complete(env)
7
+ env[:response].on_complete do |finished_env|
8
+ finished_env[:body] = begin
9
+ case finished_env[:body]
10
+ when ""
11
+ nil
12
+ when "true"
13
+ true
14
+ when "false"
15
+ false
16
+ else
17
+ MultiJson.decode(finished_env[:body])
18
+ end
19
+ end
20
+ end
21
+ end
22
+ rescue LoadError, NameError => e
23
+ self.load_error = e
24
+ end
25
+
26
+ def initialize(app)
27
+ super
28
+ @parser = nil
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ module Faraday
2
+ class Request::OAuth2 < Faraday::Middleware
3
+ begin
4
+ require 'oauth2'
5
+ rescue LoadError, NameError => e
6
+ self.load_error = e
7
+ end
8
+ def initialize(app, *args)
9
+ @app = app
10
+ @token = args.shift
11
+ end
12
+
13
+ def call(env)
14
+ params = env[:url].query_values || {}
15
+ env[:url].query_values = params.merge('access_token' => @token)
16
+ env[:request_headers].merge!('Authorization' => "Token token=\"#{@token}\"")
17
+
18
+ @app.call env
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,6 @@
1
+ require 'faraday'
2
+
3
+ directory = File.expand_path(File.dirname(__FILE__))
4
+ require File.join(directory, 'faraday', 'oauth2')
5
+ require File.join(directory, 'faraday', 'multi_json')
6
+ require File.join(directory, 'faraday', 'mashify')
@@ -0,0 +1,3 @@
1
+ module FaradayMiddleware
2
+ VERSION = "0.0.4".freeze
3
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'test/unit'
2
+ require 'pathname'
3
+ require 'rubygems'
4
+ require 'shoulda'
5
+ require 'faraday'
6
+
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ require 'faraday_middleware'
10
+
11
+ class Test::Unit::TestCase
12
+ end
@@ -0,0 +1,35 @@
1
+ require 'helper'
2
+
3
+ class MashifyTest < 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::MultiJson
10
+ builder.use Faraday::Response::Mashify
11
+ end
12
+ end
13
+
14
+ should 'create a Hashie::Mash from the body' do
15
+ @stubs.get('/hash') { [200, {}, "{\"name\":\"Wynn Netherland\",\"username\":\"pengwynn\"}"] }
16
+ me = @conn.get("/hash").body
17
+ assert_equal 'Wynn Netherland', me.name
18
+ assert_equal 'pengwynn', me.username
19
+ end
20
+
21
+ should 'handle arrays' do
22
+ @stubs.get('/array') { [200, {}, "[{\"username\":\"pengwynn\"},{\"username\":\"jnunemaker\"}]" ] }
23
+ us = @conn.get("/array").body
24
+ assert_equal 'pengwynn', us.first.username
25
+ assert_equal 'jnunemaker', us.last.username
26
+ end
27
+
28
+ should 'handle arrays of non-hashes' do
29
+ @stubs.get('/array/simple') { [200, {}, "[123, 456]" ] }
30
+ values = @conn.get("/array/simple").body
31
+ assert_equal 123, values.first
32
+ assert_equal 456, values.last
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,75 @@
1
+ require 'helper'
2
+
3
+ class Multi_Json_Test < 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::MultiJson
10
+ end
11
+ end
12
+
13
+ context "when there is a JSON body" do
14
+ setup do
15
+ @stubs.get('/me') { [200, {}, "{\"name\":\"Wynn Netherland\",\"username\":\"pengwynn\"}"] }
16
+ end
17
+
18
+ should 'parse the body as JSON' do
19
+ me = @conn.get("/me").body
20
+ assert me.is_a?(Hash)
21
+ assert_equal 'Wynn Netherland', me['name']
22
+ assert_equal 'pengwynn', me['username']
23
+ end
24
+ end
25
+
26
+ context "when the JSON body is empty" do
27
+ setup do
28
+ @stubs.get('/me') { [200, {}, ""] }
29
+ end
30
+
31
+ should 'still have the status code' do
32
+ response = @conn.get("/me")
33
+ assert_equal 200, response.status
34
+ end
35
+
36
+ should 'set body to nil' do
37
+ response = @conn.get("/me")
38
+ assert_equal response.body, nil
39
+ end
40
+ end
41
+
42
+ context "when the JSON body is 'true'" do
43
+ setup do
44
+ @stubs.get('/me') { [200, {}, "true"] }
45
+ end
46
+
47
+ should 'still have the status code' do
48
+ response = @conn.get("/me")
49
+ assert_equal 200, response.status
50
+ end
51
+
52
+ should 'set body to nil' do
53
+ response = @conn.get("/me")
54
+ assert_equal response.body, true
55
+ end
56
+ end
57
+
58
+ context "when the JSON body is 'false'" do
59
+ setup do
60
+ @stubs.get('/me') { [200, {}, "false"] }
61
+ end
62
+
63
+ should 'still have the status code' do
64
+ response = @conn.get("/me")
65
+ assert_equal 200, response.status
66
+ end
67
+
68
+ should 'set body to nil' do
69
+ response = @conn.get("/me")
70
+ assert_equal response.body, false
71
+ end
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,18 @@
1
+ require 'helper'
2
+
3
+ class OAuth2Test < Test::Unit::TestCase
4
+ context 'when used' do
5
+ setup do
6
+ @stubs = Faraday::Adapter::Test::Stubs.new
7
+ @stubs.get('/me?access_token=OU812') { [200, {}, 'pengwynn'] }
8
+ end
9
+
10
+ should 'add the access token to the request' do
11
+ do_you = Faraday::Connection.new do |builder|
12
+ builder.use Faraday::Request::OAuth2, 'OU812'
13
+ builder.adapter :test, @stubs
14
+ end
15
+ do_you.get("/me")
16
+ end
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday_middleware
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 4
10
+ version: 0.0.4
11
+ platform: ruby
12
+ authors:
13
+ - Wynn Netherland
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-27 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: hashie
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 0
32
+ - 4
33
+ - 0
34
+ version: 0.4.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: mcmire-matchy
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 15
46
+ segments:
47
+ - 0
48
+ - 5
49
+ - 2
50
+ version: 0.5.2
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: multi_json
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: 23
62
+ segments:
63
+ - 0
64
+ - 0
65
+ - 4
66
+ version: 0.0.4
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: shoulda
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ hash: 37
78
+ segments:
79
+ - 2
80
+ - 11
81
+ - 3
82
+ version: 2.11.3
83
+ type: :development
84
+ version_requirements: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ name: faraday
87
+ prerelease: false
88
+ requirement: &id005 !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ hash: 5
94
+ segments:
95
+ - 0
96
+ - 4
97
+ - 5
98
+ version: 0.4.5
99
+ type: :runtime
100
+ version_requirements: *id005
101
+ description: Various middleware for Faraday
102
+ email:
103
+ - wynn.netherland@gmail.com
104
+ executables: []
105
+
106
+ extensions: []
107
+
108
+ extra_rdoc_files:
109
+ - README.md
110
+ files:
111
+ - .gitignore
112
+ - CHANGELOG.md
113
+ - Gemfile
114
+ - Gemfile.lock
115
+ - README.md
116
+ - Rakefile
117
+ - faraday_middleware.gemspec
118
+ - lib/faraday/mashify.rb
119
+ - lib/faraday/multi_json.rb
120
+ - lib/faraday/oauth2.rb
121
+ - lib/faraday_middleware.rb
122
+ - lib/faraday_middleware/version.rb
123
+ - test/helper.rb
124
+ - test/mashify_test.rb
125
+ - test/multi_json_test.rb
126
+ - test/oauth2_test.rb
127
+ has_rdoc: true
128
+ homepage: http://wynnnetherland.com/projects/faraday-middleware/
129
+ licenses: []
130
+
131
+ post_install_message:
132
+ rdoc_options:
133
+ - --charset=UTF-8
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ hash: 3
142
+ segments:
143
+ - 0
144
+ version: "0"
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ hash: 23
151
+ segments:
152
+ - 1
153
+ - 3
154
+ - 6
155
+ version: 1.3.6
156
+ requirements: []
157
+
158
+ rubyforge_project:
159
+ rubygems_version: 1.3.7
160
+ signing_key:
161
+ specification_version: 3
162
+ summary: Various middleware for Faraday
163
+ test_files:
164
+ - test/helper.rb
165
+ - test/mashify_test.rb
166
+ - test/multi_json_test.rb
167
+ - test/oauth2_test.rb