forked from lostisland/faraday_middleware
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request lostisland#17 from pengwynn/next
Refactoring & faraday-stack merge
- Loading branch information
Showing
40 changed files
with
1,078 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
--color | ||
--format=nested | ||
--backtrace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
source 'http://rubygems.org' | ||
|
||
platforms :jruby do | ||
gem "jruby-openssl", "~> 0.7" | ||
end | ||
gem 'simplecov' unless ENV['CI'] | ||
gem 'json', :platforms => [:ruby_18, :jruby] | ||
gem 'jruby-openssl', '~> 0.7', :platforms => :jruby | ||
|
||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
#!/usr/bin/env rake | ||
|
||
task :default => [:enable_coverage, :spec, :test] | ||
|
||
require 'bundler' | ||
Bundler::GemHelper.install_tasks | ||
|
||
require 'rspec/core/rake_task' | ||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
task :test => :spec | ||
task :default => :spec | ||
task :enable_coverage do | ||
ENV['COVERAGE'] = 'yes' unless ENV['CI'] | ||
end | ||
|
||
task :test do | ||
sh 'ruby', '-Ilib', 'spec/caching_test.rb' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,55 @@ | ||
require 'faraday' | ||
|
||
class Faraday::Request | ||
autoload :OAuth, 'faraday/request/oauth' | ||
autoload :OAuth2, 'faraday/request/oauth2' | ||
end | ||
module FaradayMiddleware | ||
class << self | ||
middleware = { | ||
:OAuth => 'request/oauth', | ||
:OAuth2 => 'request/oauth2', | ||
:EncodeJson => 'request/encode_json', | ||
:Mashify => 'response/mashify', | ||
:Rashify => 'response/rashify', | ||
:ParseJson => 'response/parse_json', | ||
:ParseXml => 'response/parse_xml', | ||
:ParseMarshal => 'response/parse_marshal', | ||
:ParseYaml => 'response/parse_yaml', | ||
:Caching => 'response/caching', | ||
:RackCompatible => 'rack_compatible', | ||
:FollowRedirects => 'response/follow_redirects', | ||
:Instrumentation => 'instrumentation' | ||
} | ||
|
||
# autoload without the autoload | ||
define_method(:const_missing) { |const| | ||
if middleware.member? const | ||
require "faraday_middleware/#{middleware[const]}" | ||
raise NameError, "missing #{const} middleware" unless const_defined? const | ||
const_get const | ||
else | ||
super | ||
end | ||
} | ||
end | ||
|
||
if Faraday.respond_to? :register_middleware | ||
Faraday.register_middleware :request, | ||
:oauth => lambda { OAuth }, | ||
:oauth2 => lambda { OAuth2 }, | ||
:json => lambda { EncodeJson } | ||
|
||
class Faraday::Response | ||
autoload :Mashify, 'faraday/response/mashify' | ||
autoload :ParseJson, 'faraday/response/parse_json' | ||
autoload :ParseMarshal, 'faraday/response/parse_marshal' | ||
autoload :ParseXml, 'faraday/response/parse_xml' | ||
autoload :ParseYaml, 'faraday/response/parse_yaml' | ||
autoload :Rashify, 'faraday/response/rashify' | ||
Faraday.register_middleware :response, | ||
:mashify => lambda { Mashify }, | ||
:rashify => lambda { Rashify }, | ||
:json => lambda { ParseJson }, | ||
:json_fix => lambda { ParseJson::MimeTypeFix }, | ||
:xml => lambda { ParseXml }, | ||
:marshal => lambda { ParseMarshal }, | ||
:yaml => lambda { ParseYaml }, | ||
:caching => lambda { Caching }, | ||
:follow_redirects => lambda { FollowRedirects } | ||
|
||
Faraday.register_middleware \ | ||
:instrumentation => lambda { Instrumentation } | ||
end | ||
end | ||
|
||
require 'faraday_middleware/backwards_compatibility' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'addressable/uri' | ||
|
||
# feature-detect the bug | ||
unless Addressable::URI.parse('/?a=1&b=2') === '/?b=2&a=1' | ||
# fix `normalized_query` by sorting query key-value pairs | ||
# (rejected: https://github.com/sporkmonger/addressable/issues/28) | ||
class Addressable::URI | ||
alias normalized_query_without_ordering_fix normalized_query | ||
|
||
def normalized_query | ||
fresh = @normalized_query.nil? | ||
query = normalized_query_without_ordering_fix | ||
if query && fresh | ||
@normalized_query = query.split('&', -1).sort_by {|q| q[0..(q.index('=')||-1)] }.join('&') | ||
else | ||
query | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
deprecation_warning = lambda { |old, new, trace| | ||
warn "Deprecation warning: #{old} is deprecated; use #{new}" | ||
warn trace[0,10].join("\n") if $DEBUG | ||
} | ||
|
||
Faraday::Request.extend Module.new { | ||
legacy = [:OAuth, :OAuth2] | ||
define_method(:const_missing) { |const| | ||
if legacy.include? const | ||
klass = FaradayMiddleware.const_get(const) | ||
deprecation_warning.call "Faraday::Request::#{const}", klass.name, caller | ||
const_set const, klass | ||
else | ||
super | ||
end | ||
} | ||
} | ||
|
||
Faraday::Response.extend Module.new { | ||
legacy = [:Mashify, :Rashify, :ParseJson, :ParseMarshal, :ParseXml, :ParseYaml] | ||
define_method(:const_missing) { |const| | ||
if legacy.include? const | ||
klass = FaradayMiddleware.const_get(const) | ||
deprecation_warning.call "Faraday::Response::#{const}", klass.name, caller | ||
const_set const, klass | ||
else | ||
super | ||
end | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'faraday' | ||
|
||
module FaradayMiddleware | ||
# Public: Instruments requests using Active Support. | ||
# | ||
# Measures time spent only for synchronous requests. | ||
# | ||
# Examples | ||
# | ||
# ActiveSupport::Notifications.subscribe('request.faraday') do |name, start_time, end_time, _, env| | ||
# url = env[:url] | ||
# http_method = env[:method].to_s.upcase | ||
# duration = end_time - start_time | ||
# $stderr.puts '[%s] %s %s (%.3f s)' % [url.host, http_method, url.request_uri, duration] | ||
# end | ||
class Instrumentation < Faraday::Middleware | ||
dependency 'active_support/notifications' | ||
|
||
def initialize(app, options = {}) | ||
super(app) | ||
@name = options.fetch(:name, 'request.faraday') | ||
end | ||
|
||
def call(env) | ||
ActiveSupport::Notifications.instrument(@name, env) do | ||
@app.call(env) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
require 'stringio' | ||
|
||
module FaradayMiddleware | ||
# Wraps a handler originally written for Rack to make it compatible with Faraday. | ||
# | ||
# Experimental. Only handles changes in request headers. | ||
class RackCompatible | ||
def initialize(app, rack_handler, *args) | ||
# tiny middleware that decomposes a Faraday::Response to standard Rack | ||
# array: [status, headers, body] | ||
compatible_app = lambda do |env| | ||
restore_env(env) | ||
response = app.call(env) | ||
[response.status, response.headers, Array(response.body)] | ||
end | ||
@rack = rack_handler.new(compatible_app, *args) | ||
end | ||
|
||
def call(env) | ||
prepare_env(env) | ||
rack_response = @rack.call(env) | ||
finalize_response(env, rack_response) | ||
end | ||
|
||
NonPrefixedHeaders = %w[CONTENT_LENGTH CONTENT_TYPE] | ||
|
||
# faraday to rack-compatible | ||
def prepare_env(env) | ||
env[:request_headers].each do |name, value| | ||
name = name.upcase.tr('-', '_') | ||
name = "HTTP_#{name}" unless NonPrefixedHeaders.include? name | ||
env[name] = value | ||
end | ||
|
||
url = env[:url] | ||
env['rack.url_scheme'] = url.scheme | ||
env['PATH_INFO'] = url.path | ||
env['SERVER_PORT'] = url.respond_to?(:inferred_port) ? url.inferred_port : url.port | ||
env['QUERY_STRING'] = url.query | ||
env['REQUEST_METHOD'] = env[:method].to_s.upcase | ||
|
||
env['rack.errors'] ||= StringIO.new | ||
|
||
env | ||
end | ||
|
||
# rack to faraday-compatible | ||
def restore_env(env) | ||
headers = env[:request_headers] | ||
headers.clear | ||
|
||
env.each do |name, value| | ||
next unless String === name | ||
if NonPrefixedHeaders.include? name or name.index('HTTP_') == 0 | ||
name = name.sub(/^HTTP_/, '').downcase.tr('_', '-') | ||
headers[name] = value | ||
end | ||
end | ||
|
||
env[:method] = env['REQUEST_METHOD'].downcase.to_sym | ||
env | ||
end | ||
|
||
def finalize_response(env, rack_response) | ||
status, headers, body = rack_response | ||
body = body.inject('') { |str, part| str << part } | ||
headers = Faraday::Utils::Headers.new(headers) unless Faraday::Utils::Headers === headers | ||
|
||
response_env = { :status => status, :body => body, :response_headers => headers } | ||
|
||
env[:response] ||= Faraday::Response.new({}) | ||
env[:response].env.update(response_env) | ||
env[:response] | ||
end | ||
end | ||
end |
Oops, something went wrong.