Skip to content

Commit

Permalink
OAuth2 middleware will merge credentials and
Browse files Browse the repository at this point in the history
headers where appropriate, either the URL as a
query param, or the body of a request.
  • Loading branch information
shayne committed Feb 17, 2011
1 parent df148ed commit e854bfc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
15 changes: 11 additions & 4 deletions lib/faraday/oauth2.rb
Expand Up @@ -5,16 +5,23 @@ module Faraday
# @private # @private
class Request::OAuth2 < Faraday::Middleware class Request::OAuth2 < Faraday::Middleware
def call(env) def call(env)
params = env[:url].query_values || {}
params = {}


if @access_token if @access_token
params.merge!('access_token' => @access_token) params[:access_token] = @access_token
env[:request_headers].merge!('Authorization' => "Token token=\"#{@access_token}\"") env[:request_headers].merge!('Authorization' => "Token token=\"#{@access_token}\"")
elsif @client_id elsif @client_id
params.merge!('client_id' => @client_id) params[:client_id] = @client_id
end 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 @app.call env
end end
Expand Down
30 changes: 14 additions & 16 deletions spec/instagram/client/comments_spec.rb
Expand Up @@ -3,64 +3,62 @@
describe Instagram::Client do describe Instagram::Client do
Instagram::Configuration::VALID_FORMATS.each do |format| Instagram::Configuration::VALID_FORMATS.each do |format|
context ".new(:format => '#{format}')" do context ".new(:format => '#{format}')" do

before do before do
@client = Instagram::Client.new(:format => format, :client_id => 'CID', :access_token => 'AT') @client = Instagram::Client.new(:format => format, :client_id => 'CID', :access_token => 'AT')
end end

describe ".media_comments" do describe ".media_comments" do

before do before do
stub_get("media/777/comments.#{format}"). stub_get("media/777/comments.#{format}").
with(:query => {:access_token => @client.access_token}). with(:query => {:access_token => @client.access_token}).
to_return(:body => fixture("media_comments.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"}) to_return(:body => fixture("media_comments.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
end end

it "should get the correct resource" do it "should get the correct resource" do
@client.media_comments(777) @client.media_comments(777)
a_get("media/777/comments.#{format}"). a_get("media/777/comments.#{format}").
with(:query => {:access_token => @client.access_token}). with(:query => {:access_token => @client.access_token}).
should have_been_made should have_been_made
end end

it "should return an array of user search results" do it "should return an array of user search results" do
comments = @client.media_comments(777) comments = @client.media_comments(777)
comments.should be_a Array comments.should be_a Array
comments.first.text.should == "Vet visit" comments.first.text.should == "Vet visit"
end end
end end

describe ".create_media_comment" do describe ".create_media_comment" do

before do before do
stub_post("media/777/comments.#{format}"). stub_post("media/777/comments.#{format}").
with(:query => {:access_token => @client.access_token}). with(:body => {:text => "hi there", :access_token => @client.access_token}).
with(:body => {:text => "hi there"}).
to_return(:body => fixture("media_comment.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"}) to_return(:body => fixture("media_comment.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
end end

it "should get the correct resource" do it "should get the correct resource" do
@client.create_media_comment(777, "hi there") @client.create_media_comment(777, "hi there")
a_post("media/777/comments.#{format}"). a_post("media/777/comments.#{format}").
with(:query => {:access_token => @client.access_token}). with(:body => {:text => "hi there", :access_token => @client.access_token}).
with(:body => {:text => "hi there"}).
should have_been_made should have_been_made
end end

it "should return the new comment when successful" do it "should return the new comment when successful" do
comment = @client.create_media_comment(777, "hi there") comment = @client.create_media_comment(777, "hi there")
comment.text.should == "hi there" comment.text.should == "hi there"
end end
end end

describe ".delete_media_comment" do describe ".delete_media_comment" do

before do before do
stub_delete("media/777/comments/1234.#{format}"). stub_delete("media/777/comments/1234.#{format}").
with(:query => {:access_token => @client.access_token}). with(:query => {:access_token => @client.access_token}).
to_return(:body => fixture("media_comment_deleted.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"}) to_return(:body => fixture("media_comment_deleted.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
end end

it "should get the correct resource" do it "should get the correct resource" do
@client.delete_media_comment(777, 1234) @client.delete_media_comment(777, 1234)
a_delete("media/777/comments/1234.#{format}"). a_delete("media/777/comments/1234.#{format}").
Expand Down

0 comments on commit e854bfc

Please sign in to comment.