Permalink
Browse files

First shot at http auth seems to be working.

  • Loading branch information...
1 parent 9d4fd99 commit d713ecfbe80edde688009fa6bfbf32a2de687a39 @jnunemaker jnunemaker committed Apr 11, 2009
View
@@ -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
View
@@ -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'
View
@@ -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
View
@@ -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
View
@@ -1,5 +1,8 @@
module Twitter
class OAuth
+ extend Forwardable
+ def_delegators :access_token, :get, :post
+
attr_reader :ctoken, :csecret
def initialize(ctoken, csecret)
@@ -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
View
@@ -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
View
@@ -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
@@ -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
@@ -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
View
@@ -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
Oops, something went wrong.

0 comments on commit d713ecf

Please sign in to comment.