From b84aee3eb359b90692c7149f22caa2934b47a59f Mon Sep 17 00:00:00 2001 From: Peter Bakkum Date: Thu, 9 Feb 2012 12:17:10 -0500 Subject: [PATCH] updated faraday version and fixed related issues --- instagram.gemspec | 6 ++-- lib/faraday/oauth2.rb | 7 +++-- lib/faraday/raise_http_4xx.rb | 37 ---------------------- lib/faraday/raise_http_5xx.rb | 29 ----------------- lib/faraday/raise_http_exception.rb | 45 +++++++++++++++++++++++++++ lib/instagram/client/subscriptions.rb | 1 + lib/instagram/connection.rb | 8 ++--- spec/instagram/client_spec.rb | 2 +- spec/instagram_spec.rb | 5 ++- 9 files changed, 61 insertions(+), 79 deletions(-) delete mode 100644 lib/faraday/raise_http_4xx.rb delete mode 100644 lib/faraday/raise_http_5xx.rb create mode 100644 lib/faraday/raise_http_exception.rb diff --git a/instagram.gemspec b/instagram.gemspec index 272568ba..d14755dc 100644 --- a/instagram.gemspec +++ b/instagram.gemspec @@ -5,10 +5,10 @@ Gem::Specification.new do |s| s.add_development_dependency('rspec', '~> 2.4') s.add_development_dependency('webmock', '~> 1.6') s.add_development_dependency('bluecloth', '~> 2.0.11') - s.add_runtime_dependency('faraday', '~> 0.5.4') - s.add_runtime_dependency('faraday_middleware', '~> 0.3.1') + s.add_runtime_dependency('faraday', '~> 0.7') + s.add_runtime_dependency('faraday_middleware', '~> 0.8') s.add_runtime_dependency('multi_json', '~> 1.0.3') - s.add_runtime_dependency('hashie', '>= 0.4.0') + s.add_runtime_dependency('hashie', '>= 1.2.0') s.authors = ["Shayne Sweeney"] s.description = %q{A Ruby wrapper for the Instagram REST and Search APIs} s.post_install_message =< @access_token) env[:request_headers] = env[:request_headers].merge('Authorization' => "Token token=\"#{@access_token}\"") @@ -24,6 +25,8 @@ def call(env) end end + env[:url].query_values = nil if env[:url].query_values == {} + @app.call env end diff --git a/lib/faraday/raise_http_4xx.rb b/lib/faraday/raise_http_4xx.rb deleted file mode 100644 index 32abb6b1..00000000 --- a/lib/faraday/raise_http_4xx.rb +++ /dev/null @@ -1,37 +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 Instagram::BadRequest, error_message(response) - when 404 - raise Instagram::NotFound, error_message(response) - 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['meta'] and body['meta']['error_message'] and not body['meta']['error_message'].empty? - ": #{body['meta']['error_message']}" - end - end - end -end diff --git a/lib/faraday/raise_http_5xx.rb b/lib/faraday/raise_http_5xx.rb deleted file mode 100644 index 6a9896de..00000000 --- a/lib/faraday/raise_http_5xx.rb +++ /dev/null @@ -1,29 +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 Instagram::InternalServerError, error_message(response, "Something is technically wrong.") - when 503 - raise Instagram::ServiceUnavailable, error_message(response, "Instagram is rate limiting your requests.") - 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(' ')}" - end - end -end diff --git a/lib/faraday/raise_http_exception.rb b/lib/faraday/raise_http_exception.rb new file mode 100644 index 00000000..98948df1 --- /dev/null +++ b/lib/faraday/raise_http_exception.rb @@ -0,0 +1,45 @@ +require 'faraday' + +# @private +module FaradayMiddleware + # @private + class RaiseHttpException < Faraday::Middleware + def call(env) + @app.call(env).on_complete do |response| + case response[:status].to_i + when 400 + raise Instagram::BadRequest, error_message_400(response) + when 404 + raise Instagram::NotFound, error_message_400(response) + when 500 + raise Instagram::InternalServerError, error_message_500(response, "Something is technically wrong.") + when 503 + raise Instagram::ServiceUnavailable, error_message_500(response, "Instagram is rate limiting your requests.") + end + end + end + + def initialize(app) + super app + @parser = nil + end + + private + + def error_message_400(response) + "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{error_body(response[:body])}" + end + + def error_body(body) + if body.nil? + nil + elsif body['meta'] and body['meta']['error_message'] and not body['meta']['error_message'].empty? + ": #{body['meta']['error_message']}" + end + end + + def error_message_500(response, body=nil) + "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')}" + end + end +end diff --git a/lib/instagram/client/subscriptions.rb b/lib/instagram/client/subscriptions.rb index 0a173a56..35575eee 100644 --- a/lib/instagram/client/subscriptions.rb +++ b/lib/instagram/client/subscriptions.rb @@ -1,4 +1,5 @@ require 'openssl' +require 'multi_json' module Instagram class Client diff --git a/lib/instagram/connection.rb b/lib/instagram/connection.rb index 119ef47b..03cfd580 100644 --- a/lib/instagram/connection.rb +++ b/lib/instagram/connection.rb @@ -15,16 +15,16 @@ def connection(raw=false) } Faraday::Connection.new(options) do |connection| - connection.use Faraday::Request::OAuth2, client_id, access_token + connection.use FaradayMiddleware::OAuth2, client_id, access_token + connection.use Faraday::Request::UrlEncoded connection.adapter(adapter) - connection.use Faraday::Response::RaiseHttp5xx + connection.use FaradayMiddleware::Mashify unless raw unless raw case format.to_s.downcase when 'json' then connection.use Faraday::Response::ParseJson end end - connection.use Faraday::Response::RaiseHttp4xx - connection.use Faraday::Response::Mashify unless raw + connection.use FaradayMiddleware::RaiseHttpException end end end diff --git a/spec/instagram/client_spec.rb b/spec/instagram/client_spec.rb index fefb0902..56a7f2ef 100644 --- a/spec/instagram/client_spec.rb +++ b/spec/instagram/client_spec.rb @@ -5,7 +5,7 @@ client = Instagram::Client.new endpoint = URI.parse(client.endpoint) connection = client.send(:connection).build_url(nil).to_s - connection.should == endpoint.to_s + (connection + '/').should == endpoint.to_s end it "should not cache the user account across clients" do diff --git a/spec/instagram_spec.rb b/spec/instagram_spec.rb index 895805fa..f7eeb6d6 100644 --- a/spec/instagram_spec.rb +++ b/spec/instagram_spec.rb @@ -14,8 +14,7 @@ it "should get the correct resource" do Instagram.user_media_feed() - a_get("users/self/feed.json"). - should have_been_made + a_get("users/self/feed.json").should have_been_made end it "should return the same results as a client" do @@ -94,4 +93,4 @@ end end end -end \ No newline at end of file +end