agiley-faraday_middleware 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.gemtest +0 -0
  2. data/.gitignore +31 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +7 -0
  5. data/CHANGELOG.md +10 -0
  6. data/Gemfile +7 -0
  7. data/LICENSE.md +20 -0
  8. data/README.md +54 -0
  9. data/Rakefile +17 -0
  10. data/faraday_middleware.gemspec +24 -0
  11. data/lib/faraday_middleware.rb +42 -0
  12. data/lib/faraday_middleware/addressable_patch.rb +20 -0
  13. data/lib/faraday_middleware/backwards_compatibility.rb +15 -0
  14. data/lib/faraday_middleware/instrumentation.rb +30 -0
  15. data/lib/faraday_middleware/rack_compatible.rb +76 -0
  16. data/lib/faraday_middleware/request/encode_json.rb +50 -0
  17. data/lib/faraday_middleware/request/oauth.rb +64 -0
  18. data/lib/faraday_middleware/request/oauth2.rb +62 -0
  19. data/lib/faraday_middleware/response/caching.rb +76 -0
  20. data/lib/faraday_middleware/response/follow_redirects.rb +53 -0
  21. data/lib/faraday_middleware/response/mashify.rb +28 -0
  22. data/lib/faraday_middleware/response/parse_json.rb +38 -0
  23. data/lib/faraday_middleware/response/parse_marshal.rb +13 -0
  24. data/lib/faraday_middleware/response/parse_nokogiri_xml.rb +14 -0
  25. data/lib/faraday_middleware/response/parse_xml.rb +14 -0
  26. data/lib/faraday_middleware/response/parse_yaml.rb +13 -0
  27. data/lib/faraday_middleware/response/rashify.rb +13 -0
  28. data/lib/faraday_middleware/response_middleware.rb +78 -0
  29. data/lib/faraday_middleware/version.rb +3 -0
  30. data/spec/caching_test.rb +122 -0
  31. data/spec/encode_json_spec.rb +95 -0
  32. data/spec/follow_redirects_spec.rb +33 -0
  33. data/spec/helper.rb +33 -0
  34. data/spec/mashify_spec.rb +79 -0
  35. data/spec/oauth2_spec.rb +118 -0
  36. data/spec/oauth_spec.rb +101 -0
  37. data/spec/parse_json_spec.rb +94 -0
  38. data/spec/parse_marshal_spec.rb +16 -0
  39. data/spec/parse_xml_spec.rb +71 -0
  40. data/spec/parse_yaml_spec.rb +53 -0
  41. data/spec/rashify_spec.rb +69 -0
  42. metadata +202 -0
data/.gemtest ADDED
File without changes
data/.gitignore ADDED
@@ -0,0 +1,31 @@
1
+ # Mac OS X
2
+ .DS_Store
3
+
4
+ # TextMate
5
+ *.tmproj
6
+ tmtags
7
+
8
+ # emails
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ # vim
14
+ *.swp
15
+
16
+ # General
17
+ coverage
18
+ rdoc
19
+ doc
20
+ log
21
+ .yardoc
22
+
23
+ # Bundler
24
+ *.gem
25
+ .bundle
26
+ Gemfile.lock
27
+ pkg
28
+ *.gem
29
+
30
+ # Rubinius
31
+ *.rbc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --backtrace
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - jruby
6
+ - rbx
7
+ - ree
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,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'simplecov' unless ENV['CI']
4
+ gem 'json', :platforms => [:ruby_18, :jruby]
5
+ gem 'jruby-openssl', '~> 0.7', :platforms => :jruby
6
+
7
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Erik Michaels-Ober, Wynn Netherland, et al.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ Faraday Middleware
2
+ ==================
3
+
4
+ A collection of useful [Faraday][] middleware. [See the documentation][docs].
5
+
6
+ gem install faraday_middleware
7
+
8
+ Dependencies
9
+ ------------
10
+
11
+ Some dependent libraries are needed only when using specific middleware:
12
+
13
+ * FaradayMiddleware::EncodeJson & FaradayMiddleware::ParseJson: "json"
14
+ for ruby older than 1.9
15
+ * FaradayMiddleware::ParseXml: "multi_xml"
16
+ * FaradayMiddleware::OAuth: "simple_oauth"
17
+ * FaradayMiddleware::Mashify: "hashie"
18
+ * FaradayMiddleware::Rashify: "rash"
19
+ * FaradayMiddleware::Instrumentation: "activesupport"
20
+
21
+ Examples
22
+ --------
23
+
24
+ ``` rb
25
+ require 'faraday_middleware'
26
+
27
+ ## in Faraday 0.8 or above:
28
+ connection = Faraday.new 'http://example.com/api' do |conn|
29
+ conn.request :oauth2, 'TOKEN'
30
+ conn.request :json
31
+
32
+ conn.response :xml, :content_type => /\bxml$/
33
+ conn.response :json, :content_type => /\bjson$/
34
+
35
+ conn.use :instrumentation
36
+ conn.adapter Faraday.default_adapter
37
+ end
38
+
39
+ ## with Faraday 0.7:
40
+ connection = Faraday.new 'http://example.com/api' do |builder|
41
+ builder.use FaradayMiddleware::OAuth2, 'TOKEN'
42
+ builder.use FaradayMiddleware::EncodeJson
43
+
44
+ builder.use FaradayMiddleware::ParseXml, :content_type => /\bxml$/
45
+ builder.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
46
+
47
+ builder.use FaradayMiddleware::Instrumentation
48
+ builder.adapter Faraday.default_adapter
49
+ end
50
+ ```
51
+
52
+
53
+ [faraday]: https://github.com/technoweenie/faraday#readme
54
+ [docs]: https://github.com/pengwynn/faraday_middleware/wiki
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env rake
2
+
3
+ task :default => [:enable_coverage, :spec, :test]
4
+
5
+ require 'bundler'
6
+ Bundler::GemHelper.install_tasks
7
+
8
+ require 'rspec/core/rake_task'
9
+ RSpec::Core::RakeTask.new(:spec)
10
+
11
+ task :enable_coverage do
12
+ ENV['COVERAGE'] = 'yes' unless ENV['CI']
13
+ end
14
+
15
+ task :test do
16
+ sh 'ruby', '-Ilib', 'spec/caching_test.rb'
17
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../lib/faraday_middleware/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.add_dependency 'faraday', ['>= 0.7.4', '< 0.9']
5
+ gem.add_development_dependency 'multi_xml', '~> 0.2'
6
+ gem.add_development_dependency 'nokogiri', '~> 1.5.0'
7
+ gem.add_development_dependency 'rake', '~> 0.9'
8
+ gem.add_development_dependency 'hashie', '~> 1.2'
9
+ gem.add_development_dependency 'rash', '~> 0.3'
10
+ gem.add_development_dependency 'rspec', '~> 2.6'
11
+ gem.add_development_dependency 'simple_oauth', '~> 0.1'
12
+ gem.add_development_dependency 'rack-cache', '~> 1.1'
13
+ gem.authors = ["Erik Michaels-Ober", "Wynn Netherland"]
14
+ gem.description = %q{Various middleware for Faraday}
15
+ gem.email = ['sferik@gmail.com', 'wynn.netherland@gmail.com']
16
+ gem.files = `git ls-files`.split("\n")
17
+ gem.homepage = 'https://github.com/pengwynn/faraday_middleware'
18
+ gem.name = 'agiley-faraday_middleware'
19
+ gem.require_paths = ['lib']
20
+ gem.required_rubygems_version = Gem::Requirement.new('>= 1.3.6')
21
+ gem.summary = gem.description
22
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ gem.version = FaradayMiddleware::VERSION
24
+ end
@@ -0,0 +1,42 @@
1
+ require 'faraday'
2
+
3
+ module FaradayMiddleware
4
+ autoload :OAuth, 'faraday_middleware/request/oauth'
5
+ autoload :OAuth2, 'faraday_middleware/request/oauth2'
6
+ autoload :EncodeJson, 'faraday_middleware/request/encode_json'
7
+ autoload :Mashify, 'faraday_middleware/response/mashify'
8
+ autoload :Rashify, 'faraday_middleware/response/rashify'
9
+ autoload :ParseJson, 'faraday_middleware/response/parse_json'
10
+ autoload :ParseXml, 'faraday_middleware/response/parse_xml'
11
+ autoload :ParseNokogiriXml, 'faraday_middleware/response/parse_nokogiri_xml'
12
+ autoload :ParseMarshal, 'faraday_middleware/response/parse_marshal'
13
+ autoload :ParseYaml, 'faraday_middleware/response/parse_yaml'
14
+ autoload :Caching, 'faraday_middleware/response/caching'
15
+ autoload :RackCompatible, 'faraday_middleware/rack_compatible'
16
+ autoload :FollowRedirects, 'faraday_middleware/response/follow_redirects'
17
+ autoload :Instrumentation, 'faraday_middleware/instrumentation'
18
+
19
+ if Faraday.respond_to? :register_middleware
20
+ Faraday.register_middleware :request,
21
+ :oauth => lambda { OAuth },
22
+ :oauth2 => lambda { OAuth2 },
23
+ :json => lambda { EncodeJson }
24
+
25
+ Faraday.register_middleware :response,
26
+ :mashify => lambda { Mashify },
27
+ :rashify => lambda { Rashify },
28
+ :json => lambda { ParseJson },
29
+ :json_fix => lambda { ParseJson::MimeTypeFix },
30
+ :xml => lambda { ParseXml },
31
+ :nokogiri_xml => lambda { ParseNokogiriXml },
32
+ :marshal => lambda { ParseMarshal },
33
+ :yaml => lambda { ParseYaml },
34
+ :caching => lambda { Caching },
35
+ :follow_redirects => lambda { FollowRedirects }
36
+
37
+ Faraday.register_middleware \
38
+ :instrumentation => lambda { Instrumentation }
39
+ end
40
+ end
41
+
42
+ require 'faraday_middleware/backwards_compatibility'
@@ -0,0 +1,20 @@
1
+ require 'addressable/uri'
2
+
3
+ # feature-detect the bug
4
+ unless Addressable::URI.parse('/?a=1&b=2') === '/?b=2&a=1'
5
+ # fix `normalized_query` by sorting query key-value pairs
6
+ # (rejected: https://github.com/sporkmonger/addressable/issues/28)
7
+ class Addressable::URI
8
+ alias normalized_query_without_ordering_fix normalized_query
9
+
10
+ def normalized_query
11
+ fresh = @normalized_query.nil?
12
+ query = normalized_query_without_ordering_fix
13
+ if query && fresh
14
+ @normalized_query = query.split('&', -1).sort_by {|q| q[0..(q.index('=')||-1)] }.join('&')
15
+ else
16
+ query
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ # deprecated constants
2
+
3
+ Faraday::Request.class_eval do
4
+ autoload :OAuth, 'faraday_middleware/request/oauth'
5
+ autoload :OAuth2, 'faraday_middleware/request/oauth2'
6
+ end
7
+
8
+ Faraday::Response.class_eval do
9
+ autoload :Mashify, 'faraday_middleware/response/mashify'
10
+ autoload :Rashify, 'faraday_middleware/response/rashify'
11
+ autoload :ParseJson, 'faraday_middleware/response/parse_json'
12
+ autoload :ParseXml, 'faraday_middleware/response/parse_xml'
13
+ autoload :ParseMarshal, 'faraday_middleware/response/parse_marshal'
14
+ autoload :ParseYaml, 'faraday_middleware/response/parse_yaml'
15
+ end
@@ -0,0 +1,30 @@
1
+ require 'faraday'
2
+
3
+ module FaradayMiddleware
4
+ # Public: Instruments requests using Active Support.
5
+ #
6
+ # Measures time spent only for synchronous requests.
7
+ #
8
+ # Examples
9
+ #
10
+ # ActiveSupport::Notifications.subscribe('request.faraday') do |name, start_time, end_time, _, env|
11
+ # url = env[:url]
12
+ # http_method = env[:method].to_s.upcase
13
+ # duration = end_time - start_time
14
+ # $stderr.puts '[%s] %s %s (%.3f s)' % [url.host, http_method, url.request_uri, duration]
15
+ # end
16
+ class Instrumentation < Faraday::Middleware
17
+ dependency 'active_support/notifications'
18
+
19
+ def initialize(app, options = {})
20
+ super(app)
21
+ @name = options.fetch(:name, 'request.faraday')
22
+ end
23
+
24
+ def call(env)
25
+ ActiveSupport::Notifications.instrument(@name, env) do
26
+ @app.call(env)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,76 @@
1
+ require 'stringio'
2
+
3
+ module FaradayMiddleware
4
+ # Wraps a handler originally written for Rack to make it compatible with Faraday.
5
+ #
6
+ # Experimental. Only handles changes in request headers.
7
+ class RackCompatible
8
+ def initialize(app, rack_handler, *args)
9
+ # tiny middleware that decomposes a Faraday::Response to standard Rack
10
+ # array: [status, headers, body]
11
+ compatible_app = lambda do |env|
12
+ restore_env(env)
13
+ response = app.call(env)
14
+ [response.status, response.headers, Array(response.body)]
15
+ end
16
+ @rack = rack_handler.new(compatible_app, *args)
17
+ end
18
+
19
+ def call(env)
20
+ prepare_env(env)
21
+ rack_response = @rack.call(env)
22
+ finalize_response(env, rack_response)
23
+ end
24
+
25
+ NonPrefixedHeaders = %w[CONTENT_LENGTH CONTENT_TYPE]
26
+
27
+ # faraday to rack-compatible
28
+ def prepare_env(env)
29
+ env[:request_headers].each do |name, value|
30
+ name = name.upcase.tr('-', '_')
31
+ name = "HTTP_#{name}" unless NonPrefixedHeaders.include? name
32
+ env[name] = value
33
+ end
34
+
35
+ url = env[:url]
36
+ env['rack.url_scheme'] = url.scheme
37
+ env['PATH_INFO'] = url.path
38
+ env['SERVER_PORT'] = url.respond_to?(:inferred_port) ? url.inferred_port : url.port
39
+ env['QUERY_STRING'] = url.query
40
+ env['REQUEST_METHOD'] = env[:method].to_s.upcase
41
+
42
+ env['rack.errors'] ||= StringIO.new
43
+
44
+ env
45
+ end
46
+
47
+ # rack to faraday-compatible
48
+ def restore_env(env)
49
+ headers = env[:request_headers]
50
+ headers.clear
51
+
52
+ env.each do |name, value|
53
+ next unless String === name
54
+ if NonPrefixedHeaders.include? name or name.index('HTTP_') == 0
55
+ name = name.sub(/^HTTP_/, '').downcase.tr('_', '-')
56
+ headers[name] = value
57
+ end
58
+ end
59
+
60
+ env[:method] = env['REQUEST_METHOD'].downcase.to_sym
61
+ env
62
+ end
63
+
64
+ def finalize_response(env, rack_response)
65
+ status, headers, body = rack_response
66
+ body = body.inject('') { |str, part| str << part }
67
+ headers = Faraday::Utils::Headers.new(headers) unless Faraday::Utils::Headers === headers
68
+
69
+ response_env = { :status => status, :body => body, :response_headers => headers }
70
+
71
+ env[:response] ||= Faraday::Response.new({})
72
+ env[:response].env.update(response_env)
73
+ env[:response]
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,50 @@
1
+ require 'faraday'
2
+
3
+ module FaradayMiddleware
4
+ # Request middleware that encodes the body as JSON.
5
+ #
6
+ # Processes only requests with matching Content-type or those without a type.
7
+ # If a request doesn't have a type but has a body, it sets the Content-type
8
+ # to JSON MIME-type.
9
+ #
10
+ # Doesn't try to encode bodies that already are in string form.
11
+ class EncodeJson < Faraday::Middleware
12
+ CONTENT_TYPE = 'Content-Type'.freeze
13
+ MIME_TYPE = 'application/json'.freeze
14
+
15
+ dependency 'json'
16
+
17
+ def call(env)
18
+ match_content_type(env) do |data|
19
+ env[:body] = encode data
20
+ end
21
+ @app.call env
22
+ end
23
+
24
+ def encode(data)
25
+ JSON.dump data
26
+ end
27
+
28
+ def match_content_type(env)
29
+ if process_request?(env)
30
+ env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
31
+ yield env[:body] unless env[:body].respond_to?(:to_str)
32
+ end
33
+ end
34
+
35
+ def process_request?(env)
36
+ type = request_type(env)
37
+ has_body?(env) and (type.empty? or type == MIME_TYPE)
38
+ end
39
+
40
+ def has_body?(env)
41
+ body = env[:body] and !(body.respond_to?(:to_str) and body.empty?)
42
+ end
43
+
44
+ def request_type(env)
45
+ type = env[:request_headers][CONTENT_TYPE].to_s
46
+ type = type.split(';', 2).first if type.index(';')
47
+ type
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,64 @@
1
+ require 'faraday'
2
+
3
+ module FaradayMiddleware
4
+ # Public: Uses the simple_oauth library to sign requests according the
5
+ # OAuth protocol.
6
+ #
7
+ # The options for this middleware are forwarded to SimpleOAuth::Header:
8
+ # :consumer_key, :consumer_secret, :token, :token_secret. All these
9
+ # parameters are optional.
10
+ #
11
+ # The signature is added to the "Authorization" HTTP request header. If the
12
+ # value for this header already exists, it is not overriden.
13
+ #
14
+ # For requests that have parameters in the body, such as POST, this
15
+ # middleware expects them to be in Hash form, i.e. not encoded to string.
16
+ # This means this middleware has to be positioned on the stack before any
17
+ # encoding middleware such as UrlEncoded.
18
+ class OAuth < Faraday::Middleware
19
+ dependency 'simple_oauth'
20
+
21
+ AUTH_HEADER = 'Authorization'.freeze
22
+
23
+ def initialize(app, options)
24
+ super(app)
25
+ @options = options
26
+ end
27
+
28
+ def call(env)
29
+ env[:request_headers][AUTH_HEADER] ||= oauth_header(env).to_s if sign_request?(env)
30
+ @app.call(env)
31
+ end
32
+
33
+ def oauth_header(env)
34
+ SimpleOAuth::Header.new env[:method],
35
+ env[:url].to_s,
36
+ signature_params(body_params(env)),
37
+ oauth_options(env)
38
+ end
39
+
40
+ def sign_request?(env)
41
+ !!env[:request].fetch(:oauth, true)
42
+ end
43
+
44
+ def oauth_options(env)
45
+ if extra = env[:request][:oauth] and extra.is_a? Hash and !extra.empty?
46
+ @options.merge extra
47
+ else
48
+ @options
49
+ end
50
+ end
51
+
52
+ def body_params(env)
53
+ env[:body] || {}
54
+ end
55
+
56
+ def signature_params(params)
57
+ params.empty? ? params :
58
+ params.reject {|k,v| v.respond_to?(:content_type) }
59
+ end
60
+ end
61
+ end
62
+
63
+ # deprecated alias
64
+ Faraday::Request::OAuth = FaradayMiddleware::OAuth