faraday_middleware-0.10.0/0000755000004100000410000000000012557571071015436 5ustar www-datawww-datafaraday_middleware-0.10.0/LICENSE.md0000644000004100000410000000207712557571067017055 0ustar www-datawww-dataCopyright (c) 2011 Erik Michaels-Ober, Wynn Netherland, et al. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. faraday_middleware-0.10.0/lib/0000755000004100000410000000000012557571070016203 5ustar www-datawww-datafaraday_middleware-0.10.0/lib/faraday_middleware.rb0000644000004100000410000000417612557571070022344 0ustar www-datawww-datarequire 'faraday' module FaradayMiddleware autoload :OAuth, 'faraday_middleware/request/oauth' autoload :OAuth2, 'faraday_middleware/request/oauth2' autoload :EncodeJson, 'faraday_middleware/request/encode_json' autoload :MethodOverride, 'faraday_middleware/request/method_override' autoload :Mashify, 'faraday_middleware/response/mashify' autoload :Rashify, 'faraday_middleware/response/rashify' autoload :ParseJson, 'faraday_middleware/response/parse_json' autoload :ParseXml, 'faraday_middleware/response/parse_xml' autoload :ParseMarshal, 'faraday_middleware/response/parse_marshal' autoload :ParseYaml, 'faraday_middleware/response/parse_yaml' autoload :ParseDates, 'faraday_middleware/response/parse_dates' autoload :Caching, 'faraday_middleware/response/caching' autoload :Chunked, 'faraday_middleware/response/chunked' autoload :RackCompatible, 'faraday_middleware/rack_compatible' autoload :FollowRedirects, 'faraday_middleware/response/follow_redirects' autoload :Instrumentation, 'faraday_middleware/instrumentation' autoload :Gzip, 'faraday_middleware/gzip' if Faraday::Middleware.respond_to? :register_middleware Faraday::Request.register_middleware \ :oauth => lambda { OAuth }, :oauth2 => lambda { OAuth2 }, :json => lambda { EncodeJson }, :method_override => lambda { MethodOverride } Faraday::Response.register_middleware \ :mashify => lambda { Mashify }, :rashify => lambda { Rashify }, :json => lambda { ParseJson }, :json_fix => lambda { ParseJson::MimeTypeFix }, :xml => lambda { ParseXml }, :marshal => lambda { ParseMarshal }, :yaml => lambda { ParseYaml }, :dates => lambda { ParseDates }, :caching => lambda { Caching }, :follow_redirects => lambda { FollowRedirects }, :chunked => lambda { Chunked } Faraday::Middleware.register_middleware \ :instrumentation => lambda { Instrumentation }, :gzip => lambda { Gzip } end end require 'faraday_middleware/backwards_compatibility' faraday_middleware-0.10.0/lib/faraday_middleware/0000755000004100000410000000000012557571070022007 5ustar www-datawww-datafaraday_middleware-0.10.0/lib/faraday_middleware/response_middleware.rb0000644000004100000410000000524112557571070026371 0ustar www-datawww-datarequire 'faraday' module FaradayMiddleware # Internal: The base class for middleware that parses responses. class ResponseMiddleware < Faraday::Middleware CONTENT_TYPE = 'Content-Type'.freeze class << self attr_accessor :parser end # Store a Proc that receives the body and returns the parsed result. def self.define_parser(parser = nil) @parser = parser || Proc.new end def self.inherited(subclass) super subclass.load_error = self.load_error if subclass.respond_to? :load_error= subclass.parser = self.parser end def initialize(app = nil, options = {}) super(app) @options = options @content_types = Array(options[:content_type]) end def call(environment) @app.call(environment).on_complete do |env| if process_response_type?(response_type(env)) and parse_response?(env) process_response(env) end end end def process_response(env) env[:raw_body] = env[:body] if preserve_raw?(env) env[:body] = parse(env[:body]) end # Parse the response body. # # Instead of overriding this method, consider using `define_parser`. def parse(body) if self.class.parser begin self.class.parser.call(body) rescue StandardError, SyntaxError => err raise err if err.is_a? SyntaxError and err.class.name != 'Psych::SyntaxError' raise Faraday::Error::ParsingError, err end else body end end def response_type(env) type = env[:response_headers][CONTENT_TYPE].to_s type = type.split(';', 2).first if type.index(';') type end def process_response_type?(type) @content_types.empty? or @content_types.any? { |pattern| pattern.is_a?(Regexp) ? type =~ pattern : type == pattern } end def parse_response?(env) env[:body].respond_to? :to_str end def preserve_raw?(env) env[:request].fetch(:preserve_raw, @options[:preserve_raw]) end end # DRAGONS module OptionsExtension attr_accessor :preserve_raw def to_hash super.update(:preserve_raw => preserve_raw) end def each return to_enum(:each) unless block_given? super yield :preserve_raw, preserve_raw end def fetch(key, *args) if :preserve_raw == key value = __send__(key) value.nil? ? args.fetch(0) : value else super end end end if defined?(Faraday::RequestOptions) begin Faraday::RequestOptions.from(:preserve_raw => true) rescue NoMethodError Faraday::RequestOptions.send(:include, OptionsExtension) end end end faraday_middleware-0.10.0/lib/faraday_middleware/backwards_compatibility.rb0000644000004100000410000000115112557571070027224 0ustar www-datawww-data# deprecated constants Faraday::Request.class_eval do autoload :OAuth, 'faraday_middleware/request/oauth' autoload :OAuth2, 'faraday_middleware/request/oauth2' end Faraday::Response.class_eval do autoload :Mashify, 'faraday_middleware/response/mashify' autoload :Rashify, 'faraday_middleware/response/rashify' autoload :ParseJson, 'faraday_middleware/response/parse_json' autoload :ParseXml, 'faraday_middleware/response/parse_xml' autoload :ParseMarshal, 'faraday_middleware/response/parse_marshal' autoload :ParseYaml, 'faraday_middleware/response/parse_yaml' end faraday_middleware-0.10.0/lib/faraday_middleware/rack_compatible.rb0000644000004100000410000000511712557571070025457 0ustar www-datawww-datarequire '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 |rack_env| env = restore_env(rack_env) response = app.call(env) [response.status, response.headers, Array(response.body)] end @rack = rack_handler.new(compatible_app, *args) end def call(env) rack_env = prepare_env(env) rack_response = @rack.call(rack_env) finalize_response(env, rack_response) end NonPrefixedHeaders = %w[CONTENT_LENGTH CONTENT_TYPE] # faraday to rack-compatible def prepare_env(faraday_env) env = headers_to_rack(faraday_env) url = faraday_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'] = faraday_env[:method].to_s.upcase env['rack.errors'] ||= StringIO.new env['faraday'] = faraday_env env end def headers_to_rack(env) rack_env = {} env[:request_headers].each do |name, value| name = name.upcase.tr('-', '_') name = "HTTP_#{name}" unless NonPrefixedHeaders.include? name rack_env[name] = value end rack_env end # rack to faraday-compatible def restore_env(rack_env) env = rack_env.fetch('faraday') headers = env[:request_headers] headers.clear rack_env.each do |name, value| next unless String === name && String === value if NonPrefixedHeaders.include? name or name.index('HTTP_') == 0 name = name.sub(/^HTTP_/, '').downcase.tr('_', '-') headers[name] = value end end env[:method] = rack_env['REQUEST_METHOD'].downcase.to_sym env[:rack_errors] = rack_env['rack.errors'] 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 env.update :status => status.to_i, :body => body, :response_headers => headers env[:response] ||= Faraday::Response.new(env) env[:response] end end end faraday_middleware-0.10.0/lib/faraday_middleware/response/0000755000004100000410000000000012557571070023645 5ustar www-datawww-datafaraday_middleware-0.10.0/lib/faraday_middleware/response/parse_dates.rb0000644000004100000410000000151212557571070026463 0ustar www-datawww-datarequire "time" require "faraday" module FaradayMiddleware # Parse dates from response body class ParseDates < ::Faraday::Response::Middleware ISO_DATE_FORMAT = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z\Z/m def initialize(app, options = {}) @regexp = options[:match] || ISO_DATE_FORMAT super(app) end def call(env) response = @app.call(env) parse_dates! response.env[:body] response end private def parse_dates!(value) case value when Hash value.each do |key, element| value[key] = parse_dates!(element) end when Array value.each_with_index do |element, index| value[index] = parse_dates!(element) end when @regexp Time.parse(value) else value end end end end faraday_middleware-0.10.0/lib/faraday_middleware/response/parse_marshal.rb0000644000004100000410000000054612557571070027020 0ustar www-datawww-datarequire 'faraday_middleware/response_middleware' module FaradayMiddleware # Public: Restore marshalled Ruby objects in response bodies. class ParseMarshal < ResponseMiddleware define_parser do |body| ::Marshal.load body unless body.empty? end end end # deprecated alias Faraday::Response::ParseMarshal = FaradayMiddleware::ParseMarshal faraday_middleware-0.10.0/lib/faraday_middleware/response/mashify.rb0000644000004100000410000000142012557571070025627 0ustar www-datawww-datarequire 'faraday' module FaradayMiddleware # Public: Converts parsed response bodies to a Hashie::Mash if they were of # Hash or Array type. class Mashify < Faraday::Response::Middleware attr_accessor :mash_class class << self attr_accessor :mash_class end dependency do require 'hashie/mash' self.mash_class = ::Hashie::Mash end def initialize(app = nil, options = {}) super(app) self.mash_class = options[:mash_class] || self.class.mash_class end def parse(body) case body when Hash mash_class.new(body) when Array body.map { |item| parse(item) } else body end end end end # deprecated alias Faraday::Response::Mashify = FaradayMiddleware::Mashify faraday_middleware-0.10.0/lib/faraday_middleware/response/follow_redirects.rb0000644000004100000410000001014112557571070027535 0ustar www-datawww-datarequire 'faraday' require 'set' module FaradayMiddleware # Public: Exception thrown when the maximum amount of requests is exceeded. class RedirectLimitReached < Faraday::Error::ClientError attr_reader :response def initialize(response) super "too many redirects; last one to: #{response['location']}" @response = response end end # Public: Follow HTTP 301, 302, 303, and 307 redirects. # # For HTTP 301, 302, and 303, the original GET, POST, PUT, DELETE, or PATCH # request gets converted into a GET. With `:standards_compliant => true`, # however, the HTTP method after 301/302 remains unchanged. This allows you # to opt into HTTP/1.1 compliance and act unlike the major web browsers. # # This middleware currently only works with synchronous requests; i.e. it # doesn't support parallelism. # # If you wish to persist cookies across redirects, you could use # the faraday-cookie_jar gem: # # Faraday.new(:url => url) do |faraday| # faraday.use FaradayMiddleware::FollowRedirects # faraday.use :cookie_jar # faraday.adapter Faraday.default_adapter # end class FollowRedirects < Faraday::Middleware # HTTP methods for which 30x redirects can be followed ALLOWED_METHODS = Set.new [:head, :options, :get, :post, :put, :patch, :delete] # HTTP redirect status codes that this middleware implements REDIRECT_CODES = Set.new [301, 302, 303, 307] # Keys in env hash which will get cleared between requests ENV_TO_CLEAR = Set.new [:status, :response, :response_headers] # Default value for max redirects followed FOLLOW_LIMIT = 3 # Regex that matches characters that need to be escaped in URLs, sans # the "%" character which we assume already represents an escaped sequence. URI_UNSAFE = /[^\-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]%]/ # Public: Initialize the middleware. # # options - An options Hash (default: {}): # :limit - A Numeric redirect limit (default: 3) # :standards_compliant - A Boolean indicating whether to respect # the HTTP spec when following 301/302 # (default: false) def initialize(app, options = {}) super(app) @options = options @convert_to_get = Set.new [303] @convert_to_get << 301 << 302 unless standards_compliant? end def call(env) perform_with_redirection(env, follow_limit) end private def convert_to_get?(response) ![:head, :options].include?(response.env[:method]) && @convert_to_get.include?(response.status) end def perform_with_redirection(env, follows) request_body = env[:body] response = @app.call(env) response.on_complete do |response_env| if follow_redirect?(response_env, response) raise RedirectLimitReached, response if follows.zero? new_request_env = update_env(response_env, request_body, response) response = perform_with_redirection(new_request_env, follows - 1) end end response end def update_env(env, request_body, response) env[:url] += safe_escape(response['location']) if convert_to_get?(response) env[:method] = :get env[:body] = nil else env[:body] = request_body end ENV_TO_CLEAR.each {|key| env.delete key } env end def follow_redirect?(env, response) ALLOWED_METHODS.include? env[:method] and REDIRECT_CODES.include? response.status end def follow_limit @options.fetch(:limit, FOLLOW_LIMIT) end def standards_compliant? @options.fetch(:standards_compliant, false) end # Internal: escapes unsafe characters from an URL which might be a path # component only or a fully qualified URI so that it can be joined onto an # URI:HTTP using the `+` operator. Doesn't escape "%" characters so to not # risk double-escaping. def safe_escape(uri) uri.to_s.gsub(URI_UNSAFE) { |match| '%' + match.unpack('H2' * match.bytesize).join('%').upcase } end end end faraday_middleware-0.10.0/lib/faraday_middleware/response/chunked.rb0000644000004100000410000000170412557571070025615 0ustar www-datawww-datarequire 'faraday_middleware/response_middleware' module FaradayMiddleware # Public: Parse a Transfer-Encoding: Chunked response to just the original data class Chunked < FaradayMiddleware::ResponseMiddleware TRANSFER_ENCODING = 'transfer-encoding'.freeze define_parser do |raw_body| decoded_body = [] until raw_body.empty? chunk_len, raw_body = raw_body.split("\r\n", 2) chunk_len = chunk_len.split(';',2).first.hex break if chunk_len == 0 decoded_body << raw_body[0, chunk_len] # The 2 is to strip the extra CRLF at the end of the chunk raw_body = raw_body[chunk_len + 2, raw_body.length - chunk_len - 2] end decoded_body.join('') end def parse_response?(env) super and chunked_encoding?(env[:response_headers]) end def chunked_encoding?(headers) encoding = headers[TRANSFER_ENCODING] and encoding.split(',').include?('chunked') end end end faraday_middleware-0.10.0/lib/faraday_middleware/response/parse_json.rb0000644000004100000410000000267412557571070026346 0ustar www-datawww-datarequire 'faraday_middleware/response_middleware' module FaradayMiddleware # Public: Parse response bodies as JSON. class ParseJson < ResponseMiddleware dependency do require 'json' unless defined?(::JSON) end define_parser do |body| ::JSON.parse body unless body.strip.empty? end # Public: Override the content-type of the response with "application/json" # if the response body looks like it might be JSON, i.e. starts with an # open bracket. # # This is to fix responses from certain API providers that insist on serving # JSON with wrong MIME-types such as "text/javascript". class MimeTypeFix < ResponseMiddleware MIME_TYPE = 'application/json'.freeze def process_response(env) old_type = env[:response_headers][CONTENT_TYPE].to_s new_type = MIME_TYPE.dup new_type << ';' << old_type.split(';', 2).last if old_type.index(';') env[:response_headers][CONTENT_TYPE] = new_type end BRACKETS = %w- [ { - WHITESPACE = [ " ", "\n", "\r", "\t" ] def parse_response?(env) super and BRACKETS.include? first_char(env[:body]) end def first_char(body) idx = -1 begin char = body[idx += 1] char = char.chr if char end while char and WHITESPACE.include? char char end end end end # deprecated alias Faraday::Response::ParseJson = FaradayMiddleware::ParseJson faraday_middleware-0.10.0/lib/faraday_middleware/response/parse_yaml.rb0000644000004100000410000000157712557571070026340 0ustar www-datawww-datarequire 'faraday_middleware/response_middleware' module FaradayMiddleware # Public: Parse response bodies as YAML. # # Warning: this uses `YAML.load()` by default and as such is not safe against # code injection or DoS attacks. If you're loading resources from an # untrusted host or over HTTP, you should subclass this middleware and # redefine it to use `safe_load()` if you're using a Psych version that # supports it: # # class SafeYaml < FaradayMiddleware::ParseYaml # define_parser do |body| # YAML.safe_load(body) # end # end # # Faraday.new(..) do |config| # config.use SafeYaml # ... # end class ParseYaml < ResponseMiddleware dependency 'yaml' define_parser do |body| ::YAML.load body end end end # deprecated alias Faraday::Response::ParseYaml = FaradayMiddleware::ParseYaml faraday_middleware-0.10.0/lib/faraday_middleware/response/rashify.rb0000644000004100000410000000056412557571070025644 0ustar www-datawww-datarequire 'faraday_middleware/response/mashify' module FaradayMiddleware # Public: Converts parsed response bodies to a Hashie::Rash if they were of # Hash or Array type. class Rashify < Mashify dependency do require 'rash' self.mash_class = ::Hashie::Rash end end end # deprecated alias Faraday::Response::Rashify = FaradayMiddleware::Rashify faraday_middleware-0.10.0/lib/faraday_middleware/response/caching.rb0000644000004100000410000000572712557571070025601 0ustar www-datawww-datarequire 'faraday' require 'forwardable' # fixes normalizing query strings: require 'faraday_middleware/addressable_patch' if defined? ::Addressable::URI module FaradayMiddleware # Public: Caches GET responses and pulls subsequent ones from the cache. class Caching < Faraday::Middleware attr_reader :cache # Internal: List of status codes that can be cached: # * 200 - 'OK' # * 203 - 'Non-Authoritative Information' # * 300 - 'Multiple Choices' # * 301 - 'Moved Permanently' # * 302 - 'Found' # * 404 - 'Not Found' # * 410 - 'Gone' CACHEABLE_STATUS_CODES = [200, 203, 300, 301, 302, 404, 410] extend Forwardable def_delegators :'Faraday::Utils', :parse_query, :build_query # Public: initialize the middleware. # # cache - An object that responds to read, write and fetch (default: nil). # options - An options Hash (default: {}): # :ignore_params - String name or Array names of query params # that should be ignored when forming the cache # key (default: []). # # Yields if no cache is given. The block should return a cache object. def initialize(app, cache = nil, options = {}) super(app) options, cache = cache, nil if cache.is_a? Hash and block_given? @cache = cache || yield @options = options end def call(env) if :get == env[:method] if env[:parallel_manager] # callback mode cache_on_complete(env) else # synchronous mode key = cache_key(env) unless response = cache.read(key) and response response = @app.call(env) if CACHEABLE_STATUS_CODES.include?(response.status) cache.write(key, response) end end finalize_response(response, env) end else @app.call(env) end end def cache_key(env) url = env[:url].dup if url.query && params_to_ignore.any? params = parse_query url.query params.reject! {|k,| params_to_ignore.include? k } url.query = params.any? ? build_query(params) : nil end url.normalize! url.request_uri end def params_to_ignore @params_to_ignore ||= Array(@options[:ignore_params]).map { |p| p.to_s } end def cache_on_complete(env) key = cache_key(env) if cached_response = cache.read(key) finalize_response(cached_response, env) else response = @app.call(env) if CACHEABLE_STATUS_CODES.include?(response.status) response.on_complete { cache.write(key, response) } end end end def finalize_response(response, env) response = response.dup if response.frozen? env[:response] = response unless env[:response_headers] env.update response.env # FIXME: omg hax response.instance_variable_set('@env', env) end response end end end faraday_middleware-0.10.0/lib/faraday_middleware/response/parse_xml.rb0000644000004100000410000000053012557571070026162 0ustar www-datawww-datarequire 'faraday_middleware/response_middleware' module FaradayMiddleware # Public: parses response bodies with MultiXml. class ParseXml < ResponseMiddleware dependency 'multi_xml' define_parser do |body| ::MultiXml.parse(body) end end end # deprecated alias Faraday::Response::ParseXml = FaradayMiddleware::ParseXml faraday_middleware-0.10.0/lib/faraday_middleware/gzip.rb0000644000004100000410000000323112557571070023304 0ustar www-datawww-datarequire 'faraday' module FaradayMiddleware # Middleware to automatically decompress response bodies. If the # "Accept-Encoding" header wasn't set in the request, this sets it to # "gzip,deflate" and appropriately handles the compressed response from the # server. This resembles what Ruby 1.9+ does internally in Net::HTTP#get. # # This middleware is NOT necessary when these adapters are used: # - net_http on Ruby 1.9+ # - net_http_persistent on Ruby 2.0+ # - em_http class Gzip < Faraday::Middleware dependency 'zlib' ACCEPT_ENCODING = 'Accept-Encoding'.freeze CONTENT_ENCODING = 'Content-Encoding'.freeze CONTENT_LENGTH = 'Content-Length'.freeze SUPPORTED_ENCODINGS = 'gzip,deflate'.freeze RUBY_ENCODING = '1.9'.respond_to?(:force_encoding) def call(env) env[:request_headers][ACCEPT_ENCODING] ||= SUPPORTED_ENCODINGS @app.call(env).on_complete do |response_env| case response_env[:response_headers][CONTENT_ENCODING] when 'gzip' reset_body(response_env, &method(:uncompress_gzip)) when 'deflate' reset_body(response_env, &method(:inflate)) end end end def reset_body(env) env[:body] = yield(env[:body]) env[:response_headers].delete(CONTENT_ENCODING) env[:response_headers][CONTENT_LENGTH] = env[:body].length end def uncompress_gzip(body) io = StringIO.new(body) gzip_reader = if RUBY_ENCODING Zlib::GzipReader.new(io, :encoding => 'ASCII-8BIT') else Zlib::GzipReader.new(io) end gzip_reader.read end def inflate(body) Zlib::Inflate.inflate(body) end end end faraday_middleware-0.10.0/lib/faraday_middleware/addressable_patch.rb0000644000004100000410000000115512557571070025766 0ustar www-datawww-datarequire '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 faraday_middleware-0.10.0/lib/faraday_middleware/version.rb0000644000004100000410000000013612557571070024021 0ustar www-datawww-datamodule FaradayMiddleware VERSION = "0.10.0" unless defined?(FaradayMiddleware::VERSION) end faraday_middleware-0.10.0/lib/faraday_middleware/instrumentation.rb0000644000004100000410000000147412557571070025605 0ustar www-datawww-datarequire 'faraday' module FaradayMiddleware # Public: Instruments requests using Active Support. # # Measures time spent only for synchronous requests. # # Examples # # ActiveSupport::Notifications.subscribe('request.faraday') do |name, starts, ends, _, env| # url = env[:url] # http_method = env[:method].to_s.upcase # duration = ends - starts # $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 faraday_middleware-0.10.0/lib/faraday_middleware/request/0000755000004100000410000000000012557571070023477 5ustar www-datawww-datafaraday_middleware-0.10.0/lib/faraday_middleware/request/method_override.rb0000644000004100000410000000305512557571070027206 0ustar www-datawww-datarequire 'faraday' module FaradayMiddleware # Public: Writes the original HTTP method to "X-Http-Method-Override" header # and sends the request as POST. # # This can be used to work around technical issues with making non-POST # requests, e.g. faulty HTTP client or server router. # # This header is recognized in Rack apps by default, courtesy of the # Rack::MethodOverride module. See # http://rack.rubyforge.org/doc/classes/Rack/MethodOverride.html class MethodOverride < Faraday::Middleware HEADER = "X-Http-Method-Override".freeze # Public: Initialize the middleware. # # app - the Faraday app to wrap # options - (optional) # :rewrite - Array of HTTP methods to rewrite # (default: all but GET and POST) def initialize(app, options = nil) super(app) @methods = options && options.fetch(:rewrite).map { |method| method = method.downcase if method.respond_to? :downcase method.to_sym } end def call(env) method = env[:method] rewrite_request(env, method) if rewrite_request?(method) @app.call(env) end def rewrite_request?(method) if @methods.nil? or @methods.empty? method != :get and method != :post else @methods.include? method end end # Internal: Write the original HTTP method to header, change method to POST. def rewrite_request(env, original_method) env[:request_headers][HEADER] = original_method.to_s.upcase env[:method] = :post end end end faraday_middleware-0.10.0/lib/faraday_middleware/request/oauth.rb0000644000004100000410000000522412557571070025147 0ustar www-datawww-datarequire 'faraday' require 'forwardable' module FaradayMiddleware # Public: Uses the simple_oauth library to sign requests according the # OAuth protocol. # # The options for this middleware are forwarded to SimpleOAuth::Header: # :consumer_key, :consumer_secret, :token, :token_secret. All these # parameters are optional. # # The signature is added to the "Authorization" HTTP request header. If the # value for this header already exists, it is not overriden. # # If no Content-Type header is specified, this middleware assumes that # request body parameters should be included while signing the request. # Otherwise, it only includes them if the Content-Type is # "application/x-www-form-urlencoded", as per OAuth 1.0. # # For better performance while signing requests, this middleware should be # positioned before UrlEncoded middleware on the stack, but after any other # body-encoding middleware (such as EncodeJson). class OAuth < Faraday::Middleware dependency 'simple_oauth' AUTH_HEADER = 'Authorization'.freeze CONTENT_TYPE = 'Content-Type'.freeze TYPE_URLENCODED = 'application/x-www-form-urlencoded'.freeze extend Forwardable parser_method = :parse_nested_query parser_module = ::Faraday::Utils.respond_to?(parser_method) ? 'Faraday::Utils' : 'Rack::Utils' def_delegator parser_module, parser_method def initialize(app, options) super(app) @options = options end def call(env) env[:request_headers][AUTH_HEADER] ||= oauth_header(env).to_s if sign_request?(env) @app.call(env) end def oauth_header(env) SimpleOAuth::Header.new env[:method], env[:url].to_s, signature_params(body_params(env)), oauth_options(env) end def sign_request?(env) !!env[:request].fetch(:oauth, true) end def oauth_options(env) if extra = env[:request][:oauth] and extra.is_a? Hash and !extra.empty? @options.merge extra else @options end end def body_params(env) if include_body_params?(env) if env[:body].respond_to?(:to_str) parse_nested_query env[:body] else env[:body] end end || {} end def include_body_params?(env) # see RFC 5849, section 3.4.1.3.1 for details !(type = env[:request_headers][CONTENT_TYPE]) or type == TYPE_URLENCODED end def signature_params(params) params.empty? ? params : params.reject {|k,v| v.respond_to?(:content_type) } end end end # deprecated alias Faraday::Request::OAuth = FaradayMiddleware::OAuth faraday_middleware-0.10.0/lib/faraday_middleware/request/encode_json.rb0000644000004100000410000000247512557571070026322 0ustar www-datawww-datarequire 'faraday' module FaradayMiddleware # Request middleware that encodes the body as JSON. # # Processes only requests with matching Content-type or those without a type. # If a request doesn't have a type but has a body, it sets the Content-type # to JSON MIME-type. # # Doesn't try to encode bodies that already are in string form. class EncodeJson < Faraday::Middleware CONTENT_TYPE = 'Content-Type'.freeze MIME_TYPE = 'application/json'.freeze dependency do require 'json' unless defined?(::JSON) end def call(env) match_content_type(env) do |data| env[:body] = encode data end @app.call env end def encode(data) ::JSON.dump data end def match_content_type(env) if process_request?(env) env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE yield env[:body] unless env[:body].respond_to?(:to_str) end end def process_request?(env) type = request_type(env) has_body?(env) and (type.empty? or type == MIME_TYPE) end def has_body?(env) body = env[:body] and !(body.respond_to?(:to_str) and body.empty?) end def request_type(env) type = env[:request_headers][CONTENT_TYPE].to_s type = type.split(';', 2).first if type.index(';') type end end end faraday_middleware-0.10.0/lib/faraday_middleware/request/oauth2.rb0000644000004100000410000000336112557571070025231 0ustar www-datawww-datarequire 'faraday' require 'forwardable' module FaradayMiddleware # Public: A simple middleware that adds an access token to each request. # # The token is added as both "access_token" query parameter and the # "Authorization" HTTP request header. However, an explicit "access_token" # parameter or "Authorization" header for the current request are not # overriden. # # Examples # # # configure default token: # OAuth2.new(app, 'abc123') # # # configure query parameter name: # OAuth2.new(app, 'abc123', :param_name => 'my_oauth_token') # # # default token value is optional: # OAuth2.new(app, :param_name => 'my_oauth_token') class OAuth2 < Faraday::Middleware PARAM_NAME = 'access_token'.freeze AUTH_HEADER = 'Authorization'.freeze attr_reader :param_name extend Forwardable def_delegators :'Faraday::Utils', :parse_query, :build_query def call(env) params = { param_name => @token }.update query_params(env[:url]) token = params[param_name] if token.respond_to?(:empty?) && !token.empty? env[:url].query = build_query params env[:request_headers][AUTH_HEADER] ||= %(Token token="#{token}") end @app.call env end def initialize(app, token = nil, options = {}) super(app) options, token = token, nil if token.is_a? Hash @token = token && token.to_s @param_name = options.fetch(:param_name, PARAM_NAME).to_s raise ArgumentError, ":param_name can't be blank" if @param_name.empty? end def query_params(url) if url.query.nil? or url.query.empty? {} else parse_query url.query end end end end # deprecated alias Faraday::Request::OAuth2 = FaradayMiddleware::OAuth2 faraday_middleware-0.10.0/metadata.yml0000644000004100000410000000557512557571071017755 0ustar www-datawww-data--- !ruby/object:Gem::Specification name: faraday_middleware version: !ruby/object:Gem::Version version: 0.10.0 platform: ruby authors: - Erik Michaels-Ober - Wynn Netherland autorequire: bindir: bin cert_chain: [] date: 2015-07-07 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: faraday requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: 0.7.4 - - "<" - !ruby/object:Gem::Version version: '0.10' type: :runtime prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: 0.7.4 - - "<" - !ruby/object:Gem::Version version: '0.10' - !ruby/object:Gem::Dependency name: bundler requirement: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '1.0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '1.0' description: Various middleware for Faraday email: - sferik@gmail.com - wynn.netherland@gmail.com executables: [] extensions: [] extra_rdoc_files: [] files: - CHANGELOG.md - CONTRIBUTING.md - LICENSE.md - README.md - faraday_middleware.gemspec - lib/faraday_middleware.rb - lib/faraday_middleware/addressable_patch.rb - lib/faraday_middleware/backwards_compatibility.rb - lib/faraday_middleware/gzip.rb - lib/faraday_middleware/instrumentation.rb - lib/faraday_middleware/rack_compatible.rb - lib/faraday_middleware/request/encode_json.rb - lib/faraday_middleware/request/method_override.rb - lib/faraday_middleware/request/oauth.rb - lib/faraday_middleware/request/oauth2.rb - lib/faraday_middleware/response/caching.rb - lib/faraday_middleware/response/chunked.rb - lib/faraday_middleware/response/follow_redirects.rb - lib/faraday_middleware/response/mashify.rb - lib/faraday_middleware/response/parse_dates.rb - lib/faraday_middleware/response/parse_json.rb - lib/faraday_middleware/response/parse_marshal.rb - lib/faraday_middleware/response/parse_xml.rb - lib/faraday_middleware/response/parse_yaml.rb - lib/faraday_middleware/response/rashify.rb - lib/faraday_middleware/response_middleware.rb - lib/faraday_middleware/version.rb homepage: https://github.com/lostisland/faraday_middleware licenses: - MIT metadata: {} post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: 1.3.5 requirements: [] rubyforge_project: rubygems_version: 2.2.2 signing_key: specification_version: 4 summary: Various middleware for Faraday test_files: [] faraday_middleware-0.10.0/CONTRIBUTING.md0000644000004100000410000000342512557571067017700 0ustar www-datawww-data## Contributing In the spirit of [free software][free-sw], **everyone** is encouraged to help improve this project. [free-sw]: http://www.fsf.org/licensing/essays/free-sw.html Here are some ways *you* can contribute: * by using alpha, beta, and prerelease versions * by reporting bugs * by suggesting new features * by writing or editing documentation * by writing specifications * by writing code (**no patch is too small**: fix typos, add comments, clean up inconsistent whitespace) * by refactoring code * by fixing [issues][] * by reviewing patches [issues]: https://github.com/lostisland/faraday_middleware/issues ## Submitting an Issue We use the [GitHub issue tracker][issues] to track bugs and features. Before submitting a bug report or feature request, check to make sure it hasn't already been submitted. When submitting a bug report, please include a [Gist][] that includes a stack trace and any details that may be necessary to reproduce the bug, including your gem version, Ruby version, and operating system. Ideally, a bug report should include a pull request with failing specs. [gist]: https://gist.github.com/ ## Submitting a Pull Request 1. [Fork the repository.][fork] 2. [Create a topic branch.][branch] 3. Add specs for your unimplemented feature or bug fix. 4. Run `bundle exec rake spec`. If your specs pass, return to step 3. 5. Implement your feature or bug fix. 6. Run `COVERAGE=true bundle exec rake spec`. If your specs fail, return to step 5. 7. Run `open coverage/index.html`. If your changes are not completely covered by your tests, return to step 3. 8. Add, commit, and push your changes. 9. [Submit a pull request.][pr] [fork]: http://help.github.com/fork-a-repo/ [branch]: http://learn.github.com/p/branching.html [pr]: http://help.github.com/send-pull-requests/ faraday_middleware-0.10.0/faraday_middleware.gemspec0000644000004100000410000000156212557571067022620 0ustar www-datawww-data# coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'faraday_middleware/version' Gem::Specification.new do |spec| spec.add_dependency 'faraday', ['>= 0.7.4', '< 0.10'] spec.add_development_dependency 'bundler', '~> 1.0' spec.authors = ["Erik Michaels-Ober", "Wynn Netherland"] spec.description = %q{Various middleware for Faraday} spec.email = ['sferik@gmail.com', 'wynn.netherland@gmail.com'] spec.files = %w(CHANGELOG.md CONTRIBUTING.md LICENSE.md README.md faraday_middleware.gemspec) + Dir['lib/**/*.rb'] spec.homepage = 'https://github.com/lostisland/faraday_middleware' spec.licenses = ['MIT'] spec.name = 'faraday_middleware' spec.require_paths = ['lib'] spec.required_rubygems_version = '>= 1.3.5' spec.summary = spec.description spec.version = FaradayMiddleware::VERSION end faraday_middleware-0.10.0/CHANGELOG.md0000644000004100000410000000020512557571067017251 0ustar www-datawww-data# Changelog ### 0.0.2 September 25, 2010 * Mashify now handles arrays of non-hashes ### 0.0.1 June 27, 2010 * MultiJSON * Mashifyfaraday_middleware-0.10.0/README.md0000644000004100000410000000271212557571067016724 0ustar www-datawww-dataFaraday Middleware ================== A collection of useful [Faraday][] middleware. [See the documentation][docs]. gem install faraday_middleware Dependencies ------------ Some dependent libraries are needed only when using specific middleware: * FaradayMiddleware::EncodeJson & FaradayMiddleware::ParseJson: "json" for ruby older than 1.9 * FaradayMiddleware::ParseXml: "multi_xml" * FaradayMiddleware::OAuth: "simple_oauth" * FaradayMiddleware::Mashify: "hashie" * FaradayMiddleware::Rashify: "rash" * FaradayMiddleware::Instrumentation: "activesupport" Examples -------- ``` rb require 'faraday_middleware' ## in Faraday 0.8 or above: connection = Faraday.new 'http://example.com/api' do |conn| conn.request :oauth2, 'TOKEN' conn.request :json conn.response :xml, :content_type => /\bxml$/ conn.response :json, :content_type => /\bjson$/ conn.use :instrumentation conn.adapter Faraday.default_adapter end ## with Faraday 0.7: connection = Faraday.new 'http://example.com/api' do |builder| builder.use FaradayMiddleware::OAuth2, 'TOKEN' builder.use FaradayMiddleware::EncodeJson builder.use FaradayMiddleware::ParseXml, :content_type => /\bxml$/ builder.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/ builder.use FaradayMiddleware::Instrumentation builder.adapter Faraday.default_adapter end ``` [faraday]: https://github.com/lostisland/faraday#readme [docs]: https://github.com/lostisland/faraday_middleware/wiki