Permalink
Browse files

Use Faraday::Utils to parse and update the query (inspired by https:/…

  • Loading branch information...
1 parent 9e726fb commit e7587f4de04c5cdd49f9e9eeff9c37791275427a @rewritten rewritten committed Apr 28, 2012
Showing with 6 additions and 8 deletions.
  1. +6 −8 lib/faraday/oauth2.rb
View
@@ -1,22 +1,20 @@
-require 'addressable/uri'
require 'faraday'
# @private
module FaradayMiddleware
# @private
class OAuth2 < Faraday::Middleware
def call(env)
- env[:url] = Addressable::URI.parse(env[:url])
- env[:url].port = env[:url].inferred_port
if env[:method] == :get or env[:method] == :delete
- env[:url].query_values = {} if env[:url].query_values.nil?
+ query = Faraday::Utils.parse_query(env[:url].query)
- if @access_token and not env[:url].query_values["client_secret"]
- env[:url].query_values = env[:url].query_values.merge(:access_token => @access_token)
+ if @access_token and not query["client_secret"]
+ query.update(:access_token => @access_token)
env[:request_headers] = env[:request_headers].merge('Authorization' => "Token token=\"#{@access_token}\"")
elsif @client_id
- env[:url].query_values = env[:url].query_values.merge(:client_id => @client_id)
+ query.update(:client_id => @client_id)
end
+ env[:url].query = Faraday::Utils.build_query(query)
else
if @access_token and not env[:body] && env[:body][:client_secret]
env[:body] = {} if env[:body].nil?
@@ -27,7 +25,7 @@ def call(env)
end
end
- env[:url].query_values = nil if env[:url].query_values == {}
+ env[:url].query = nil if env[:url].query == ""
@app.call env
end

0 comments on commit e7587f4

Please sign in to comment.