Skip to content

Commit

Permalink
updated faraday version and fixed related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Bakkum committed Feb 9, 2012
1 parent c24c963 commit b84aee3
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 79 deletions.
6 changes: 3 additions & 3 deletions instagram.gemspec
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions lib/faraday/oauth2.rb
@@ -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}\"")
Expand All @@ -24,6 +25,8 @@ def call(env)
end
end

env[:url].query_values = nil if env[:url].query_values == {}

@app.call env
end

Expand Down
37 changes: 0 additions & 37 deletions lib/faraday/raise_http_4xx.rb

This file was deleted.

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

This file was deleted.

45 changes: 45 additions & 0 deletions 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
1 change: 1 addition & 0 deletions lib/instagram/client/subscriptions.rb
@@ -1,4 +1,5 @@
require 'openssl'
require 'multi_json'

module Instagram
class Client
Expand Down
8 changes: 4 additions & 4 deletions lib/instagram/connection.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/instagram/client_spec.rb
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions spec/instagram_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -94,4 +93,4 @@
end
end
end
end
end

0 comments on commit b84aee3

Please sign in to comment.