Skip to content
Browse files

OAuth2 middleware will merge credentials and

headers where appropriate, either the URL as a
query param, or the body of a request.
  • Loading branch information...
1 parent df148ed commit e854bfc77e7b987ed12ff72c91e2e6078b089859 @shayne shayne committed Feb 16, 2011
Showing with 25 additions and 20 deletions.
  1. +11 −4 lib/faraday/oauth2.rb
  2. +14 −16 spec/instagram/client/comments_spec.rb
View
15 lib/faraday/oauth2.rb
@@ -5,16 +5,23 @@ module Faraday
# @private
class Request::OAuth2 < Faraday::Middleware
def call(env)
- params = env[:url].query_values || {}
+
+ params = {}
if @access_token
- params.merge!('access_token' => @access_token)
+ params[:access_token] = @access_token
env[:request_headers].merge!('Authorization' => "Token token=\"#{@access_token}\"")
elsif @client_id
- params.merge!('client_id' => @client_id)
+ params[:client_id] = @client_id
end
- env[:url].query_values = params unless params == {}
+ if env[:body]
+ env[:body] = env[:body].merge(params)
+ elsif env[:url].query_values
+ env[:url].query_values = env[:url].query_values.merge(params)
+ else
+ env[:url].query_values = params
+ end
@app.call env
end
View
30 spec/instagram/client/comments_spec.rb
@@ -3,64 +3,62 @@
describe Instagram::Client do
Instagram::Configuration::VALID_FORMATS.each do |format|
context ".new(:format => '#{format}')" do
-
+
before do
@client = Instagram::Client.new(:format => format, :client_id => 'CID', :access_token => 'AT')
end
-
+
describe ".media_comments" do
-
+
before do
stub_get("media/777/comments.#{format}").
with(:query => {:access_token => @client.access_token}).
to_return(:body => fixture("media_comments.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
end
-
+
it "should get the correct resource" do
@client.media_comments(777)
a_get("media/777/comments.#{format}").
with(:query => {:access_token => @client.access_token}).
should have_been_made
end
-
+
it "should return an array of user search results" do
comments = @client.media_comments(777)
comments.should be_a Array
comments.first.text.should == "Vet visit"
end
end
-
+
describe ".create_media_comment" do
-
+
before do
stub_post("media/777/comments.#{format}").
- with(:query => {:access_token => @client.access_token}).
- with(:body => {:text => "hi there"}).
+ with(:body => {:text => "hi there", :access_token => @client.access_token}).
to_return(:body => fixture("media_comment.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
end
-
+
it "should get the correct resource" do
@client.create_media_comment(777, "hi there")
a_post("media/777/comments.#{format}").
- with(:query => {:access_token => @client.access_token}).
- with(:body => {:text => "hi there"}).
+ with(:body => {:text => "hi there", :access_token => @client.access_token}).
should have_been_made
end
-
+
it "should return the new comment when successful" do
comment = @client.create_media_comment(777, "hi there")
comment.text.should == "hi there"
end
end
-
+
describe ".delete_media_comment" do
-
+
before do
stub_delete("media/777/comments/1234.#{format}").
with(:query => {:access_token => @client.access_token}).
to_return(:body => fixture("media_comment_deleted.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
end
-
+
it "should get the correct resource" do
@client.delete_media_comment(777, 1234)
a_delete("media/777/comments/1234.#{format}").

0 comments on commit e854bfc

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