Skip to content

Commit

Permalink
First shot at http auth seems to be working.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Apr 11, 2009
1 parent 9d4fd99 commit d713ecf
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 50 deletions.
11 changes: 11 additions & 0 deletions examples/httpauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require File.join(File.dirname(__FILE__), '..', 'lib', 'twitter')
require File.join(File.dirname(__FILE__), 'helpers', 'config_store')
require 'pp'

config = ConfigStore.new("#{ENV['HOME']}/.twitter")

httpauth = Twitter::HTTPAuth.new(config['email'], config['password'])
base = Twitter::Base.new(httpauth)

pp base.user_timeline
pp base.verify_credentials
1 change: 1 addition & 0 deletions lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def self.firehose
$:.unshift(directory) unless $:.include?(directory)

require 'twitter/oauth'
require 'twitter/httpauth'
require 'twitter/request'
require 'twitter/base'
require 'twitter/search'
8 changes: 5 additions & 3 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ module Twitter
class Base
extend Forwardable

def_delegators :@client, :get, :post
def_delegators :client, :get, :post

def initialize(oauth)
@client = oauth.access_token
attr_reader :client

def initialize(client)
@client = client
end

# Options: since_id, max_id, count, page, since
Expand Down
26 changes: 26 additions & 0 deletions lib/twitter/httpauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Twitter
class HTTPAuth
include HTTParty
base_uri 'http://twitter.com'
format :plain

attr_reader :username, :password

def initialize(username, password)
@username, @password = username, password
end

def get(uri, headers={})
self.class.get(uri, :headers => headers, :basic_auth => basic_auth)
end

def post(uri, body={}, headers={})
self.class.post(uri, :body => body, :headers => headers, :basic_auth => basic_auth)
end

private
def basic_auth
@basic_auth ||= {:username => @username, :password => @password}
end
end
end
5 changes: 3 additions & 2 deletions lib/twitter/oauth.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module Twitter
class OAuth
extend Forwardable
def_delegators :access_token, :get, :post

attr_reader :ctoken, :csecret

def initialize(ctoken, csecret)
Expand All @@ -24,8 +27,6 @@ def access_token
@access_token ||= ::OAuth::AccessToken.new(consumer, @atoken, @asecret)
end

alias client access_token

def authorize_from_access(atoken, asecret)
@atoken, @asecret = atoken, asecret
end
Expand Down
16 changes: 8 additions & 8 deletions lib/twitter/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ module Twitter
class Request
extend Forwardable

def self.get(base, path, options={})
new(base, :get, path, options).perform
def self.get(client, path, options={})
new(client, :get, path, options).perform
end

def self.post(base, path, options={})
new(base, :post, path, options).perform
def self.post(client, path, options={})
new(client, :post, path, options).perform
end

attr_reader :base, :method, :path, :options
attr_reader :client, :method, :path, :options

def_delegators :base, :get, :post
def_delegators :client, :get, :post

def initialize(base, method, path, options={})
@base, @method, @path, @options = base, method, path, options
def initialize(client, method, path, options={})
@client, @method, @path, @options = client, method, path, options
end

def uri
Expand Down
10 changes: 6 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ def fixture_file(filename)
File.read(file_path)
end

def twitter_url(url)
url =~ /^http/ ? url : "http://twitter.com:80#{url}"
end

def stub_get(url, filename, status=nil)
url = url =~ /^http/ ? url : "http://twitter.com:80#{url}"

options = {:string => fixture_file(filename)}
options.merge!({:status => status}) unless status.nil?

FakeWeb.register_uri(:get, url, options)
FakeWeb.register_uri(:get, twitter_url(url), options)
end

def stub_post(url, filename)
FakeWeb.register_uri(:post, "http://twitter.com:80#{url}", :string => fixture_file(filename))
FakeWeb.register_uri(:post, twitter_url(url), :string => fixture_file(filename))
end
5 changes: 3 additions & 2 deletions test/twitter/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class BaseTest < Test::Unit::TestCase
end

context "initialize" do
should "require an oauth object" do
@twitter.instance_variable_get('@client').should == @access_token
should "require a client" do
@twitter.client.should respond_to(:get)
@twitter.client.should respond_to(:post)
end
end

Expand Down
50 changes: 50 additions & 0 deletions test/twitter/httpauth_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require File.dirname(__FILE__) + '/../test_helper'

class HTTPAuthTest < Test::Unit::TestCase
context "Creating new instance" do
should "should take user and password" do
twitter = Twitter::HTTPAuth.new('username', 'password')
twitter.username.should == 'username'
twitter.password.should == 'password'
end
end

context "Client methods" do
setup do
@twitter = Twitter::HTTPAuth.new('username', 'password')
end

should "be able to get" do
stub_get('http://twitter.com:80/statuses/user_timeline.json', 'user_timeline.json')
response = @twitter.get('/statuses/user_timeline.json')
response.should == fixture_file('user_timeline.json')
end

should "be able to get with headers" do
@twitter.class.expects(:get).with(
'/statuses/user_timeline.json', {
:basic_auth => {:username => 'username', :password => 'password'},
:headers => {'Foo' => 'Bar'}
}
).returns(fixture_file('user_timeline.json'))
@twitter.get('/statuses/user_timeline.json', {'Foo' => 'Bar'})
end

should "be able to post" do
stub_post('http://twitter.com:80/statuses/update.json', 'status.json')
response = @twitter.post('/statuses/update.json', :text => 'My update.')
response.should == fixture_file('status.json')
end

should "be able to post with headers" do
@twitter.class.expects(:post).with(
'/statuses/update.json', {
:headers => {'Foo' => 'Bar'},
:body => {:text => 'My update.'},
:basic_auth => {:username => 'username', :password => 'password'}
}
).returns(fixture_file('status.json'))
@twitter.post('/statuses/update.json', {:text => 'My update.'}, {'Foo' => 'Bar'})
end
end
end
17 changes: 13 additions & 4 deletions test/twitter/oauth_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,19 @@ class OAuthTest < Test::Unit::TestCase
twitter.access_token.secret.should == 'asecret'
end

should "alias oauth token to client" do
should "delegate get to access token" do
access_token = mock('access token')
twitter = Twitter::OAuth.new('token', 'secret')
twitter.authorize_from_access('atoken', 'asecret')

twitter.client.should == twitter.access_token
twitter.stubs(:access_token).returns(access_token)
access_token.expects(:get).returns(nil)
twitter.get('/foo')
end

should "delegate post to access token" do
access_token = mock('access token')
twitter = Twitter::OAuth.new('token', 'secret')
twitter.stubs(:access_token).returns(access_token)
access_token.expects(:post).returns(nil)
twitter.post('/foo')
end
end
Loading

0 comments on commit d713ecf

Please sign in to comment.