Permalink
Browse files

Reclassify display_uris as Strings.

  • Loading branch information...
1 parent d7f53d2 commit 17017643bbc180819937d8a0e3b267271c97d49a @michaelherold michaelherold committed Oct 1, 2013
View
@@ -42,6 +42,22 @@ def object_attr_reader(klass, key1, key2=nil)
define_predicate_method(key1)
end
+ # Define display URI methods from attributes
+ #
+ # @param attrs [Array, Symbol]
+ def display_uri_attr_reader(*attrs)
+ for uri_key in attrs
+ array = uri_key.to_s.split("_")
+ index = array.index("uri")
+ array[index] = "url"
+ url_key = array.join("_").to_sym
+ define_display_uri_method(uri_key, url_key)
+ define_predicate_method(uri_key, url_key)
+ alias_method(url_key, uri_key)
+ alias_method("#{url_key}?", "#{uri_key}?")
+ end
+ end
+
# Define URI methods from attributes
#
# @param attrs [Array, Symbol]
@@ -60,6 +76,17 @@ def uri_attr_reader(*attrs)
private
+ # Dynamically define a method for a display URI
+ #
+ # @param key1 [Symbol]
+ # @param key2 [Symbol]
+ def define_display_uri_method(key1, key2)
+ define_method(key1) do
+ @attrs[key2] if @attrs[key2]
+ end
+ memoize(key1)
+ end
+
# Dynamically define a method for a URI
#
# @param key1 [Symbol]
@@ -3,7 +3,8 @@
module Twitter
class Entity
class URI < Twitter::Entity
- uri_attr_reader :display_uri, :expanded_uri, :uri
+ uri_attr_reader :expanded_uri, :uri
+ display_uri_attr_reader :display_uri
end
Uri = URI
@@ -4,7 +4,8 @@ module Twitter
module Media
class Photo < Twitter::Identity
attr_reader :indices
- uri_attr_reader :display_uri, :expanded_uri, :media_uri, :media_uri_https, :uri
+ uri_attr_reader :expanded_uri, :media_uri, :media_uri_https, :uri
+ display_uri_attr_reader :display_uri
# Returns an array of photo sizes
#
@@ -3,14 +3,14 @@
describe Twitter::Entity::URI do
describe "#display_uri" do
- it "returns a URI when the display_url is set" do
+ it "returns a String when the display_url is set" do
uri = Twitter::Entity::URI.new(:display_url => "https://github.com/sferik")
- expect(uri.display_uri).to be_a URI
+ expect(uri.display_uri).to be_a String
expect(uri.display_uri.to_s).to eq("https://github.com/sferik")
end
- it "returns a URI when the display_url is set to a truncated version" do
+ it "returns a String when the display_url is set to a truncated version" do
uri = Twitter::Entity::URI.new(:display_url => "gawker.com/louis-c-k-s-ex…")
- expect(uri.display_uri).to be_a URI
+ expect(uri.display_uri).to be_a String
expect(uri.display_uri.to_s).to eq("gawker.com/louis-c-k-s-ex…")
end
it "returns nil when the display_url is not set" do
@@ -33,9 +33,9 @@
end
describe "#display_uri" do
- it "returns a URI when the display_url is set" do
+ it "returns a String when the display_url is set" do
photo = Twitter::Media::Photo.new(:id => 1, :display_url => "http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png")
- expect(photo.display_uri).to be_a URI
+ expect(photo.display_uri).to be_a String
expect(photo.display_uri.to_s).to eq("http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png")
end
it "returns nil when the display_url is not set" do
@@ -298,7 +298,7 @@
expect(tweet.uris).to be_an Array
expect(tweet.uris.first).to be_a Twitter::Entity::URI
expect(tweet.uris.first.indices).to eq([10, 33])
- expect(tweet.uris.first.display_uri).to be_a URI
+ expect(tweet.uris.first.display_uri).to be_a String
expect(tweet.uris.first.display_uri.to_s).to eq("example.com/expanded")
end
it "is empty when not set" do

0 comments on commit 1701764

Please sign in to comment.