Skip to content

Commit

Permalink
Add Urls#resolve method
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Sep 1, 2011
1 parent 4455aec commit b8d92b9
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/twitter/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Client < API
require 'twitter/client/timeline'
require 'twitter/client/trends'
require 'twitter/client/tweets'
require 'twitter/client/urls'
require 'twitter/client/user'
require 'twitter/client/utils'

Expand All @@ -52,6 +53,7 @@ class Client < API
include Twitter::Client::Timeline
include Twitter::Client::Trends
include Twitter::Client::Tweets
include Twitter::Client::Urls
include Twitter::Client::User
end
end
3 changes: 2 additions & 1 deletion lib/twitter/client/tweets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ def update(status, options={})

# Updates with media the authenticating user's status
#
# @format :json, :xml
# @note A status update with text/media identical to the authenticating user's current status will NOT be ignored
# @authenticated true
# @rate_limited false
# @response_format `json`
# @response_format `xml`
# @param status [String] The text of your status update, up to 140 characters.
# @param media [File] A File object with your picture (PNG, JPEG or GIF)
# @param options [Hash] A customizable set of options.
Expand Down
23 changes: 23 additions & 0 deletions lib/twitter/client/urls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Twitter
class Client
# Defines methods related to URLs
module Urls
# Returns the canonical version of a URL shortened by Twitter
#
# @note Undocumented
# @rate_limited Yes
# @requires_authentication Yes
# @response_format `json`
# @param urls [String] A list of shortened URLs
# @param options [Hash] A customizable set of options.
# @return [Hashie::Mash] A hash of URLs with the shortened URLs as the key
# @example Return the canonical version of a URL shortened by Twitter
# Twitter.resolve('http://t.co/uw5bn1w', 'http://t.co/dXvMz9i')
# Twitter.resolve(['http://t.co/uw5bn1w', 'http://t.co/dXvMz9i']) # Same as above
def resolve(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
get("urls/resolve", options.merge("urls[]" => args), :json)
end
end
end
end
8 changes: 6 additions & 2 deletions lib/twitter/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ module Connection
def connection(format=format, temp_api_endpoint=nil)
options = {
:headers => {
:accept => "application/#{format}",
:user_agent => user_agent,
'Accept' => "application/#{format}",
'User-Agent' => user_agent,
# Not sure what what the X-Phx (Phoenix?) header is for but it's
# required to access certain undocumented resources
# e.g. GET urls/resolve
'X-Phx' => 'true',
},
:proxy => proxy,
:ssl => {:verify => false},
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/resolve.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"http:\/\/t.co\/uw5bn1w":"http:\/\/www.jeanniejeannie.com\/2011\/08\/29\/the-art-of-clean-up-sorting-and-stacking-everyday-objects\/"}
32 changes: 32 additions & 0 deletions spec/twitter/client/urls_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'helper'

describe Twitter::Client do
context ".new" do
before do
@client = Twitter::Client.new
end

describe ".resolve" do

before do
stub_get("urls/resolve.json").
with(:query => {:urls => ["http://t.co/uw5bn1w"]}).
to_return(:body => fixture("resolve.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end

it "should get the correct resource" do
@client.resolve('http://t.co/uw5bn1w')
a_get("urls/resolve.json").
with(:query => {:urls => ["http://t.co/uw5bn1w"]}).
should have_been_made
end

it "should return the canonical version of a URL shortened by Twitter" do
resolve = @client.resolve('http://t.co/uw5bn1w')
resolve.should be_a Hash
resolve["http://t.co/uw5bn1w"].should == "http://www.jeanniejeannie.com/2011/08/29/the-art-of-clean-up-sorting-and-stacking-everyday-objects/"
end

end
end
end
2 changes: 1 addition & 1 deletion spec/twitter/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
context ".new" do

before do
@client = Twitter::Search.new(:consumer_key => 'CK', :consumer_secret => 'CS', :oauth_token => 'OT', :oauth_token_secret => 'OS', :endpoint => 'https://search.twitter.com/')
@client = Twitter::Search.new
end

describe ".containing" do
Expand Down

0 comments on commit b8d92b9

Please sign in to comment.