Skip to content

Commit

Permalink
Move Photo class under Media namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jul 18, 2012
1 parent 059cc55 commit 979ed71
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 50 deletions.
24 changes: 13 additions & 11 deletions lib/twitter/media/photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
require 'twitter/size'

module Twitter
class Photo < Twitter::Identity
attr_reader :display_url, :expanded_url, :indices, :media_url,
:media_url_https, :url
module Media
class Photo < Twitter::Identity
attr_reader :display_url, :expanded_url, :indices, :media_url,
:media_url_https, :url

# Returns an array of photo sizes
#
# @return [Array<Twitter::Size>]
def sizes
@sizes ||= Array(@attrs[:sizes]).inject({}) do |object, (key, value)|
object[key] = Twitter::Size.fetch_or_new(value)
object
# Returns an array of photo sizes
#
# @return [Array<Twitter::Size>]
def sizes
@sizes ||= Array(@attrs[:sizes]).inject({}) do |object, (key, value)|
object[key] = Twitter::Size.fetch_or_new(value)
object
end
end
end

end
end
end
4 changes: 2 additions & 2 deletions lib/twitter/media_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class MediaFactory
#
# @param attrs [Hash]
# @raise [ArgumentError] Error raised when supplied argument is missing a :type key.
# @return [Twitter::Photo]
# @return [Twitter::Media::Photo]
def self.fetch_or_new(attrs={})
if type = attrs.delete(:type)
Twitter.const_get(type.camelize.to_sym).fetch_or_new(attrs)
Twitter::Media.const_get(type.camelize.to_sym).fetch_or_new(attrs)
else
raise ArgumentError, "argument must have a :type key"
end
Expand Down
35 changes: 35 additions & 0 deletions spec/twitter/media/photo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'helper'

describe Twitter::Media::Photo do

describe "#==" do
it "returns true when objects IDs are the same" do
photo = Twitter::Media::Photo.new(:id => 1, :url => "foo")
other = Twitter::Media::Photo.new(:id => 1, :url => "bar")
(photo == other).should be_true
end
it "returns false when objects IDs are different" do
photo = Twitter::Media::Photo.new(:id => 1)
other = Twitter::Media::Photo.new(:id => 2)
(photo == other).should be_false
end
it "returns false when classes are different" do
photo = Twitter::Media::Photo.new(:id => 1)
other = Twitter::Identity.new(:id => 1)
(photo == other).should be_false
end
end

describe "#sizes" do
it "returns a hash of Sizes when sizes is set" do
sizes = Twitter::Media::Photo.new(:id => 110102452988157952, :sizes => {:small => {:h => 226, :w => 340, :resize => 'fit'}, :large => {:h => 466, :w => 700, :resize => 'fit'}, :medium => {:h => 399, :w => 600, :resize => 'fit'}, :thumb => {:h => 150, :w => 150, :resize => 'crop'}}).sizes
sizes.should be_a Hash
sizes[:small].should be_a Twitter::Size
end
it "is empty when sizes is not set" do
sizes = Twitter::Media::Photo.new(:id => 110102452988157952).sizes
sizes.should be_empty
end
end

end
2 changes: 1 addition & 1 deletion spec/twitter/media_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe ".new" do
it "generates a Photo" do
media = Twitter::MediaFactory.fetch_or_new(:id => 1, :type => 'photo')
media.should be_a Twitter::Photo
media.should be_a Twitter::Media::Photo
end
it "raises an ArgumentError when type is not specified" do
lambda do
Expand Down
35 changes: 0 additions & 35 deletions spec/twitter/photo_spec.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/twitter/status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
it "returns media" do
media = Twitter::Status.new(:id => 28669546014, :entities => {:media => [{:id => 1, :type => 'photo'}]}).media
media.should be_an Array
media.first.should be_a Twitter::Photo
media.first.should be_a Twitter::Media::Photo
end
it "returns nil when not set" do
media = Twitter::Status.new(:id => 28669546014).media
Expand Down

0 comments on commit 979ed71

Please sign in to comment.