Class: MTV::Video

Inherits:
Base
  • Object
show all
Defined in:
lib/mtv/video.rb

Overview

This class represents a music video in MTV’s database.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#attribute_for_inspect, request

Constructor Details

#initialize(values = {}) ⇒ Video

Creates a new video, setting all the given hash keys as attributes.

Attributes

  • options - Possible attributes are title, artist_uri, artist_name,

    <tt>published</tt>, <tt>content</tt>, <tt>uri</tt>, <tt>updated</tt>, <tt>media_category</tt>,
    <tt>media_credits</tt>, <tt>media_description</tt>, <tt>media_duration</tt>, <tt>media_expression</tt>, 
    <tt>media_keywords</tt>, <tt>media_medium</tt>, <tt>media_player_url</tt>, <tt>media_thumbnails</tt>,
    <tt>media_title</tt>, <tt>media_type</tt>, <tt>media_url</tt>
    


138
139
140
141
142
# File 'lib/mtv/video.rb', line 138

def initialize(values={})
  super(values)
  self.uid = uri.gsub(self.class.base_url + "/video", '').delete '/' unless @uid
  self.vid = media_url.split(':').last if media_url
end

Instance Attribute Details

#artistObject

Returns the artist object for the video.

Examples

MTV::Video.search('mellow gold').first.artist
> #<MTV::Artist name: "Beck">


150
151
152
# File 'lib/mtv/video.rb', line 150

def artist
  @artist
end

#artist_nameObject

Returns the value of attribute artist_name.



4
5
6
# File 'lib/mtv/video.rb', line 4

def artist_name
  @artist_name
end

#artist_uriObject

Returns the value of attribute artist_uri.



4
5
6
# File 'lib/mtv/video.rb', line 4

def artist_uri
  @artist_uri
end

#categoryObject

Returns the value of attribute category.



4
5
6
# File 'lib/mtv/video.rb', line 4

def category
  @category
end

#contentObject

Returns the value of attribute content.



4
5
6
# File 'lib/mtv/video.rb', line 4

def content
  @content
end

#creditObject

Returns the value of attribute credit.



4
5
6
# File 'lib/mtv/video.rb', line 4

def credit
  @credit
end

#media_categoryObject

Returns the value of attribute media_category.



4
5
6
# File 'lib/mtv/video.rb', line 4

def media_category
  @media_category
end

#media_creditsObject

Returns the value of attribute media_credits.



4
5
6
# File 'lib/mtv/video.rb', line 4

def media_credits
  @media_credits
end

#media_descriptionObject

Returns the value of attribute media_description.



4
5
6
# File 'lib/mtv/video.rb', line 4

def media_description
  @media_description
end

#media_durationObject

Returns the value of attribute media_duration.



4
5
6
# File 'lib/mtv/video.rb', line 4

def media_duration
  @media_duration
end

#media_expressionObject

Returns the value of attribute media_expression.



4
5
6
# File 'lib/mtv/video.rb', line 4

def media_expression
  @media_expression
end

#media_keywordsObject

Returns the value of attribute media_keywords.



4
5
6
# File 'lib/mtv/video.rb', line 4

def media_keywords
  @media_keywords
end

#media_mediumObject

Returns the value of attribute media_medium.



4
5
6
# File 'lib/mtv/video.rb', line 4

def media_medium
  @media_medium
end

#media_player_urlObject

Returns the value of attribute media_player_url.



4
5
6
# File 'lib/mtv/video.rb', line 4

def media_player_url
  @media_player_url
end

#media_thumbnailsObject

Returns the value of attribute media_thumbnails.



4
5
6
# File 'lib/mtv/video.rb', line 4

def media_thumbnails
  @media_thumbnails
end

#media_titleObject

Returns the value of attribute media_title.



4
5
6
# File 'lib/mtv/video.rb', line 4

def media_title
  @media_title
end

#media_typeObject

Returns the value of attribute media_type.



4
5
6
# File 'lib/mtv/video.rb', line 4

def media_type
  @media_type
end

#media_urlObject

Returns the value of attribute media_url.



4
5
6
# File 'lib/mtv/video.rb', line 4

def media_url
  @media_url
end

#publishedObject

Returns the value of attribute published.



4
5
6
# File 'lib/mtv/video.rb', line 4

def published
  @published
end

#thumbnailsObject

Returns the value of attribute thumbnails.



4
5
6
# File 'lib/mtv/video.rb', line 4

def thumbnails
  @thumbnails
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/mtv/video.rb', line 4

def title
  @title
end

#uidObject

Returns the value of attribute uid.



4
5
6
# File 'lib/mtv/video.rb', line 4

def uid
  @uid
end

#updatedObject

Returns the value of attribute updated.



4
5
6
# File 'lib/mtv/video.rb', line 4

def updated
  @updated
end

#uriObject

Returns the value of attribute uri.



4
5
6
# File 'lib/mtv/video.rb', line 4

def uri
  @uri
end

#vidObject

Returns the value of attribute vid.



4
5
6
# File 'lib/mtv/video.rb', line 4

def vid
  @vid
end

Class Method Details

.find(uid, options = {}) ⇒ Object

Finds a video based on its unique id. It is unlike the intuitive MTV::Artist.uid, so you usually have to find a video’s uid first through a search or through the artist.

Examples

MTV::Video.find 'hznHivqrbHHDGDAW'
> #<MTV::Video title: "Loser", artist_uri: "http://api.mtvnservices.com/1/artist/beck/">

Raises:



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mtv/video.rb', line 22

def find(uid, options={})
  return uid if uid.is_a? Artist

  options.symbolize_keys!
  uid = options[:uid] || uid

  response = request "video/#{uid}/"
  response = hack_empty_response_case(response)
  raise Error, "That video not found!" if response.nil? || response.empty?
  parse_one response
end

.from_artist(artist) ⇒ Object

Finds all videos from a given artist or artist name.

Examples

MTV::Video.from_artist(MTV::Artist.find('beck'))
> #<MTV::Video title: "Gamma Ray", artist_uri: "http://api.mtvnservices.com/1/artist/beck/"> ... ]
MTV::Video.from_artist('beck')
> #<MTV::Video title: "Gamma Ray", artist_uri: "http://api.mtvnservices.com/1/artist/beck/"> ... ]

Raises:



42
43
44
45
46
47
48
# File 'lib/mtv/video.rb', line 42

def from_artist(artist)
  artist_uid = artist.is_a?(Artist) ? artist.uid : string_to_uid(artist)

  response = request "artist/#{artist_uid}/videos/"
  raise Error, "No videos found!" if response.nil? || response.empty?
  parse_many response
end

.search(term = nil, options = {}) ⇒ Object

Returns an array of videos that match a given search term.

Attributes

  • term - The search term to find a video

  • options - :max_results and/or :start_index for pagination.

Examples

MTV::Video.search 'the golden age', :max_results => 1, :start_index => 1
> [#<MTV::Video title: "The Golden Age", artist_uri: "http://api.mtvnservices.com/1/artist/beck/">]

Raises:



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mtv/video.rb', line 61

def search(term=nil, options={})
  options.symbolize_keys!
  params = { :'max-results' => (options[:max_results] || 1),
             :'start-index' => options[:start_index] }.reject { |k,v| v.nil? || v.to_s.empty? }.to_query
  params = "&#{params}" unless params.empty?
  term   = (options[:term] || term).to_query('term')

  response = request "video/search?#{term}#{params}"
  raise Error, "No videos not found!" if response.nil? || response.empty?
  parse_many response
end

Instance Method Details

#embed_code(options = {}) ⇒ Object

Returns HTML to embed the Flash player for this video anywhere

Attributes

  • options - Takes a combination of width and/or height for the Flash player

    to specify a certain size (if one is given, the ratio will be calculated for the other value)
    

Examples

MTV::Video.search('mellow gold').first.embed_code
MTV::Video.search('mellow gold').first.embed_code(:width => 100)
MTV::Video.search('mellow gold').first.embed_code(:width => 57, :height => 943)


166
167
168
169
170
171
172
173
174
# File 'lib/mtv/video.rb', line 166

def embed_code(options={})
  options[:width]  ||= (options[:height] ? ((options[:height]*448).to_f/366).to_i : 448)
  options[:height] ||= (options[:width]==448 ? 366 : ((options[:width]*366).to_f/448).to_i)

  <<-EOS
    <embed src="#{media_url}" type="#{media_type}" width="#{options[:width]}" height="#{options[:height]}"
    allowFullScreen="true" allowScriptAccess="always" />
  EOS
end

#inspectObject



176
177
178
179
# File 'lib/mtv/video.rb', line 176

def inspect
  attrs = [:title, :artist_uri].map { |name| "#{name}: #{attribute_for_inspect(name)}" }.compact.join(", ")
  "#<#{self.class} #{attrs}>"
end