Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module Fredo | ||
module Shorthands | ||
|
||
def oauth(provider, credentials) | ||
|
||
case provider | ||
when 'twitter' | ||
#Application request token | ||
post 'https://api.twitter.com/oauth/request_token' do | ||
[ | ||
200, {:oauth_token_secret => 1234, | ||
:oauth_token => 'abc123', | ||
:oauth_callback_confirmed => true}, | ||
[] | ||
] | ||
end | ||
|
||
# User performs verification | ||
get 'https://api.twitter.com/oauth/authorize' do | ||
redirect_url = URI.parse params['redirect_uri'] | ||
redirect_url.query = querify :oauth_token => 12345, :oauth_verifier => 'abc123' | ||
[ 302, {'Location'=> redirect_url.to_s }, [] ] | ||
end | ||
|
||
# Twitter sends back the access token as an url encoded reply? | ||
post 'https://api.twitter.com/oauth/access_token' do | ||
[ | ||
200, {}, | ||
querify(:oauth_token_secret => 1234, :oauth_token => 'abc123', :user_id => 1234, :screen_name => 'john.smith') | ||
] | ||
end | ||
|
||
get('https://api.twitter.com/account/verify_credentials.json'){[200, {}, credentials.to_json]} | ||
|
||
when 'facebook' | ||
|
||
# User authorization | ||
get 'https://graph.facebook.com/oauth/authorize' do | ||
redirect_url = URI.parse params['redirect_uri'] | ||
redirect_url.query = querify :code => 12345 | ||
[ 302, {'Location'=> redirect_url.to_s }, [] ] | ||
end | ||
|
||
# Fetching the token | ||
get 'https://graph.facebook.com/oauth/access_token' do | ||
[ | ||
200, {'Content-Type' => 'application/json' }, | ||
{:access_token => 12345, :expires => 10.days.from_now.to_i}.to_json | ||
] | ||
end | ||
|
||
# fetching credentials | ||
get 'https://graph.facebook.com/me' do | ||
[ | ||
200, {'Content-Type' => 'application/json' }, | ||
credentials.to_json | ||
] | ||
end | ||
end | ||
|
||
end | ||
|
||
private | ||
|
||
def queryfy(hash) | ||
hash.map {|k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v.to_s)]}.join('&') | ||
end | ||
|
||
end | ||
end |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper') | ||
|
||
require 'faraday' | ||
|
||
describe Fredo do | ||
|
||
|
||
context "request handling" do | ||
|
||
|
||
|
||
it "intercepts faraday's requests" do | ||
body = "Today ain't your lucky day, kid." | ||
|
||
Fredo.get('http://www.google.com') {body} | ||
|
||
response = Faraday.get 'http://www.google.com/' | ||
|
||
response.body.should eql(body) | ||
end | ||
end | ||
|
||
end |