Skip to content
This repository
Browse code

updated faraday version and fixed related issues

  • Loading branch information...
commit b84aee3eb359b90692c7149f22caa2934b47a59f 1 parent c24c963
Peter Bakkum authored February 09, 2012
6  instagram.gemspec
@@ -5,10 +5,10 @@ Gem::Specification.new do |s|
5 5
   s.add_development_dependency('rspec', '~> 2.4')
6 6
   s.add_development_dependency('webmock', '~> 1.6')
7 7
   s.add_development_dependency('bluecloth', '~> 2.0.11')
8  
-  s.add_runtime_dependency('faraday', '~> 0.5.4')
9  
-  s.add_runtime_dependency('faraday_middleware', '~> 0.3.1')
  8
+  s.add_runtime_dependency('faraday', '~> 0.7')
  9
+  s.add_runtime_dependency('faraday_middleware', '~> 0.8')
10 10
   s.add_runtime_dependency('multi_json', '~> 1.0.3')
11  
-  s.add_runtime_dependency('hashie',  '>= 0.4.0')
  11
+  s.add_runtime_dependency('hashie',  '>= 1.2.0')
12 12
   s.authors = ["Shayne Sweeney"]
13 13
   s.description = %q{A Ruby wrapper for the Instagram REST and Search APIs}
14 14
   s.post_install_message =<<eos
7  lib/faraday/oauth2.rb
... ...
@@ -1,13 +1,14 @@
1 1
 require 'faraday'
2 2
 
3 3
 # @private
4  
-module Faraday
  4
+module FaradayMiddleware
5 5
   # @private
6  
-  class Request::OAuth2 < Faraday::Middleware
  6
+  class OAuth2 < Faraday::Middleware
7 7
     def call(env)
8 8
 
9 9
       if env[:method] == :get or env[:method] == :delete
10 10
         env[:url].query_values = {} if env[:url].query_values.nil?
  11
+
11 12
         if @access_token and not env[:url].query_values["client_secret"]
12 13
           env[:url].query_values = env[:url].query_values.merge(:access_token => @access_token)
13 14
           env[:request_headers] = env[:request_headers].merge('Authorization' => "Token token=\"#{@access_token}\"")
@@ -24,6 +25,8 @@ def call(env)
24 25
         end
25 26
       end
26 27
 
  28
+      env[:url].query_values = nil if env[:url].query_values == {}
  29
+
27 30
       @app.call env
28 31
     end
29 32
 
37  lib/faraday/raise_http_4xx.rb
... ...
@@ -1,37 +0,0 @@
1  
-require 'faraday'
2  
-
3  
-# @private
4  
-module Faraday
5  
-  # @private
6  
-  class Response::RaiseHttp4xx < Response::Middleware
7  
-    def self.register_on_complete(env)
8  
-      env[:response].on_complete do |response|
9  
-        case response[:status].to_i
10  
-        when 400
11  
-          raise Instagram::BadRequest, error_message(response)
12  
-        when 404
13  
-          raise Instagram::NotFound, error_message(response)
14  
-        end
15  
-      end
16  
-    end
17  
-
18  
-    def initialize(app)
19  
-      super
20  
-      @parser = nil
21  
-    end
22  
-
23  
-    private
24  
-
25  
-    def self.error_message(response)
26  
-      "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{error_body(response[:body])}"
27  
-    end
28  
-
29  
-    def self.error_body(body)
30  
-      if body.nil?
31  
-        nil
32  
-      elsif body['meta'] and body['meta']['error_message'] and not body['meta']['error_message'].empty?
33  
-        ": #{body['meta']['error_message']}"
34  
-      end
35  
-    end
36  
-  end
37  
-end
29  lib/faraday/raise_http_5xx.rb
... ...
@@ -1,29 +0,0 @@
1  
-require 'faraday'
2  
-
3  
-# @private
4  
-module Faraday
5  
-  # @private
6  
-  class Response::RaiseHttp5xx < Response::Middleware
7  
-    def self.register_on_complete(env)
8  
-      env[:response].on_complete do |response|
9  
-        case response[:status].to_i
10  
-        when 500
11  
-          raise Instagram::InternalServerError, error_message(response, "Something is technically wrong.")
12  
-        when 503
13  
-          raise Instagram::ServiceUnavailable, error_message(response, "Instagram is rate limiting your requests.")
14  
-        end
15  
-      end
16  
-    end
17  
-
18  
-    def initialize(app)
19  
-      super
20  
-      @parser = nil
21  
-    end
22  
-
23  
-    private
24  
-
25  
-    def self.error_message(response, body=nil)
26  
-      "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
27  
-    end
28  
-  end
29  
-end
45  lib/faraday/raise_http_exception.rb
... ...
@@ -0,0 +1,45 @@
  1
+require 'faraday'
  2
+
  3
+# @private
  4
+module FaradayMiddleware
  5
+  # @private
  6
+  class RaiseHttpException < Faraday::Middleware
  7
+    def call(env)
  8
+      @app.call(env).on_complete do |response|
  9
+        case response[:status].to_i
  10
+        when 400
  11
+          raise Instagram::BadRequest, error_message_400(response)
  12
+        when 404
  13
+          raise Instagram::NotFound, error_message_400(response)
  14
+        when 500
  15
+          raise Instagram::InternalServerError, error_message_500(response, "Something is technically wrong.")
  16
+        when 503
  17
+          raise Instagram::ServiceUnavailable, error_message_500(response, "Instagram is rate limiting your requests.")
  18
+        end
  19
+      end
  20
+    end
  21
+
  22
+    def initialize(app)
  23
+      super app
  24
+      @parser = nil
  25
+    end
  26
+
  27
+    private
  28
+
  29
+    def error_message_400(response)
  30
+      "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{error_body(response[:body])}"
  31
+    end
  32
+
  33
+    def error_body(body)
  34
+      if body.nil?
  35
+        nil
  36
+      elsif body['meta'] and body['meta']['error_message'] and not body['meta']['error_message'].empty?
  37
+        ": #{body['meta']['error_message']}"
  38
+      end
  39
+    end
  40
+
  41
+    def error_message_500(response, body=nil)
  42
+      "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
  43
+    end
  44
+  end
  45
+end
1  lib/instagram/client/subscriptions.rb
... ...
@@ -1,4 +1,5 @@
1 1
 require 'openssl'
  2
+require 'multi_json'
2 3
 
3 4
 module Instagram
4 5
   class Client
8  lib/instagram/connection.rb
@@ -15,16 +15,16 @@ def connection(raw=false)
15 15
       }
16 16
 
17 17
       Faraday::Connection.new(options) do |connection|
18  
-        connection.use Faraday::Request::OAuth2, client_id, access_token
  18
+        connection.use FaradayMiddleware::OAuth2, client_id, access_token
  19
+        connection.use Faraday::Request::UrlEncoded
19 20
         connection.adapter(adapter)
20  
-        connection.use Faraday::Response::RaiseHttp5xx
  21
+        connection.use FaradayMiddleware::Mashify unless raw
21 22
         unless raw
22 23
           case format.to_s.downcase
23 24
           when 'json' then connection.use Faraday::Response::ParseJson
24 25
           end
25 26
         end
26  
-        connection.use Faraday::Response::RaiseHttp4xx
27  
-        connection.use Faraday::Response::Mashify unless raw
  27
+        connection.use FaradayMiddleware::RaiseHttpException
28 28
       end
29 29
     end
30 30
   end
2  spec/instagram/client_spec.rb
@@ -5,7 +5,7 @@
5 5
     client = Instagram::Client.new
6 6
     endpoint = URI.parse(client.endpoint)
7 7
     connection = client.send(:connection).build_url(nil).to_s
8  
-    connection.should == endpoint.to_s
  8
+    (connection + '/').should == endpoint.to_s
9 9
   end
10 10
 
11 11
   it "should not cache the user account across clients" do
5  spec/instagram_spec.rb
@@ -14,8 +14,7 @@
14 14
 
15 15
      it "should get the correct resource" do
16 16
        Instagram.user_media_feed()
17  
-       a_get("users/self/feed.json").
18  
-         should have_been_made
  17
+       a_get("users/self/feed.json").should have_been_made
19 18
      end
20 19
 
21 20
      it "should return the same results as a client" do
@@ -94,4 +93,4 @@
94 93
       end
95 94
     end
96 95
   end
97  
-end
  96
+end

0 notes on commit b84aee3

Please sign in to comment.
Something went wrong with that request. Please try again.