Skip to content

Commit

Permalink
Merge branch 'faraday-0.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Apr 6, 2011
2 parents 9539d9a + 377bdda commit 2b29c21
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 121 deletions.
22 changes: 0 additions & 22 deletions lib/faraday/oauth.rb

This file was deleted.

52 changes: 0 additions & 52 deletions lib/faraday/raise_http_4xx.rb

This file was deleted.

31 changes: 0 additions & 31 deletions lib/faraday/raise_http_5xx.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
45 changes: 45 additions & 0 deletions lib/faraday/response/raise_http_4xx.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 24 additions & 0 deletions lib/faraday/response/raise_http_5xx.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 13 deletions lib/twitter/connection.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/twitter/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions twitter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 2b29c21

Please sign in to comment.