Permalink
Browse files

Add #url methods Twitter::List, Twitter::Tweet, and Twitter::User

Closes #431.
  • Loading branch information...
sferik committed Jul 28, 2013
1 parent a4ef3ac commit a89ec0f4e81097cc303b6c204e0375eb57ffd614
View
@@ -278,7 +278,7 @@ To prevent such errors, you may have introduced checks for the truthiness of
the response, for example:
```ruby
-status = client.status(55709764298092545)
+status = Twitter.status(55709764298092545)
if status.place
# Do something with the Twitter::Place object
elsif status.geo
@@ -288,18 +288,37 @@ end
In version 5, all such methods will return a `Twitter::NullObject` instead of
`nil`. This should prevent `NoMethodError` but may result in unexpected
behavior if you have truthiness checks in place, since everything is truthy in
-Ruby execpt `false` and `nil`. For these cases, there are now predicate
+Ruby except `false` and `nil`. For these cases, there are now predicate
methods:
```ruby
-status = client.status(55709764298092545)
+status = Twitter.status(55709764298092545)
if status.place?
# Do something with the Twitter::Place object
elsif status.geo?
# Do something with the Twitter::Geo object
end
```
+### URL methods
+`Twitter::List`, `Twitter::Tweet`, and `Twitter::User` objects all have `#url`
+methods, which generate an HTTPS URL to twitter.com. You may specify a
+different protocol by passing it to the `#url` method. For example:
+
+```ruby
+status = Twitter.status(55709764298092545)
+status.url #=> https://twitter.com/sferik/status/55709764298092545
+status.url("http") #=> http://twitter.com/sferik/status/55709764298092545
+```
+
+`Twitter::User` previously had a method called `#url`, which returned the URL
+to the user's website. The URL is now accessible via the `#website` method.
+
+
+These methods are aliased to `#uri`, for users who prefer that nomenclature.
+This clobbers the `Twitter::List#uri` method, which previously returned the
+list's path (not a full URI).
+
## Configuration
Twitter API v1.1 requires you to authenticate via OAuth, so you'll need to
[register your application with Twitter][register]. Once you've registered an
View
@@ -5,7 +5,25 @@ module Twitter
class List < Twitter::Identity
include Twitter::Creatable
attr_reader :description, :following, :full_name, :member_count,
- :mode, :name, :slug, :subscriber_count, :uri
+ :mode, :name, :slug, :subscriber_count
+
+ # @return [String] The URL to the list members.
+ def members_url(protocol="https")
+ "#{protocol}://twitter.com/#{user.screen_name}/#{slug}/members"
+ end
+ alias members_uri members_url
+
+ # @return [String] The URL to the list subscribers.
+ def subscribers_url(protocol="https")
+ "#{protocol}://twitter.com/#{user.screen_name}/#{slug}/subscribers"
+ end
+ alias subscribers_uri subscribers_url
+
+ # @return [String] The URL to the list.
+ def url(protocol="https")
+ "#{protocol}://twitter.com/#{user.screen_name}/#{slug}"
+ end
+ alias uri url
# @return [Twitter::User, Twitter::NullObject]
def user
View
@@ -4,5 +4,6 @@ module Twitter
class OEmbed < Twitter::Base
attr_reader :author_name, :author_url, :cache_age, :height, :html,
:provider_name, :provider_url, :type, :url, :version, :width
+ alias uri url
end
end
View
@@ -3,6 +3,7 @@
module Twitter
class Place < Twitter::Identity
attr_reader :attributes, :country, :full_name, :name, :url, :woeid
+ alias uri url
alias woe_id woeid
# @return [Twitter::Geo, Twitter::NullObject]
View
@@ -3,6 +3,7 @@
module Twitter
class Trend < Twitter::Base
attr_reader :events, :name, :promoted_content, :query, :url
+ alias uri url
# @param other [Twitter::Trend]
# @return [Boolean]
View
@@ -114,6 +114,12 @@ def symbols
@symbols ||= entities(Twitter::Entity::Symbol, :symbols)
end
+ # @return [String] The URL to the tweet.
+ def url(protocol="https")
+ "#{protocol}://twitter.com/#{user.screen_name}/status/#{id}"
+ end
+ alias uri url
+
# @note Must include entities in your request for this method to work
# @return [Array<Twitter::Entity::Url>]
def urls
View
@@ -14,7 +14,7 @@ class User < Twitter::BasicUser
:profile_link_color, :profile_sidebar_border_color,
:profile_sidebar_fill_color, :profile_text_color,
:profile_use_background_image, :protected, :statuses_count, :time_zone,
- :url, :utc_offset, :verified
+ :utc_offset, :verified
alias favorite_count favourites_count
alias favoriters_count favourites_count
alias favorites_count favourites_count
@@ -97,6 +97,17 @@ def status?
alias tweet? status?
alias tweeted? status?
+ # @return [String] The URL to the user.
+ def url(protocol="https")
+ "#{protocol}://twitter.com/#{screen_name}"
+ end
+ alias uri url
+
+ # @return [String] The URL to the user's website.
+ def website
+ @attrs[:url]
+ end
+
private
def insecure_url(url)
View
@@ -42,6 +42,27 @@
end
end
+ describe "#members_url" do
+ it "returns the URL to the list members" do
+ list = Twitter::List.new(:id => 8863586, :slug => "presidents", :user => {:id => 7505382, :screen_name => "sferik"})
+ expect(list.members_url).to eq "https://twitter.com/sferik/presidents/members"
+ end
+ end
+
+ describe "#subscribers_url" do
+ it "returns the URL to the list subscribers" do
+ list = Twitter::List.new(:id => 8863586, :slug => "presidents", :user => {:id => 7505382, :screen_name => "sferik"})
+ expect(list.subscribers_url).to eq "https://twitter.com/sferik/presidents/subscribers"
+ end
+ end
+
+ describe "#url" do
+ it "returns the URL to the list" do
+ list = Twitter::List.new(:id => 8863586, :slug => "presidents", :user => {:id => 7505382, :screen_name => "sferik"})
+ expect(list.url).to eq "https://twitter.com/sferik/presidents"
+ end
+ end
+
describe "#user" do
it "returns a User when user is set" do
list = Twitter::List.new(:id => 8863586, :user => {:id => 7505382})
View
@@ -305,18 +305,25 @@
end
end
+ describe "#url" do
+ it "returns the URL to the tweet" do
+ tweet = Twitter::Tweet.new(:id => 28669546014, :user => {:id => 7505382, :screen_name => "sferik"})
+ expect(tweet.url).to eq "https://twitter.com/sferik/status/28669546014"
+ end
+ end
+
describe "#user" do
it "returns a User when user is set" do
- user = Twitter::Tweet.new(:id => 28669546014, :user => {:id => 7505382}).user
- expect(user).to be_a Twitter::User
+ tweet = Twitter::Tweet.new(:id => 28669546014, :user => {:id => 7505382})
+ expect(tweet.user).to be_a Twitter::User
end
it "returns nil when user is not set" do
- user = Twitter::Tweet.new(:id => 28669546014).user
- expect(user).to be_nil
+ tweet = Twitter::Tweet.new(:id => 28669546014)
+ expect(tweet.user).to be_nil
end
it "has a status when status is set" do
- user = Twitter::Tweet.new(:id => 28669546014, :text => "Tweet text.", :user => {:id => 7505382}).user
- expect(user.status).to be_a Twitter::Tweet
+ tweet = Twitter::Tweet.new(:id => 28669546014, :text => "Tweet text.", :user => {:id => 7505382})
+ expect(tweet.user.status).to be_a Twitter::Tweet
end
end
View
@@ -285,4 +285,18 @@
end
end
+ describe "#url" do
+ it "returns the URL to the user" do
+ user = Twitter::User.new(:id => 7505382, :screen_name => "sferik")
+ expect(user.url).to eq "https://twitter.com/sferik"
+ end
+ end
+
+ describe "#website" do
+ it "returns the website of the user" do
+ user = Twitter::User.new(:id => 7505382, :url => "https://github.com/sferik")
+ expect(user.website).to eq "https://github.com/sferik"
+ end
+ end
+
end

0 comments on commit a89ec0f

Please sign in to comment.