Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Dispatcher classes to perform API calls without depending on an…
…other Twitter client gem.
- Loading branch information
Michael Bleigh
committed
Mar 20, 2009
1 parent
4ff5ea6
commit 8b9bdf1
Showing
12 changed files
with
254 additions
and
7 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
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
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
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
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
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,44 @@ | ||
require 'net/http' | ||
|
||
module TwitterAuth | ||
module Dispatcher | ||
class Basic | ||
attr_accessor :user | ||
|
||
def initialize(user) | ||
raise TwitterAuth::Error, 'Dispatcher must be initialized with a User.' unless user.is_a?(TwitterAuth::BasicUser) | ||
self.user = user | ||
end | ||
|
||
def request(http_method, path, *arguments) | ||
path << '.json' unless path.match(/\.(:?xml|json)\z/i) | ||
|
||
response = TwitterAuth.net.start{ |http| | ||
req = "Net::HTTP::#{http_method.to_s.capitalize}".constantize.new(path, *arguments) | ||
req.basic_auth user.login, user.password | ||
http.request(req) | ||
} | ||
|
||
JSON.parse(response.body) | ||
rescue JSON::ParserError | ||
response.body | ||
end | ||
|
||
def get(path, *arguments) | ||
request(:get, path, *arguments) | ||
end | ||
|
||
def post(path, *arguments) | ||
request(:post, path, *arguments) | ||
end | ||
|
||
def put(path, *arguments) | ||
request(:put, path, *arguments) | ||
end | ||
|
||
def delete(path, *arguments) | ||
request(:delete, path, *arguments) | ||
end | ||
end | ||
end | ||
end |
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 'oauth' | ||
|
||
module TwitterAuth | ||
module Dispatcher | ||
class Oauth < OAuth::AccessToken | ||
attr_accessor :user | ||
|
||
def initialize(user) | ||
raise TwitterAuth::Error, 'Dispatcher must be initialized with a User.' unless user.is_a?(TwitterAuth::OauthUser) | ||
self.user = user | ||
super(TwitterAuth.consumer, user.access_token, user.access_secret) | ||
end | ||
|
||
def request(http_method, path, *arguments) | ||
path << '.json' unless path.match(/\.(:?xml|json)\z/i) | ||
response = super | ||
JSON.parse(response.body) | ||
rescue JSON::ParserError | ||
response.body | ||
end | ||
end | ||
end | ||
end |
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
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
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,59 @@ | ||
require File.dirname(__FILE__) + '/../../spec_helper' | ||
|
||
describe TwitterAuth::Dispatcher::Basic do | ||
before do | ||
stub_basic! | ||
@user = Factory.create(:twitter_basic_user, :login => 'twitterman', :password => 'test') | ||
end | ||
|
||
it 'should require a user as the initialization argument' do | ||
lambda{TwitterAuth::Dispatcher::Basic.new(nil)}.should raise_error(TwitterAuth::Error, 'Dispatcher must be initialized with a User.') | ||
end | ||
|
||
it 'should store the user in an attr_accessor' do | ||
TwitterAuth::Dispatcher::Basic.new(@user).user.should == @user | ||
end | ||
|
||
describe '#request' do | ||
before do | ||
@dispatcher = TwitterAuth::Dispatcher::Basic.new(@user) | ||
FakeWeb.register_uri('https://twitter.com:443/fake.json', :string => {'fake' => true}.to_json) | ||
FakeWeb.register_uri('https://twitter.com:443/fake.xml', :string => '<fake>true</fake>') | ||
end | ||
|
||
it 'should automatically parse JSON if valid' do | ||
@dispatcher.request(:get, '/fake.json').should == {'fake' => true} | ||
end | ||
|
||
it 'should return XML as a string' do | ||
@dispatcher.request(:get, '/fake.xml').should == "<fake>true</fake>" | ||
end | ||
|
||
it 'should append .json to the path if no extension is provided' do | ||
@dispatcher.request(:get, '/fake.json').should == @dispatcher.request(:get, '/fake') | ||
end | ||
|
||
%w(get post put delete).each do |method| | ||
it "should build a #{method} class based on a :#{method} http_method" do | ||
@req = "Net::HTTP::#{method.capitalize}".constantize.new('/fake.json') | ||
"Net::HTTP::#{method.capitalize}".constantize.should_receive(:new).and_return(@req) | ||
@dispatcher.request(method.to_sym, '/fake') | ||
end | ||
end | ||
|
||
it 'should start the HTTP session' do | ||
@net = TwitterAuth.net | ||
TwitterAuth.stub!(:net).and_return(@net) | ||
@net.should_receive(:start) | ||
lambda{@dispatcher.request(:get, '/fake')}.should raise_error(NoMethodError) | ||
end | ||
end | ||
|
||
%w(get post delete put).each do |method| | ||
it "should have a ##{method} method that calls request(:#{method})" do | ||
dispatcher = TwitterAuth::Dispatcher::Basic.new(@user) | ||
dispatcher.should_receive(:request).with(method.to_sym, '/fake.json') | ||
dispatcher.send(method, '/fake.json') | ||
end | ||
end | ||
end |
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,52 @@ | ||
require File.dirname(__FILE__) + '/../../spec_helper' | ||
|
||
describe TwitterAuth::Dispatcher::Oauth do | ||
before do | ||
stub_oauth! | ||
@user = Factory.create(:twitter_oauth_user, :access_token => 'token', :access_secret => 'secret') | ||
end | ||
|
||
it 'should be a child class of OAuth::AccessToken' do | ||
TwitterAuth::Dispatcher::Oauth.new(@user).should be_a(OAuth::AccessToken) | ||
end | ||
|
||
it 'should require initialization of an OauthUser' do | ||
lambda{TwitterAuth::Dispatcher::Oauth.new(nil)}.should raise_error(TwitterAuth::Error, 'Dispatcher must be initialized with a User.') | ||
end | ||
|
||
it 'should store the user in an attr_accessor' do | ||
TwitterAuth::Dispatcher::Oauth.new(@user).user.should == @user | ||
end | ||
|
||
it "should initialize with the user's token and secret" do | ||
d = TwitterAuth::Dispatcher::Oauth.new(@user) | ||
d.token.should == 'token' | ||
d.secret.should == 'secret' | ||
end | ||
|
||
describe '#request' do | ||
before do | ||
@dispatcher = TwitterAuth::Dispatcher::Oauth.new(@user) | ||
FakeWeb.register_uri(:get, 'https://twitter.com:443/fake.json', :string => {'fake' => true}.to_json) | ||
FakeWeb.register_uri(:get, 'https://twitter.com:443/fake.xml', :string => "<fake>true</fake>") | ||
end | ||
|
||
it 'should automatically parse json' do | ||
result = @dispatcher.request(:get, '/fake.json') | ||
result.should be_a(Hash) | ||
result['fake'].should be_true | ||
end | ||
|
||
it 'should return xml as a string' do | ||
@dispatcher.request(:get, '/fake.xml').should == '<fake>true</fake>' | ||
end | ||
|
||
it 'should append .json to the path if no extension is provided' do | ||
@dispatcher.request(:get, '/fake').should == @dispatcher.request(:get, '/fake.json') | ||
end | ||
|
||
it 'should work with verb methods' do | ||
@dispatcher.get('/fake').should == @dispatcher.request(:get, '/fake') | ||
end | ||
end | ||
end |
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