Skip to content
This repository
Browse code

Merge branch 'faraday-0.6'

  • Loading branch information...
commit 2b29c2109d2ca95a699ebe3822b98091a96256d8 2 parents 9539d9a + 377bdda
Erik Michaels-Ober authored
22 lib/faraday/oauth.rb
... ... @@ -1,22 +0,0 @@
1   -require 'faraday'
2   -require 'simple_oauth'
3   -
4   -# @private
5   -module Faraday
6   - # @private
7   - class Request::OAuth < Faraday::Middleware
8   - def call(env)
9   - params = env[:body].is_a?(Hash) ? env[:body] : {}
10   - signature_params = params.reject{|k,v| v.respond_to?(:content_type) }
11   - header = SimpleOAuth::Header.new(env[:method], env[:url], signature_params, @options)
12   -
13   - env[:request_headers]['Authorization'] = header.to_s
14   -
15   - @app.call(env)
16   - end
17   -
18   - def initialize(app, options)
19   - @app, @options = app, options
20   - end
21   - end
22   -end
52 lib/faraday/raise_http_4xx.rb
... ... @@ -1,52 +0,0 @@
1   -require 'faraday'
2   -
3   -# @private
4   -module Faraday
5   - # @private
6   - class Response::RaiseHttp4xx < Response::Middleware
7   - def self.register_on_complete(env)
8   - env[:response].on_complete do |response|
9   - case response[:status].to_i
10   - when 400
11   - raise Twitter::BadRequest.new(error_message(response), response[:response_headers])
12   - when 401
13   - raise Twitter::Unauthorized.new(error_message(response), response[:response_headers])
14   - when 403
15   - raise Twitter::Forbidden.new(error_message(response), response[:response_headers])
16   - when 404
17   - raise Twitter::NotFound.new(error_message(response), response[:response_headers])
18   - when 406
19   - raise Twitter::NotAcceptable.new(error_message(response), response[:response_headers])
20   - when 420
21   - raise Twitter::EnhanceYourCalm.new(error_message(response), response[:response_headers])
22   - end
23   - end
24   - end
25   -
26   - def initialize(app)
27   - super
28   - @parser = nil
29   - end
30   -
31   - private
32   -
33   - def self.error_message(response)
34   - "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{error_body(response[:body])}"
35   - end
36   -
37   - def self.error_body(body)
38   - if body.nil?
39   - nil
40   - elsif body['error']
41   - ": #{body['error']}"
42   - elsif body['errors']
43   - first = body['errors'].to_a.first
44   - if first.kind_of? Hash
45   - ": #{first['message'].chomp}"
46   - else
47   - ": #{first.chomp}"
48   - end
49   - end
50   - end
51   - end
52   -end
31 lib/faraday/raise_http_5xx.rb
... ... @@ -1,31 +0,0 @@
1   -require 'faraday'
2   -
3   -# @private
4   -module Faraday
5   - # @private
6   - class Response::RaiseHttp5xx < Response::Middleware
7   - def self.register_on_complete(env)
8   - env[:response].on_complete do |response|
9   - case response[:status].to_i
10   - when 500
11   - raise Twitter::InternalServerError.new(error_message(response, "Something is technically wrong."), response[:response_headers])
12   - when 502
13   - raise Twitter::BadGateway.new(error_message(response, "Twitter is down or being upgraded."), response[:response_headers])
14   - when 503
15   - raise Twitter::ServiceUnavailable.new(error_message(response, "(__-){ Twitter is over capacity."), response[:response_headers])
16   - end
17   - end
18   - end
19   -
20   - def initialize(app)
21   - super
22   - @parser = nil
23   - end
24   -
25   - private
26   -
27   - def self.error_message(response, body=nil)
28   - "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')} Check http://status.twitter.com/ for updates on the status of the Twitter service."
29   - end
30   - end
31   -end
2  lib/faraday/multipart.rb → lib/faraday/request/multipart_with_file.rb
@@ -3,7 +3,7 @@
3 3 # @private
4 4 module Faraday
5 5 # @private
6   - class Request::Multipart < Faraday::Middleware
  6 + class Request::MultipartWithFile < Faraday::Middleware
7 7 def call(env)
8 8 if env[:body].is_a?(Hash)
9 9 env[:body].each do |key, value|
45 lib/faraday/response/raise_http_4xx.rb
... ... @@ -0,0 +1,45 @@
  1 +require 'faraday'
  2 +
  3 +# @private
  4 +module Faraday
  5 + # @private
  6 + class Response::RaiseHttp4xx < Response::Middleware
  7 + def on_complete(env)
  8 + case env[:status].to_i
  9 + when 400
  10 + raise Twitter::BadRequest.new(error_message(env), env[:response_headers])
  11 + when 401
  12 + raise Twitter::Unauthorized.new(error_message(env), env[:response_headers])
  13 + when 403
  14 + raise Twitter::Forbidden.new(error_message(env), env[:response_headers])
  15 + when 404
  16 + raise Twitter::NotFound.new(error_message(env), env[:response_headers])
  17 + when 406
  18 + raise Twitter::NotAcceptable.new(error_message(env), env[:response_headers])
  19 + when 420
  20 + raise Twitter::EnhanceYourCalm.new(error_message(env), env[:response_headers])
  21 + end
  22 + end
  23 +
  24 + private
  25 +
  26 + def error_message(env)
  27 + "#{env[:method].to_s.upcase} #{env[:url].to_s}: #{env[:status]}#{error_body(env[:body])}"
  28 + end
  29 +
  30 + def error_body(body)
  31 + if body.nil?
  32 + nil
  33 + elsif body['error']
  34 + ": #{body['error']}"
  35 + elsif body['errors']
  36 + first = body['errors'].to_a.first
  37 + if first.kind_of? Hash
  38 + ": #{first['message'].chomp}"
  39 + else
  40 + ": #{first.chomp}"
  41 + end
  42 + end
  43 + end
  44 + end
  45 +end
24 lib/faraday/response/raise_http_5xx.rb
... ... @@ -0,0 +1,24 @@
  1 +require 'faraday'
  2 +
  3 +# @private
  4 +module Faraday
  5 + # @private
  6 + class Response::RaiseHttp5xx < Response::Middleware
  7 + def on_complete(env)
  8 + case env[:status].to_i
  9 + when 500
  10 + raise Twitter::InternalServerError.new(error_message(env, "Something is technically wrong."), env[:response_headers])
  11 + when 502
  12 + raise Twitter::BadGateway.new(error_message(env, "Twitter is down or being upgraded."), env[:response_headers])
  13 + when 503
  14 + raise Twitter::ServiceUnavailable.new(error_message(env, "(__-){ Twitter is over capacity."), env[:response_headers])
  15 + end
  16 + end
  17 +
  18 + private
  19 +
  20 + def error_message(env, body=nil)
  21 + "#{env[:method].to_s.upcase} #{env[:url].to_s}: #{[env[:status].to_s + ':', body].compact.join(' ')} Check http://status.twitter.com/ for updates on the status of the Twitter service."
  22 + end
  23 + end
  24 +end
29 lib/twitter/connection.rb
... ... @@ -1,8 +1,7 @@
1 1 require 'faraday_middleware'
2   -require 'faraday/multipart'
3   -require 'faraday/oauth'
4   -require 'faraday/raise_http_4xx'
5   -require 'faraday/raise_http_5xx'
  2 +require 'faraday/request/multipart_with_file'
  3 +require 'faraday/response/raise_http_4xx'
  4 +require 'faraday/response/raise_http_5xx'
6 5
7 6 module Twitter
8 7 # @private
@@ -17,19 +16,23 @@ def connection(raw=false)
17 16 :url => api_endpoint,
18 17 }
19 18
20   - Faraday::Connection.new(options) do |connection|
21   - connection.use Faraday::Request::Multipart
22   - connection.use Faraday::Request::OAuth, authentication if authenticated?
23   - connection.adapter(adapter)
24   - connection.use Faraday::Response::RaiseHttp5xx
  19 + Faraday.new(options) do |builder|
  20 + builder.use Faraday::Request::MultipartWithFile
  21 + builder.use Faraday::Request::Multipart
  22 + builder.use Faraday::Request::OAuth, authentication if authenticated?
  23 + builder.use Faraday::Request::UrlEncoded
  24 + builder.use Faraday::Response::RaiseHttp4xx
  25 + builder.use Faraday::Response::Mashify unless raw
25 26 unless raw
26 27 case format.to_s.downcase
27   - when 'json' then connection.use Faraday::Response::ParseJson
28   - when 'xml' then connection.use Faraday::Response::ParseXml
  28 + when 'json'
  29 + builder.use Faraday::Response::ParseJson
  30 + when 'xml'
  31 + builder.use Faraday::Response::ParseXml
29 32 end
30 33 end
31   - connection.use Faraday::Response::RaiseHttp4xx
32   - connection.use Faraday::Response::Mashify unless raw
  34 + builder.use Faraday::Response::RaiseHttp5xx
  35 + builder.adapter(adapter)
33 36 end
34 37 end
35 38 end
1  lib/twitter/error.rb
@@ -4,6 +4,7 @@ class Error < StandardError
4 4 attr_reader :http_headers
5 5
6 6 def initialize(message, http_headers)
  7 + http_headers ||= {}
7 8 @http_headers = Hash[http_headers]
8 9 super message
9 10 end
4 twitter.gemspec
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
12 12 s.add_development_dependency('yard', '~> 0.6')
13 13 s.add_development_dependency('ZenTest', '~> 4.5')
14 14 s.add_runtime_dependency('hashie', '~> 1.0.0')
15   - s.add_runtime_dependency('faraday', '~> 0.5.4')
16   - s.add_runtime_dependency('faraday_middleware', '~> 0.3.2')
  15 + s.add_runtime_dependency('faraday', '~> 0.6.0')
  16 + s.add_runtime_dependency('faraday_middleware', '~> 0.6.0')
17 17 s.add_runtime_dependency('jruby-openssl', '~> 0.7.2') if RUBY_PLATFORM == 'java'
18 18 s.add_runtime_dependency('multi_json', '~> 0.0.5')
19 19 s.add_runtime_dependency('multi_xml', '~> 0.2.0')

0 comments on commit 2b29c21

Please sign in to comment.
Something went wrong with that request. Please try again.