Skip to content

Commit

Permalink
Added post requests to the Twitter::Request class and added update to…
Browse files Browse the repository at this point in the history
… post updates to twitter.
  • Loading branch information
jnunemaker committed Apr 3, 2009
1 parent b8f1b76 commit 00eebab
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
9 changes: 9 additions & 0 deletions lib/twitter/base.rb
Expand Up @@ -23,9 +23,18 @@ def status(id)
perform_get("/statuses/show/#{id}.json")
end

# Options: in_reply_to_status_id
def update(status, options={})
perform_post("/statuses/update.json", :body => {:status => status}.merge(options))
end

private
def perform_get(path, options={})
Twitter::Request.get(self, path, options)
end

def perform_post(path, options={})
Twitter::Request.post(self, path, options)
end
end
end
14 changes: 13 additions & 1 deletion lib/twitter/request.rb
Expand Up @@ -6,6 +6,10 @@ def self.get(base, path, options={})
new(base, :get, path, options).perform
end

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

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

def_delegators :base, :get, :post
Expand All @@ -27,10 +31,18 @@ def uri
end

def perform
make_friendly(send(method, uri))
make_friendly(send("perform_#{method}"))
end

private
def perform_get
send(:get, uri, options[:headers])
end

def perform_post
send(:post, uri, options[:body], options[:headers])
end

def make_friendly(response)
mash(parse(response))
end
Expand Down
4 changes: 4 additions & 0 deletions test/test_helper.rb
Expand Up @@ -22,3 +22,7 @@ def fixture_file(filename)
def stub_get(url, filename)
FakeWeb.register_uri(:get, "http://twitter.com:80#{url}", :string => fixture_file(filename))
end

def stub_post(url, filename)
FakeWeb.register_uri(:post, "http://twitter.com:80#{url}", :string => fixture_file(filename))
end
7 changes: 7 additions & 0 deletions test/twitter/base_test.rb
Expand Up @@ -53,6 +53,13 @@ class BaseTest < Test::Unit::TestCase
status.user.name.should == 'John Nunemaker'
status.id.should == 1441588944
end

should "be able to update status" do
stub_post('/statuses/update.json', 'status.json')
status = @twitter.update('Rob Dyrdek is the funniest man alive. That is all.')
status.user.name.should == 'John Nunemaker'
status.text.should == 'Rob Dyrdek is the funniest man alive. That is all.'
end
end
end
end
46 changes: 44 additions & 2 deletions test/twitter/request_test.rb
Expand Up @@ -30,7 +30,7 @@ class RequestTest < Test::Unit::TestCase
context "performing request for collection" do
setup do
response = mock('response', :body => fixture_file('user_timeline.json'))
@base.expects(:get).with('/statuses/user_timeline.json?since_id=1234').returns(response)
@base.expects(:get).returns(response)
@object = @request.perform
end

Expand All @@ -44,7 +44,7 @@ class RequestTest < Test::Unit::TestCase
context "performing a request for a single object" do
setup do
response = mock('response', :body => fixture_file('status.json'))
@base.expects(:get).with('/statuses/user_timeline.json?since_id=1234').returns(response)
@base.expects(:get).returns(response)
@object = @request.perform
end

Expand All @@ -67,5 +67,47 @@ class RequestTest < Test::Unit::TestCase
request.uri.should == '/statuses/user_timeline.json'
end
end

should "have get shortcut to initialize and perform all in one" do
Twitter::Request.any_instance.expects(:perform).returns(nil)
Twitter::Request.get(@base, '/foo')
end

should "allow setting query string and headers" do
response = mock('response', :body => '')
@base.expects(:get).with('/statuses/friends_timeline.json?since_id=1234', {'Foo' => 'Bar'}).returns(response)
Twitter::Request.get(@base, '/statuses/friends_timeline.json?since_id=1234', :headers => {'Foo' => 'Bar'})
end
end

context "new post request" do
setup do
@base = mock('twitter base')
@request = Twitter::Request.new(@base, :post, '/statuses/update.json', {:body => {:status => 'Woohoo!'}})
end

should "allow setting body and headers" do
response = mock('response', :body => '')
@base.expects(:post).with('/statuses/update.json', {:status => 'Woohoo!'}, {'Foo' => 'Bar'}).returns(response)
Twitter::Request.post(@base, '/statuses/update.json', :body => {:status => 'Woohoo!'}, :headers => {'Foo' => 'Bar'})
end

context "performing request" do
setup do
response = mock('response', :body => fixture_file('status.json'))
@base.expects(:post).returns(response)
@object = @request.perform
end

should "return a mash of the object" do
@object.text.should == 'Rob Dyrdek is the funniest man alive. That is all.'
end
end

should "have post shortcut to initialize and perform all in one" do
Twitter::Request.any_instance.expects(:perform).returns(nil)
Twitter::Request.post(@base, '/foo')
end
end

end

0 comments on commit 00eebab

Please sign in to comment.