Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Addressable::URI everywhere #492

Merged
merged 2 commits into from
Nov 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ These methods are aliased to `#url` for users who prefer that nomenclature.
`Twitter::User` previously had a `#url` method, which returned the user's
website. This URI is now available via the `#website` method.

All `#uri` methods now return `URI` objects instead of strings. To convert a
`URI` object to a string, call `#to_s` on it.
All `#uri` methods now return `Addressable::URI` objects instead of strings. To convert an
`Addressable::URI` object to a string, call `#to_s` on it.

## Configuration
Twitter API v1.1 requires you to authenticate via OAuth, so you'll need to
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def display_uri_attr_reader
# @param key2 [Symbol]
def define_uri_method(key1, key2)
define_method(key1) do ||
URI.parse(@attrs[key2]) if @attrs[key2]
Addressable::URI.parse(@attrs[key2]) if @attrs[key2]
end
memoize(key1)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def validate_credential_type!
end

def oauth_auth_header(method, uri, params={})
uri = URI.parse(uri)
uri = Addressable::URI.parse(uri)
SimpleOAuth::Header.new(method, uri, params, credentials)
end

Expand Down
12 changes: 6 additions & 6 deletions lib/twitter/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ class List < Twitter::Identity
:mode, :name, :slug, :subscriber_count
object_attr_reader :User, :user

# @return [URI] The URI to the list members.
# @return [Addressable::URI] The URI to the list members.
def members_uri
URI.parse("https://twitter.com/#{user.screen_name}/#{slug}/members")
Addressable::URI.parse("https://twitter.com/#{user.screen_name}/#{slug}/members")
end
memoize :members_uri
alias members_url members_uri

# @return [URI] The URI to the list subscribers.
# @return [Addressable::URI] The URI to the list subscribers.
def subscribers_uri
URI.parse("https://twitter.com/#{user.screen_name}/#{slug}/subscribers")
Addressable::URI.parse("https://twitter.com/#{user.screen_name}/#{slug}/subscribers")
end
memoize :subscribers_uri
alias subscribers_url subscribers_uri

# @return [URI] The URI to the list.
# @return [Addressable::URI] The URI to the list.
def uri
URI.parse("https://twitter.com/#{user.screen_name}/#{slug}")
Addressable::URI.parse("https://twitter.com/#{user.screen_name}/#{slug}")
end
memoize :uri
alias url uri
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/rest/api/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def block?(user, options={})
user_id = case user
when Integer
user
when String, URI
when String, URI, Addressable::URI
user(user).id
when Twitter::User
user.id
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/rest/api/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def merge_user!(hash, user, prefix=nil)
else
set_compound_key("screen_name", user, hash, prefix)
end
when URI
when URI, Addressable::URI
set_compound_key("screen_name", user.path.split("/").last, hash, prefix)
when Twitter::User
set_compound_key("user_id", user.id, hash, prefix)
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/tweet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def symbols

# @return [String] The URL to the tweet.
def uri
URI.parse("https://twitter.com/#{user.screen_name}/status/#{id}")
Addressable::URI.parse("https://twitter.com/#{user.screen_name}/status/#{id}")
end
memoize :uri
alias url uri
Expand Down
6 changes: 3 additions & 3 deletions lib/twitter/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ def profile_image_uri?

# @return [String] The URL to the user.
def uri
URI.parse("https://twitter.com/#{screen_name}")
Addressable::URI.parse("https://twitter.com/#{screen_name}")
end
memoize :uri
alias url uri

# @return [String] The URL to the user's website.
def website
URI.parse(@attrs[:url]) if @attrs[:url]
Addressable::URI.parse(@attrs[:url]) if @attrs[:url]
end
memoize :website

Expand All @@ -128,7 +128,7 @@ def website?
private

def parse_encoded_uri(uri)
URI.parse(URI.encode(uri))
Addressable::URI.parse(URI.encode(uri))
end

def insecure_uri(uri)
Expand Down
4 changes: 2 additions & 2 deletions spec/twitter/entity/uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
describe "#expanded_uri" do
it "returns a URI when the expanded_url is set" do
uri = Twitter::Entity::URI.new(:expanded_url => "https://github.com/sferik")
expect(uri.expanded_uri).to be_a URI
expect(uri.expanded_uri).to be_a Addressable::URI
expect(uri.expanded_uri.to_s).to eq("https://github.com/sferik")
end
it "returns nil when the expanded_url is not set" do
Expand All @@ -52,7 +52,7 @@
describe "#uri" do
it "returns a URI when the url is set" do
uri = Twitter::Entity::URI.new(:url => "https://github.com/sferik")
expect(uri.uri).to be_a URI
expect(uri.uri).to be_a Addressable::URI
expect(uri.uri.to_s).to eq("https://github.com/sferik")
end
it "returns nil when the url is not set" do
Expand Down
8 changes: 4 additions & 4 deletions spec/twitter/media/photo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
describe "#expanded_uri" do
it "returns a URI when the expanded_url is set" do
photo = Twitter::Media::Photo.new(:id => 1, :expanded_url => "http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png")
expect(photo.expanded_uri).to be_a URI
expect(photo.expanded_uri).to be_a Addressable::URI
expect(photo.expanded_uri.to_s).to eq("http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png")
end
it "returns nil when the expanded_url is not set" do
Expand All @@ -82,7 +82,7 @@
describe "#media_uri" do
it "returns a URI when the media_url is set" do
photo = Twitter::Media::Photo.new(:id => 1, :media_url => "http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png")
expect(photo.media_uri).to be_a URI
expect(photo.media_uri).to be_a Addressable::URI
expect(photo.media_uri.to_s).to eq("http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png")
end
it "returns nil when the media_url is not set" do
Expand All @@ -105,7 +105,7 @@
describe "#media_uri_https" do
it "returns a URI when the media_url_https is set" do
photo = Twitter::Media::Photo.new(:id => 1, :media_url_https => "http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png")
expect(photo.media_uri_https).to be_a URI
expect(photo.media_uri_https).to be_a Addressable::URI
expect(photo.media_uri_https.to_s).to eq("http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png")
end
it "returns nil when the media_url_https is not set" do
Expand All @@ -128,7 +128,7 @@
describe "#uri" do
it "returns a URI when the url is set" do
photo = Twitter::Media::Photo.new(:id => 1, :url => "http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png")
expect(photo.uri).to be_a URI
expect(photo.uri).to be_a Addressable::URI
expect(photo.uri.to_s).to eq("http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png")
end
it "returns nil when the url is not set" do
Expand Down
6 changes: 3 additions & 3 deletions spec/twitter/oembed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe "#author_uri" do
it "returns a URI when the author_url is set" do
oembed = Twitter::OEmbed.new(:author_url => "https://twitter.com/sferik")
expect(oembed.author_uri).to be_a URI
expect(oembed.author_uri).to be_a Addressable::URI
expect(oembed.author_uri.to_s).to eq("https://twitter.com/sferik")
end
it "returns nil when the author_uri is not set" do
Expand Down Expand Up @@ -87,7 +87,7 @@
describe "#provider_uri" do
it "returns a URI when the provider_url is set" do
oembed = Twitter::OEmbed.new(:provider_url => "http://twitter.com")
expect(oembed.provider_uri).to be_a URI
expect(oembed.provider_uri).to be_a Addressable::URI
expect(oembed.provider_uri.to_s).to eq("http://twitter.com")
end
it "returns nil when the provider_uri is not set" do
Expand Down Expand Up @@ -136,7 +136,7 @@
describe "#uri" do
it "returns a URI when the url is set" do
oembed = Twitter::OEmbed.new(:url => "https://twitter.com/twitterapi/status/133640144317198338")
expect(oembed.uri).to be_a URI
expect(oembed.uri).to be_a Addressable::URI
expect(oembed.uri.to_s).to eq("https://twitter.com/twitterapi/status/133640144317198338")
end
it "returns nil when the url is not set" do
Expand Down
2 changes: 1 addition & 1 deletion spec/twitter/place_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
describe "#uri" do
it "returns a URI when the url is set" do
place = Twitter::Place.new(:woeid => "247f43d441defc03", :url => "https://api.twitter.com/1.1/geo/id/247f43d441defc03.json")
expect(place.uri).to be_a URI
expect(place.uri).to be_a Addressable::URI
expect(place.uri.to_s).to eq("https://api.twitter.com/1.1/geo/id/247f43d441defc03.json")
end
it "returns nil when the url is not set" do
Expand Down
2 changes: 1 addition & 1 deletion spec/twitter/trend_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
describe "#uri" do
it "returns a URI when the url is set" do
trend = Twitter::Trend.new(:url => "http://twitter.com/search/?q=%23sevenwordsaftersex")
expect(trend.uri).to be_a URI
expect(trend.uri).to be_a Addressable::URI
expect(trend.uri.to_s).to eq("http://twitter.com/search/?q=%23sevenwordsaftersex")
end
it "returns nil when the url is not set" do
Expand Down
19 changes: 18 additions & 1 deletion spec/twitter/tweet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,29 @@
Twitter::Tweet.new(:id => 28669546014).urls
expect($stderr.string).to match(/To get urls, you must pass `:include_entities => true` when requesting the Twitter::Tweet\./)
end

it "can handle strange urls" do
urls_array = [
{
:url => "http://with_underscore.example.com/t.co",
:expanded_url => "http://with_underscore.example.com/expanded",
:display_url => "with_underscore.example.com/expanded…",
:indices => [10, 33],
}
]
tweet = Twitter::Tweet.new(:id => 28669546014, :entities => {:urls => urls_array})
uri = tweet.uris.first
expect{ uri.url }.to_not raise_error
expect{ uri.expanded_url }.to_not raise_error
expect{ uri.display_url }.to_not raise_error
end

end

describe "#uri" do
it "returns the URI to the tweet" do
tweet = Twitter::Tweet.new(:id => 28669546014, :user => {:id => 7505382, :screen_name => "sferik"})
expect(tweet.uri).to be_a URI
expect(tweet.uri).to be_a Addressable::URI
expect(tweet.uri.to_s).to eq("https://twitter.com/sferik/status/28669546014")
end
end
Expand Down
20 changes: 10 additions & 10 deletions spec/twitter/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@
describe "#profile_banner_uri" do
it "accepts utf8 urls" do
user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581©_normal.png")
expect(user.profile_banner_uri).to be_a URI
expect(user.profile_banner_uri).to be_a Addressable::URI
end
it "returns a URI when profile_banner_url is set" do
user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581")
expect(user.profile_banner_uri).to be_a URI
expect(user.profile_banner_uri).to be_a Addressable::URI
end
it "returns nil when profile_banner_uri is not set" do
user = Twitter::User.new(:id => 7505382)
Expand Down Expand Up @@ -117,11 +117,11 @@
describe "#profile_banner_uri_https" do
it "accepts utf8 urls" do
user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581©_normal.png")
expect(user.profile_banner_uri_https).to be_a URI
expect(user.profile_banner_uri_https).to be_a Addressable::URI
end
it "returns a URI when profile_banner_url is set" do
user = Twitter::User.new(:id => 7505382, :profile_banner_url => "https://si0.twimg.com/profile_banners/7505382/1348266581")
expect(user.profile_banner_uri_https).to be_a URI
expect(user.profile_banner_uri_https).to be_a Addressable::URI
end
it "returns nil when created_at is not set" do
user = Twitter::User.new(:id => 7505382)
Expand Down Expand Up @@ -177,11 +177,11 @@
describe "#profile_image_uri" do
it "accepts utf8 urls" do
user = Twitter::User.new(:id => 7505382, :profile_image_url_https => "https://si0.twimg.com/profile_images/7505382/1348266581©_normal.png")
expect(user.profile_image_uri).to be_a URI
expect(user.profile_image_uri).to be_a Addressable::URI
end
it "returns a URI when profile_image_url_https is set" do
user = Twitter::User.new(:id => 7505382, :profile_image_url_https => "https://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png")
expect(user.profile_image_uri).to be_a URI
expect(user.profile_image_uri).to be_a Addressable::URI
end
it "returns nil when created_at is not set" do
user = Twitter::User.new(:id => 7505382)
Expand Down Expand Up @@ -222,11 +222,11 @@
describe "#profile_image_uri_https" do
it "accepts utf8 urls" do
user = Twitter::User.new(:id => 7505382, :profile_image_url_https => "https://si0.twimg.com/profile_images/7505382/1348266581©_normal.png")
expect(user.profile_image_uri_https).to be_a URI
expect(user.profile_image_uri_https).to be_a Addressable::URI
end
it "returns a URI when profile_image_url_https is set" do
user = Twitter::User.new(:id => 7505382, :profile_image_url_https => "https://a0.twimg.com/profile_images/1759857427/image1326743606_normal.png")
expect(user.profile_image_uri_https).to be_a URI
expect(user.profile_image_uri_https).to be_a Addressable::URI
end
it "returns nil when created_at is not set" do
user = Twitter::User.new(:id => 7505382)
Expand Down Expand Up @@ -305,15 +305,15 @@
describe "#uri" do
it "returns the URI to the user" do
user = Twitter::User.new(:id => 7505382, :screen_name => "sferik")
expect(user.uri).to be_a URI
expect(user.uri).to be_a Addressable::URI
expect(user.uri.to_s).to eq("https://twitter.com/sferik")
end
end

describe "#website" do
it "returns a URI when the url is set" do
user = Twitter::User.new(:id => 7505382, :url => "https://github.com/sferik")
expect(user.website).to be_a URI
expect(user.website).to be_a Addressable::URI
expect(user.website.to_s).to eq("https://github.com/sferik")
end
it "returns nil when the url is not set" do
Expand Down