Skip to content

Commit

Permalink
Fix code to use integers and add to authenticator
Browse files Browse the repository at this point in the history
  • Loading branch information
mmangino committed Feb 15, 2011
1 parent 45f7192 commit 468cb88
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
15 changes: 11 additions & 4 deletions lib/mogli/authenticator.rb
Expand Up @@ -24,24 +24,31 @@ def get_access_token_for_session_key(session_keys)
keystr = session_keys.is_a?(Array) ?
session_keys.join(',') : session_keys
client = Mogli::Client.new
client.post("oauth/exchange_sessions", nil,
response = client.post("oauth/exchange_sessions", nil,
{:type => 'client_cred',
:client_id => client_id,
:client_secret => secret,
:sessions => keystr})
raise_exception_if_required(response)
response
end

def get_access_token_for_application
client = Mogli::Client.new
request = client.class.post(client.api_path('oauth/access_token'),
response = client.class.post(client.api_path('oauth/access_token'),
:body=> {
:grant_type => 'client_credentials',
:client_id => client_id,
:client_secret => secret
}
)
request.to_s.split("=").last
end
raise_exception_if_required(response)
response.to_s.split("=").last
end

def raise_exception_if_required(response)
raise Mogli::Client::HTTPException if response.code != 200
end

end
end
2 changes: 1 addition & 1 deletion lib/mogli/client.rb
Expand Up @@ -184,7 +184,7 @@ def determine_class(klass_or_klasses,data)
end

def raise_error_if_necessary(data)
raise HTTPException if data.respond_to?(:code) and data.code != '200'
raise HTTPException if data.respond_to?(:code) and data.code != 200
if data.kind_of?(Hash)
if data.keys.size == 1 and data["error"]
self.class.raise_error_by_type_and_message(data["error"]["type"], data["error"]["message"])
Expand Down
25 changes: 21 additions & 4 deletions spec/authenticator_spec.rb
Expand Up @@ -31,13 +31,16 @@
end

it "can trade session_keys for access_tokens" do
response = [{"access_token" => "myaccesstoken", "expires" => 5000},
{"access_token" => "youraccesstoken", "expires" => 5000}]
response.stub!(:code).and_return(200)

Mogli::Client.should_receive(:post).
with("https://graph.facebook.com/oauth/exchange_sessions",
:body => {:type => "client_cred", :client_id => "123456",
:client_secret => "secret",
:sessions => "mysessionkey,yoursessionkey"}).
and_return([{"access_token" => "myaccesstoken", "expires" => 5000},
{"access_token" => "youraccesstoken", "expires" => 5000}])
and_return(response)

authenticator.
get_access_token_for_session_key(["mysessionkey","yoursessionkey"]).
Expand All @@ -46,30 +49,44 @@
end

it "can trade one session_key for an access_tokens" do
response = {"access_token" => "myaccesstoken", "expires" => 5000}
response.stub!(:code).and_return(200)
Mogli::Client.should_receive(:post).
with("https://graph.facebook.com/oauth/exchange_sessions",
:body => {:type => "client_cred", :client_id => "123456",
:client_secret => "secret", :sessions => "mysessionkey"}).
and_return({"access_token" => "myaccesstoken", "expires" => 5000})
and_return(response)

authenticator.
get_access_token_for_session_key("mysessionkey").
should == {"access_token" => "myaccesstoken", "expires" => 5000}
end

it "can ask for an application token" do
response = "access_token=123456|3SDdfgdfgv0bbEvYjBH5tJtl-dcBdsfgo"
response.stub!(:code).and_return(200)
Mogli::Client.should_receive(:post).
with("https://graph.facebook.com/oauth/access_token",
:body=> {
:grant_type => 'client_credentials',
:client_id => "123456",
:client_secret => "secret"
}).
and_return("access_token=123456|3SDdfgdfgv0bbEvYjBH5tJtl-dcBdsfgo")
and_return(response)


token = authenticator.get_access_token_for_application
token.should =="123456|3SDdfgdfgv0bbEvYjBH5tJtl-dcBdsfgo"
end

it "raises an error if not a 200" do
response = "access_token=123456|3SDdfgdfgv0bbEvYjBH5tJtl-dcBdsfgo"
response.stub!(:code).and_return(500)
Mogli::Client.should_receive(:post).and_return(response)
lambda do
token = authenticator.get_access_token_for_application
end.should raise_error(Mogli::Client::HTTPException)

end

end
2 changes: 1 addition & 1 deletion spec/client_spec.rb
Expand Up @@ -110,7 +110,7 @@
end

it "parses http response errors" do
Mogli::Client.should_receive(:post).and_return(mock("httpresponse",:code=>'500'))
Mogli::Client.should_receive(:post).and_return(mock("httpresponse",:code=>500))
client = Mogli::Client.new("1234")
lambda do
result = client.post("1/feed","Post",:message=>"message")
Expand Down

0 comments on commit 468cb88

Please sign in to comment.