Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'faraday-0.6'

  • Loading branch information...
commit 2b29c2109d2ca95a699ebe3822b98091a96256d8 2 parents 9539d9a + 377bdda
Erik Michaels-Ober authored
22 lib/faraday/oauth.rb
View
@@ -1,22 +0,0 @@
-require 'faraday'
-require 'simple_oauth'
-
-# @private
-module Faraday
- # @private
- class Request::OAuth < Faraday::Middleware
- def call(env)
- params = env[:body].is_a?(Hash) ? env[:body] : {}
- signature_params = params.reject{|k,v| v.respond_to?(:content_type) }
- header = SimpleOAuth::Header.new(env[:method], env[:url], signature_params, @options)
-
- env[:request_headers]['Authorization'] = header.to_s
-
- @app.call(env)
- end
-
- def initialize(app, options)
- @app, @options = app, options
- end
- end
-end
52 lib/faraday/raise_http_4xx.rb
View
@@ -1,52 +0,0 @@
-require 'faraday'
-
-# @private
-module Faraday
- # @private
- class Response::RaiseHttp4xx < Response::Middleware
- def self.register_on_complete(env)
- env[:response].on_complete do |response|
- case response[:status].to_i
- when 400
- raise Twitter::BadRequest.new(error_message(response), response[:response_headers])
- when 401
- raise Twitter::Unauthorized.new(error_message(response), response[:response_headers])
- when 403
- raise Twitter::Forbidden.new(error_message(response), response[:response_headers])
- when 404
- raise Twitter::NotFound.new(error_message(response), response[:response_headers])
- when 406
- raise Twitter::NotAcceptable.new(error_message(response), response[:response_headers])
- when 420
- raise Twitter::EnhanceYourCalm.new(error_message(response), response[:response_headers])
- end
- end
- end
-
- def initialize(app)
- super
- @parser = nil
- end
-
- private
-
- def self.error_message(response)
- "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{error_body(response[:body])}"
- end
-
- def self.error_body(body)
- if body.nil?
- nil
- elsif body['error']
- ": #{body['error']}"
- elsif body['errors']
- first = body['errors'].to_a.first
- if first.kind_of? Hash
- ": #{first['message'].chomp}"
- else
- ": #{first.chomp}"
- end
- end
- end
- end
-end
31 lib/faraday/raise_http_5xx.rb
View
@@ -1,31 +0,0 @@
-require 'faraday'
-
-# @private
-module Faraday
- # @private
- class Response::RaiseHttp5xx < Response::Middleware
- def self.register_on_complete(env)
- env[:response].on_complete do |response|
- case response[:status].to_i
- when 500
- raise Twitter::InternalServerError.new(error_message(response, "Something is technically wrong."), response[:response_headers])
- when 502
- raise Twitter::BadGateway.new(error_message(response, "Twitter is down or being upgraded."), response[:response_headers])
- when 503
- raise Twitter::ServiceUnavailable.new(error_message(response, "(__-){ Twitter is over capacity."), response[:response_headers])
- end
- end
- end
-
- def initialize(app)
- super
- @parser = nil
- end
-
- private
-
- def self.error_message(response, body=nil)
- "#{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."
- end
- end
-end
2  lib/faraday/multipart.rb → lib/faraday/request/multipart_with_file.rb
View
@@ -3,7 +3,7 @@
# @private
module Faraday
# @private
- class Request::Multipart < Faraday::Middleware
+ class Request::MultipartWithFile < Faraday::Middleware
def call(env)
if env[:body].is_a?(Hash)
env[:body].each do |key, value|
45 lib/faraday/response/raise_http_4xx.rb
View
@@ -0,0 +1,45 @@
+require 'faraday'
+
+# @private
+module Faraday
+ # @private
+ class Response::RaiseHttp4xx < Response::Middleware
+ def on_complete(env)
+ case env[:status].to_i
+ when 400
+ raise Twitter::BadRequest.new(error_message(env), env[:response_headers])
+ when 401
+ raise Twitter::Unauthorized.new(error_message(env), env[:response_headers])
+ when 403
+ raise Twitter::Forbidden.new(error_message(env), env[:response_headers])
+ when 404
+ raise Twitter::NotFound.new(error_message(env), env[:response_headers])
+ when 406
+ raise Twitter::NotAcceptable.new(error_message(env), env[:response_headers])
+ when 420
+ raise Twitter::EnhanceYourCalm.new(error_message(env), env[:response_headers])
+ end
+ end
+
+ private
+
+ def error_message(env)
+ "#{env[:method].to_s.upcase} #{env[:url].to_s}: #{env[:status]}#{error_body(env[:body])}"
+ end
+
+ def error_body(body)
+ if body.nil?
+ nil
+ elsif body['error']
+ ": #{body['error']}"
+ elsif body['errors']
+ first = body['errors'].to_a.first
+ if first.kind_of? Hash
+ ": #{first['message'].chomp}"
+ else
+ ": #{first.chomp}"
+ end
+ end
+ end
+ end
+end
24 lib/faraday/response/raise_http_5xx.rb
View
@@ -0,0 +1,24 @@
+require 'faraday'
+
+# @private
+module Faraday
+ # @private
+ class Response::RaiseHttp5xx < Response::Middleware
+ def on_complete(env)
+ case env[:status].to_i
+ when 500
+ raise Twitter::InternalServerError.new(error_message(env, "Something is technically wrong."), env[:response_headers])
+ when 502
+ raise Twitter::BadGateway.new(error_message(env, "Twitter is down or being upgraded."), env[:response_headers])
+ when 503
+ raise Twitter::ServiceUnavailable.new(error_message(env, "(__-){ Twitter is over capacity."), env[:response_headers])
+ end
+ end
+
+ private
+
+ def error_message(env, body=nil)
+ "#{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."
+ end
+ end
+end
29 lib/twitter/connection.rb
View
@@ -1,8 +1,7 @@
require 'faraday_middleware'
-require 'faraday/multipart'
-require 'faraday/oauth'
-require 'faraday/raise_http_4xx'
-require 'faraday/raise_http_5xx'
+require 'faraday/request/multipart_with_file'
+require 'faraday/response/raise_http_4xx'
+require 'faraday/response/raise_http_5xx'
module Twitter
# @private
@@ -17,19 +16,23 @@ def connection(raw=false)
:url => api_endpoint,
}
- Faraday::Connection.new(options) do |connection|
- connection.use Faraday::Request::Multipart
- connection.use Faraday::Request::OAuth, authentication if authenticated?
- connection.adapter(adapter)
- connection.use Faraday::Response::RaiseHttp5xx
+ Faraday.new(options) do |builder|
+ builder.use Faraday::Request::MultipartWithFile
+ builder.use Faraday::Request::Multipart
+ builder.use Faraday::Request::OAuth, authentication if authenticated?
+ builder.use Faraday::Request::UrlEncoded
+ builder.use Faraday::Response::RaiseHttp4xx
+ builder.use Faraday::Response::Mashify unless raw
unless raw
case format.to_s.downcase
- when 'json' then connection.use Faraday::Response::ParseJson
- when 'xml' then connection.use Faraday::Response::ParseXml
+ when 'json'
+ builder.use Faraday::Response::ParseJson
+ when 'xml'
+ builder.use Faraday::Response::ParseXml
end
end
- connection.use Faraday::Response::RaiseHttp4xx
- connection.use Faraday::Response::Mashify unless raw
+ builder.use Faraday::Response::RaiseHttp5xx
+ builder.adapter(adapter)
end
end
end
1  lib/twitter/error.rb
View
@@ -4,6 +4,7 @@ class Error < StandardError
attr_reader :http_headers
def initialize(message, http_headers)
+ http_headers ||= {}
@http_headers = Hash[http_headers]
super message
end
4 twitter.gemspec
View
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
s.add_development_dependency('yard', '~> 0.6')
s.add_development_dependency('ZenTest', '~> 4.5')
s.add_runtime_dependency('hashie', '~> 1.0.0')
- s.add_runtime_dependency('faraday', '~> 0.5.4')
- s.add_runtime_dependency('faraday_middleware', '~> 0.3.2')
+ s.add_runtime_dependency('faraday', '~> 0.6.0')
+ s.add_runtime_dependency('faraday_middleware', '~> 0.6.0')
s.add_runtime_dependency('jruby-openssl', '~> 0.7.2') if RUBY_PLATFORM == 'java'
s.add_runtime_dependency('multi_json', '~> 0.0.5')
s.add_runtime_dependency('multi_xml', '~> 0.2.0')
Please sign in to comment.
Something went wrong with that request. Please try again.