Skip to content
Browse files

update to latest Faraday + faraday_middleware

  • Loading branch information...
1 parent 66bd4c8 commit d56d5d8a020274cadd922d5fd4fd47191909fc9d @mislav committed Apr 27, 2012
Showing with 24 additions and 40 deletions.
  1. +1 −2 Gemfile
  2. +7 −11 Gemfile.lock
  3. +3 −2 app.rb
  4. +13 −25 instagram.rb
View
3 Gemfile
@@ -17,8 +17,7 @@ gem 'mingo'
gem 'activemodel'
gem 'mongo_ext'
gem 'bson_ext'
-gem 'faraday-stack' #, :path => '/Users/mislav/Projects/faraday-stack'
+gem 'faraday_middleware'
gem 'choices'
-gem 'addressable'
gem 'rake'
gem 'never-forget'
View
18 Gemfile.lock
@@ -6,7 +6,6 @@ GEM
builder (~> 2.1.2)
i18n (~> 0.4)
activesupport (3.0.5)
- addressable (2.2.6)
bson (1.4.0)
bson_ext (1.4.0)
builder (2.1.2)
@@ -17,12 +16,10 @@ GEM
daemons (1.1.2)
erubis (2.7.0)
eventmachine (0.12.10)
- faraday (0.7.4)
- addressable (~> 2.2.6)
- multipart-post (~> 1.1.0)
- rack (>= 1.1.0, < 2)
- faraday-stack (0.1.3)
- faraday (~> 0.6)
+ faraday (0.8.0)
+ multipart-post (~> 1.1)
+ faraday_middleware (0.8.7)
+ faraday (>= 0.7.4, < 0.9)
haml (3.0.25)
hashie (1.1.0)
i18n (0.5.0)
@@ -31,13 +28,13 @@ GEM
mongo (1.4.0)
bson (= 1.4.0)
mongo_ext (0.19.3)
- multipart-post (1.1.2)
+ multipart-post (1.1.5)
never-forget (0.1.0)
activesupport
erubis
mingo (>= 0.2)
rack
- rack (1.3.0)
+ rack (1.4.1)
rake (0.9.2)
shotgun (0.9)
rack (>= 1.0)
@@ -57,12 +54,11 @@ PLATFORMS
DEPENDENCIES
activemodel
activesupport
- addressable
bson_ext
builder
choices
compass
- faraday-stack
+ faraday_middleware
haml
i18n
mingo
View
5 app.rb
@@ -79,9 +79,10 @@ def record(name)
ActiveSupport::Notifications.subscribe('request.faraday') do |name, start, ending, _, payload|
url = payload[:url]
- if url.query_values and (url.query_values.keys & strip_params).any?
+ if url.query
+ query_values = Faraday::Utils.parse_query url.query
url = url.dup
- url.query_values = url.query_values.reject { |k,| strip_params.include? k }
+ url.query = Faraday::Utils.build_query query_values.reject { |k,| strip_params.include? k }
end
$stderr.puts '[%s] %s %s (%.3f s)' % [url.host, payload[:method].to_s.upcase, url.request_uri, ending - start]
View
38 instagram.rb
@@ -1,4 +1,4 @@
-require 'faraday_stack'
+require 'faraday_middleware'
require 'hashie/mash'
module Instagram
@@ -9,16 +9,6 @@ def configure
yield self
end
end
-
- class Mashify < Faraday::Response::Middleware
- def on_complete(env)
- super if Hash === env[:body]
- end
-
- def parse(body)
- Hashie::Mash.new(body)
- end
- end
class OAuthRequest < Faraday::Middleware
def initialize(app, options)
@@ -34,7 +24,8 @@ def call(env)
end
if env[:method] == :get
url = env[:url]
- url.query_values = params.update(url.query_values || {})
+ query_values = params.update Faraday::Utils.parse_query(url.query.to_s)
+ url.query = Faraday::Utils.build_query query_values
env[:url] = url
else
env[:body] = params.update(env[:body] || {})
@@ -52,25 +43,22 @@ def on_complete(env)
module Connection
def connection
- @connection ||= begin
- conn = Faraday.new('https://api.instagram.com/v1/') do |b|
- b.use OAuthRequest, config: self
- b.request :url_encoded
- b.use Mashify
- b.use FaradayStack::ResponseJSON, content_type: 'application/json'
- b.use PreserveRawBody
- b.use FaradayStack::Caching, cache, strip_params: %w[access_token client_id client_secret] unless cache.nil?
- b.use FaradayStack::Instrumentation
- b.adapter Faraday.default_adapter
- end
+ @connection ||= Faraday.new('https://api.instagram.com/v1/') do |conn|
+ conn.use OAuthRequest, config: self
+ conn.request :url_encoded
+ conn.response :mashify
+ conn.response :json, content_type: 'application/json'
+ conn.use PreserveRawBody
+ conn.response :caching, cache, ignore_params: %w[access_token client_id client_secret] unless cache.nil?
+ conn.use :instrumentation
+ conn.adapter Faraday.default_adapter
conn.headers['User-Agent'] = 'instagram.heroku.com ruby client'
conn.options[:timeout] = 6
conn.options[:open_timeout] = 2
- conn
end
end
-
+
def get(path, params = nil)
connection.get(path) do |request|
request.params.update(params) if params

0 comments on commit d56d5d8

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