Permalink
Browse files

updated faraday version and fixed related issues

  • Loading branch information...
1 parent c24c963 commit b84aee3eb359b90692c7149f22caa2934b47a59f @bakks bakks committed Feb 9, 2012
View
@@ -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 =<<eos
View
@@ -1,13 +1,14 @@
require 'faraday'
# @private
-module Faraday
+module FaradayMiddleware
# @private
- class Request::OAuth2 < Faraday::Middleware
+ class OAuth2 < Faraday::Middleware
def call(env)
if env[:method] == :get or env[:method] == :delete
env[:url].query_values = {} if env[:url].query_values.nil?
+
if @access_token and not env[:url].query_values["client_secret"]
env[:url].query_values = env[:url].query_values.merge(:access_token => @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
@@ -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
@@ -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
@@ -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
@@ -1,4 +1,5 @@
require 'openssl'
+require 'multi_json'
module Instagram
class Client
@@ -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
@@ -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
View
@@ -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
+end

0 comments on commit b84aee3

Please sign in to comment.