Permalink
Browse files

Namespace errors and middleware

  • Loading branch information...
1 parent 384737f commit 1245742f030415ebf5f1054e85fe93bca85cddfb @sferik committed Oct 3, 2011
Showing with 345 additions and 291 deletions.
  1. +0 −18 lib/faraday/request/gateway.rb
  2. +0 −36 lib/faraday/request/multipart_with_file.rb
  3. +0 −18 lib/faraday/request/phoenix.rb
  4. +0 −24 lib/faraday/request/twitter_oauth.rb
  5. +0 −24 lib/faraday/response/mashify.rb
  6. +0 −20 lib/faraday/response/parse_json.rb
  7. +0 −45 lib/faraday/response/raise_http_4xx.rb
  8. +0 −24 lib/faraday/response/raise_http_5xx.rb
  9. +1 −3 lib/twitter.rb
  10. +1 −3 lib/twitter/api.rb
  11. +1 −2 lib/twitter/authentication.rb
  12. +2 −0 lib/twitter/client.rb
  13. +3 −1 lib/twitter/client/block.rb
  14. +4 −1 lib/twitter/client/list_members.rb
  15. +4 −1 lib/twitter/client/list_subscribers.rb
  16. +3 −1 lib/twitter/client/user.rb
  17. +1 −3 lib/twitter/client/utils.rb
  18. +0 −1 lib/twitter/configuration.rb
  19. +17 −18 lib/twitter/connection.rb
  20. +0 −34 lib/twitter/error.rb
  21. +7 −0 lib/twitter/error/bad_gateway.rb
  22. +7 −0 lib/twitter/error/bad_request.rb
  23. +13 −0 lib/twitter/error/enhance_your_calm.rb
  24. +7 −0 lib/twitter/error/forbidden.rb
  25. +7 −0 lib/twitter/error/internal_server_error.rb
  26. +7 −0 lib/twitter/error/not_acceptable.rb
  27. +7 −0 lib/twitter/error/not_found.rb
  28. +7 −0 lib/twitter/error/service_unavailable.rb
  29. +7 −0 lib/twitter/error/unauthorized.rb
  30. +1 −1 lib/twitter/request.rb
  31. +18 −0 lib/twitter/request/gateway.rb
  32. +36 −0 lib/twitter/request/multipart_with_file.rb
  33. +25 −0 lib/twitter/request/oauth.rb
  34. +20 −0 lib/twitter/request/phoenix.rb
  35. +26 −0 lib/twitter/response/mashify.rb
  36. +22 −0 lib/twitter/response/parse_json.rb
  37. +51 −0 lib/twitter/response/raise_http_4xx.rb
  38. +27 −0 lib/twitter/response/raise_http_5xx.rb
  39. +1 −1 lib/twitter/search.rb
  40. +1 −1 spec/faraday/request_spec.rb
  41. +11 −11 spec/faraday/response_spec.rb
@@ -1,18 +0,0 @@
-require 'faraday'
-
-# @private
-module Faraday
- # @private
- class Request::Gateway < Faraday::Middleware
- def call(env)
- url = env[:url].dup
- url.host = @gateway
- env[:url] = url
- @app.call(env)
- end
-
- def initialize(app, gateway)
- @app, @gateway = app, gateway
- end
- end
-end
@@ -1,36 +0,0 @@
-require 'faraday'
-
-# @private
-module Faraday
- # @private
- class Request::MultipartWithFile < Faraday::Middleware
- def call(env)
- if env[:body].is_a?(Hash)
- env[:body].each do |key, value|
- if value.is_a?(File)
- env[:body][key] = Faraday::UploadIO.new(value, mime_type(value.path), value.path)
- elsif value.is_a?(Hash) && (value['io'].is_a?(IO) || value['io'].is_a?(StringIO))
- env[:body][key] = Faraday::UploadIO.new(value['io'], mime_type('.'+value['type']), '')
- end
- end
- end
-
- @app.call(env)
- end
-
- private
-
- def mime_type(path)
- case path
- when /\.jpe?g/i
- 'image/jpeg'
- when /\.gif$/i
- 'image/gif'
- when /\.png$/i
- 'image/png'
- else
- 'application/octet-stream'
- end
- end
- end
-end
@@ -1,18 +0,0 @@
-require 'faraday'
-
-module Faraday
- class Request::Phoenix < Faraday::Middleware
- def call(env)
- # Not sure what what the X-Phx (Phoenix?) header is for but it's
- # required to access certain undocumented resources
- # e.g. GET urls/resolve
- env[:request_headers]['X-Phx'] = 'true'
-
- @app.call(env)
- end
-
- def initialize(app)
- @app = app
- end
- end
-end
@@ -1,24 +0,0 @@
-require 'faraday'
-
-module Faraday
- class Request::TwitterOAuth < Faraday::Middleware
- dependency 'simple_oauth'
-
- def call(env)
- params = env[:body] || {}
- signature_params = params
-
- params.map{ |k,v| signature_params = {} if 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
@@ -1,24 +0,0 @@
-require 'faraday'
-
-module Faraday
- class Response::Mashify < Response::Middleware
- dependency 'hashie/mash'
-
- class << self
- attr_accessor :mash_class
- end
-
- self.mash_class = ::Hashie::Mash
-
- def parse(body)
- case body
- when Hash
- self.class.mash_class.new(body)
- when Array
- body.map { |item| item.is_a?(Hash) ? self.class.mash_class.new(item) : item }
- else
- body
- end
- end
- end
-end
@@ -1,20 +0,0 @@
-require 'faraday'
-
-module Faraday
- class Response::ParseJson < Response::Middleware
- dependency 'multi_json'
-
- def parse(body)
- case body
- when ''
- nil
- when 'true'
- true
- when 'false'
- false
- else
- ::MultiJson.decode(body)
- end
- end
- end
-end
@@ -1,45 +0,0 @@
-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 = Array(body['errors']).first
- if first.kind_of? Hash
- ": #{first['message'].chomp}"
- else
- ": #{first.chomp}"
- end
- end
- end
- end
-end
@@ -1,24 +0,0 @@
-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
View
@@ -1,7 +1,5 @@
-require 'twitter/api'
require 'twitter/client'
require 'twitter/configuration'
-require 'twitter/error'
require 'twitter/search'
module Twitter
@@ -20,7 +18,7 @@ def method_missing(method, *args, &block)
new.send(method, *args, &block)
end
- def respond_to?(method, include_private = false)
+ def respond_to?(method, include_private=false)
new.respond_to?(method, include_private) || super(method, include_private)
end
end
View
@@ -4,13 +4,11 @@
require 'twitter/request'
module Twitter
- # @private
class API
+ include Authentication
include Connection
include Request
- include Authentication
- # @private
attr_accessor *Configuration::VALID_OPTIONS_KEYS
# Creates a new API
@@ -1,7 +1,6 @@
module Twitter
- # @private
module Authentication
- private
+ private
# Authentication hash
#
View
@@ -1,3 +1,5 @@
+require 'twitter/api'
+
module Twitter
# Wrapper for the Twitter REST API
#
@@ -1,3 +1,5 @@
+require 'twitter/error/not_found'
+
module Twitter
class Client
# Defines methods related to blocking and unblocking users
@@ -53,7 +55,7 @@ def block?(user, options={})
merge_user_into_options!(user, options)
get("/1/blocks/exists.json", options, :raw => true)
true
- rescue Twitter::NotFound
+ rescue Twitter::Error::NotFound
false
end
@@ -1,3 +1,6 @@
+require 'twitter/error/forbidden'
+require 'twitter/error/not_found'
+
module Twitter
class Client
# Defines methods related to list members
@@ -183,7 +186,7 @@ def list_member?(*args)
merge_user_into_options!(user_to_check, options)
get("/1/lists/members/show.json", options, :raw => true)
true
- rescue Twitter::NotFound, Twitter::Forbidden
+ rescue Twitter::Error::NotFound, Twitter::Error::Forbidden
false
end
end
@@ -1,3 +1,6 @@
+require 'twitter/error/forbidden'
+require 'twitter/error/not_found'
+
module Twitter
class Client
# Defines methods related to list subscribers
@@ -136,7 +139,7 @@ def list_subscriber?(*args)
merge_user_into_options!(user_to_check, options)
get("/1/lists/subscribers/show.json", options, :raw => true)
true
- rescue Twitter::NotFound, Twitter::Forbidden
+ rescue Twitter::Error::NotFound, Twitter::Error::Forbidden
false
end
end
@@ -1,3 +1,5 @@
+require 'twitter/error/not_found'
+
module Twitter
class Client
# Defines methods related to users
@@ -35,7 +37,7 @@ def user?(user, options={})
merge_user_into_options!(user, options)
get("/1/users/show.json", options, :raw => true)
true
- rescue Twitter::NotFound
+ rescue Twitter::Error::NotFound
false
end
@@ -1,9 +1,7 @@
-# -*- encoding: utf-8 -*-
module Twitter
class Client
- # @private
module Utils
- private
+ private
# Returns the configured screen name or the screen name of the authenticated user
#
@@ -61,7 +61,6 @@ module Configuration
DEFAULT_FARADAY_OPTIONS = {}.freeze
- # @private
attr_accessor *VALID_OPTIONS_KEYS
# When this module is extended, set all configuration options to their default values
View
@@ -1,16 +1,15 @@
-require 'faraday/request/gateway'
-require 'faraday/request/multipart_with_file'
-require 'faraday/request/phoenix'
-require 'faraday/request/twitter_oauth'
-require 'faraday/response/mashify'
-require 'faraday/response/parse_json'
-require 'faraday/response/raise_http_4xx'
-require 'faraday/response/raise_http_5xx'
+require 'twitter/request/gateway'
+require 'twitter/request/multipart_with_file'
+require 'twitter/request/phoenix'
+require 'twitter/request/oauth'
+require 'twitter/response/mashify'
+require 'twitter/response/parse_json'
+require 'twitter/response/raise_http_4xx'
+require 'twitter/response/raise_http_5xx'
module Twitter
- # @private
module Connection
- private
+ private
def connection(options={})
merged_options = faraday_options.merge({
@@ -24,18 +23,18 @@ def connection(options={})
})
Faraday.new(merged_options) do |builder|
- builder.use Faraday::Request::Phoenix if options[:phoenix]
- builder.use Faraday::Request::MultipartWithFile
- builder.use Faraday::Request::TwitterOAuth, authentication if authenticated?
+ builder.use Twitter::Request::Phoenix if options[:phoenix]
+ builder.use Twitter::Request::MultipartWithFile
+ builder.use Twitter::Request::TwitterOAuth, authentication if authenticated?
builder.use Faraday::Request::Multipart
builder.use Faraday::Request::UrlEncoded
- builder.use Faraday::Request::Gateway, gateway if gateway
- builder.use Faraday::Response::RaiseHttp4xx
+ builder.use Twitter::Request::Gateway, gateway if gateway
+ builder.use Twitter::Response::RaiseHttp4xx
unless options[:raw]
- builder.use Faraday::Response::Mashify
- builder.use Faraday::Response::ParseJson
+ builder.use Twitter::Response::Mashify
+ builder.use Twitter::Response::ParseJson
end
- builder.use Faraday::Response::RaiseHttp5xx
+ builder.use Twitter::Response::RaiseHttp5xx
builder.adapter(adapter)
end
end
Oops, something went wrong.

0 comments on commit 1245742

Please sign in to comment.