Skip to content

Commit

Permalink
add support for video entities
Browse files Browse the repository at this point in the history
  • Loading branch information
stve committed Jan 28, 2015
1 parent bf6a90d commit 158193f
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 144 deletions.
31 changes: 31 additions & 0 deletions lib/twitter/media/video.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'twitter/identity'
require 'twitter/media/video_info'

module Twitter
module Media
class Video < Twitter::Identity
# @return [Array<Integer>]
attr_reader :indices
display_uri_attr_reader
uri_attr_reader :expanded_uri, :media_uri, :media_uri_https, :uri

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

# Returns video info
#
# @return [Twitter::Media::VideoInfo]
def video_info
VideoInfo.new(@attrs[:video_info]) unless @attrs[:video_info].nil?
end
memoize :video_info
end
end
end
23 changes: 23 additions & 0 deletions lib/twitter/media/video_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'twitter/variant'

module Twitter
module Media
class VideoInfo < Twitter::Base
# @return [Array<Integer]
attr_reader :aspect_ratio

# @return [Integer]
attr_reader :duration_millis

# Returns an array of video variants
#
# @return [Array<Twitter::Variant>]
def variants
@attrs.fetch(:variants, []).map do |variant|
Variant.new(variant)
end
end
memoize :variants
end
end
end
1 change: 1 addition & 0 deletions lib/twitter/media_factory.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'twitter/factory'
require 'twitter/media/photo'
require 'twitter/media/video'

module Twitter
class MediaFactory < Twitter::Factory
Expand Down
12 changes: 12 additions & 0 deletions lib/twitter/variant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'twitter/base'

module Twitter
class Variant < Twitter::Base
# @return [Integer]
attr_reader :bitrate

# @return [String]
attr_reader :content_type
uri_attr_reader :uri
end
end
2 changes: 2 additions & 0 deletions spec/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
require 'timecop'
require 'webmock/rspec'

require_relative 'support/media_object_examples'

WebMock.disable_net_connect!(allow: 'coveralls.io')

RSpec.configure do |config|
Expand Down
146 changes: 146 additions & 0 deletions spec/support/media_object_examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
shared_examples_for 'a Twitter::Media object' do
describe '#==' do
it 'returns true when objects IDs are the same' do
media = described_class.new(id: 1)
other = described_class.new(id: 1)
expect(media == other).to be true
end
it 'returns false when objects IDs are different' do
media = described_class.new(id: 1)
other = described_class.new(id: 2)
expect(media == other).to be false
end
it 'returns false when classes are different' do
media = described_class.new(id: 1)
other = Twitter::Identity.new(id: 1)
expect(media == other).to be false
end
end

describe '#sizes' do
it 'returns a hash of Sizes when sizes is set' do
sizes = described_class.new(id: 110_102_452_988_157_952, 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
expect(sizes).to be_a Hash
expect(sizes[:small]).to be_a Twitter::Size
end
it 'is empty when sizes is not set' do
sizes = described_class.new(id: 110_102_452_988_157_952).sizes
expect(sizes).to be_empty
end
end

describe '#display_uri' do
it 'returns a String when the display_url is set' do
photo = Twitter::Media::Photo.new(id: 1, display_url: 'example.com/expanded...')
expect(photo.display_uri).to be_a String
expect(photo.display_uri).to eq('example.com/expanded...')
end
it 'returns nil when the display_url is not set' do
photo = Twitter::Media::Photo.new(id: 1)
expect(photo.display_uri).to be_nil
end
end

describe '#display_uri?' do
it 'returns true when the display_url is set' do
photo = Twitter::Media::Photo.new(id: 1, display_url: 'example.com/expanded...')
expect(photo.display_uri?).to be true
end
it 'returns false when the display_url is not set' do
photo = Twitter::Media::Photo.new(id: 1)
expect(photo.display_uri?).to be false
end
end

describe '#expanded_uri' do
it 'returns a URI when the expanded_url is set' do
media = described_class.new(id: 1, expanded_url: 'http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png')
expect(media.expanded_uri).to be_an Addressable::URI
expect(media.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
media = described_class.new(id: 1)
expect(media.expanded_uri).to be_nil
end
end

describe '#expanded_uri?' do
it 'returns true when the expanded_url is set' do
media = described_class.new(id: 1, expanded_url: 'http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png')
expect(media.expanded_uri?).to be true
end
it 'returns false when the expanded_url is not set' do
media = described_class.new(id: 1)
expect(media.expanded_uri?).to be false
end
end

describe '#media_uri' do
it 'returns a URI when the media_url is set' do
media = described_class.new(id: 1, media_url: 'http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png')
expect(media.media_uri).to be_an Addressable::URI
expect(media.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
media = described_class.new(id: 1)
expect(media.media_uri).to be_nil
end
end

describe '#media_uri?' do
it 'returns true when the media_url is set' do
media = described_class.new(id: 1, media_url: 'http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png')
expect(media.media_uri?).to be true
end
it 'returns false when the media_url is not set' do
media = described_class.new(id: 1)
expect(media.media_uri?).to be false
end
end

describe '#media_uri_https' do
it 'returns a URI when the media_url_https is set' do
media = described_class.new(id: 1, media_url_https: 'http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png')
expect(media.media_uri_https).to be_an Addressable::URI
expect(media.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
media = described_class.new(id: 1)
expect(media.media_uri_https).to be_nil
end
end

describe '#media_uri_https?' do
it 'returns true when the media_url_https is set' do
media = described_class.new(id: 1, media_url_https: 'http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png')
expect(media.media_uri_https?).to be true
end
it 'returns false when the media_url_https is not set' do
media = described_class.new(id: 1)
expect(media.media_uri_https?).to be false
end
end

describe '#uri' do
it 'returns a URI when the url is set' do
media = described_class.new(id: 1, url: 'http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png')
expect(media.uri).to be_an Addressable::URI
expect(media.uri.to_s).to eq('http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png')
end
it 'returns nil when the url is not set' do
media = described_class.new(id: 1)
expect(media.uri).to be_nil
end
end

describe '#uri?' do
it 'returns true when the url is set' do
media = described_class.new(id: 1, url: 'http://pbs.twimg.com/media/BQD6MPOCEAAbCH0.png')
expect(media.uri?).to be true
end
it 'returns false when the url is not set' do
media = described_class.new(id: 1)
expect(media.uri?).to be false
end
end
end
145 changes: 1 addition & 144 deletions spec/twitter/media/photo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,148 +2,5 @@
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)
other = Twitter::Media::Photo.new(id: 1)
expect(photo == other).to 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)
expect(photo == other).to be false
end
it 'returns false when classes are different' do
photo = Twitter::Media::Photo.new(id: 1)
other = Twitter::Identity.new(id: 1)
expect(photo == other).to be false
end
end

describe '#sizes' do
it 'returns a hash of Sizes when sizes is set' do
sizes = Twitter::Media::Photo.new(id: 110_102_452_988_157_952, 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
expect(sizes).to be_a Hash
expect(sizes[:small]).to be_a Twitter::Size
end
it 'is empty when sizes is not set' do
sizes = Twitter::Media::Photo.new(id: 110_102_452_988_157_952).sizes
expect(sizes).to be_empty
end
end

describe '#display_uri' do
it 'returns a String when the display_url is set' do
photo = Twitter::Media::Photo.new(id: 1, display_url: 'example.com/expanded...')
expect(photo.display_uri).to be_a String
expect(photo.display_uri).to eq('example.com/expanded...')
end
it 'returns nil when the display_url is not set' do
photo = Twitter::Media::Photo.new(id: 1)
expect(photo.display_uri).to be_nil
end
end

describe '#display_uri?' do
it 'returns true when the display_url is set' do
photo = Twitter::Media::Photo.new(id: 1, display_url: 'example.com/expanded...')
expect(photo.display_uri?).to be true
end
it 'returns false when the display_url is not set' do
photo = Twitter::Media::Photo.new(id: 1)
expect(photo.display_uri?).to be false
end
end

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_an 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
photo = Twitter::Media::Photo.new(id: 1)
expect(photo.expanded_uri).to be_nil
end
end

describe '#expanded_uri?' do
it 'returns true 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 true
end
it 'returns false when the expanded_url is not set' do
photo = Twitter::Media::Photo.new(id: 1)
expect(photo.expanded_uri?).to be false
end
end

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_an 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
photo = Twitter::Media::Photo.new(id: 1)
expect(photo.media_uri).to be_nil
end
end

describe '#media_uri?' do
it 'returns true 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 true
end
it 'returns false when the media_url is not set' do
photo = Twitter::Media::Photo.new(id: 1)
expect(photo.media_uri?).to be false
end
end

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_an 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
photo = Twitter::Media::Photo.new(id: 1)
expect(photo.media_uri_https).to be_nil
end
end

describe '#media_uri_https?' do
it 'returns true 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 true
end
it 'returns false when the media_url_https is not set' do
photo = Twitter::Media::Photo.new(id: 1)
expect(photo.media_uri_https?).to be false
end
end

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_an 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
photo = Twitter::Media::Photo.new(id: 1)
expect(photo.uri).to be_nil
end
end

describe '#uri?' do
it 'returns true 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 true
end
it 'returns false when the url is not set' do
photo = Twitter::Media::Photo.new(id: 1)
expect(photo.uri?).to be false
end
end
it_behaves_like 'a Twitter::Media object'
end
Loading

0 comments on commit 158193f

Please sign in to comment.