Class: MTV::Artist

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

Overview

This class represents a music artist 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 = {}) ⇒ Artist

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

Attributes

  • options - Possible attributes are name, uid, uri, links, updated



125
126
127
128
# File 'lib/mtv/artist.rb', line 125

def initialize(values={})
  super(values)
  self.uid = uri.gsub(self.class.base_url + "/artist", '').delete '/' unless @uri.nil?
end

Instance Attribute Details

Returns the value of attribute links.



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

def links
  @links
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#uidObject

Returns the value of attribute uid.



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

def uid
  @uid
end

#updatedObject

Returns the value of attribute updated.



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

def updated
  @updated
end

#uriObject

Returns the value of attribute uri.



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

def uri
  @uri
end

Class Method Details

.browse(letter = 'a') ⇒ Object

Browse all artists by a single letter.

Examples

MTV::Artist.browse('x')
> [#<MTV::Artist name: "X">, #<MTV::Artist name: "X-Clan">, #<MTV::Artist name: "The X-Ecutioners">, ... ]

Raises:



17
18
19
20
21
# File 'lib/mtv/artist.rb', line 17

def browse(letter='a')
  response = request "artist/browse/#{letter.to_s.first}/"
  raise Error, "That artist not found!" if response.nil? || response.empty?
  parse_many response
end

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

Find an artist by their specific name. The name will be sanitized for the url, for example ‘Dinosaur Jr.’ becomes ‘dinosaur_jr’.

Examples

MTV::Artist.find('beck')
> #<MTV::Artist name: "Beck">
MTV::Artist.find('meGADeath')
> #<MTV::Artist name: "Megadeath">

Raises:



32
33
34
35
36
37
38
39
40
41
# File 'lib/mtv/artist.rb', line 32

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

  options.symbolize_keys!
  name = string_to_uid(options[:name] || name)

  response = request "artist/#{name}/"
  raise Error, "That artist not found!" if response.nil? || response.empty?
  parse_one response
end

Finds any related artists, given an artist or artist name.

Examples

MTV::Artist.related('Q Tip')
> [#<MTV::Artist name: "Allure">, #<MTV::Artist name: "Beastie Boys">, #<MTV::Artist name: "Busta Rhymes">, ... ]

Raises:



49
50
51
52
53
54
# File 'lib/mtv/artist.rb', line 49

def related(artist)
  artist = find(artist)
  response = request "artist/#{artist.uid}/related/"
  raise Error, "That artist not found!" if response.nil? || response.empty?
  parse_many response
end

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

Search all artists.

Attributes

  • term - The search term to find an artist

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

Examples

MTV::Artist.search('Beck', :max_results => 1, :start_index => 1)
> [#<MTV::Artist name: "Beck">]

Raises:



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mtv/artist.rb', line 67

def search(term=nil, options={})
  options.symbolize_keys!
  params = {}
  params[:'max-results'] = options[:max_results] || 1
  params[:'start-index'] = options[:start_index]
  term                   = options[:term] || term
  params.reject! { |k,v| !v }
  response = request "artist/search?#{term.to_query('term')}&#{params.to_param}"
  raise Error, "That artist not found!" if response.nil? || response.empty?
  parse_many response
end

.videos(artist) ⇒ Object

Returns a list of MTV::Video instances that are by the given artist.

Examples

MTV::Artist.videos('buggles')
> [#<MTV::Video title: "Video Killed The Radio Star", artist_uri: "http://api.mtvnservices.com/1/artist/buggles/">]


85
86
87
# File 'lib/mtv/artist.rb', line 85

def videos(artist)
  MTV::Video.from_artist(artist)
end

Instance Method Details

#browseObject

Searches for artists with the same first letter.

Examples

MTV::Artist.find('Sonic Youth').browse
> [#<MTV::Artist name: "The S.O.U.L. S.Y.S.T.E.M.">, #<MTV::Artist name: "S.T.U.N."> ... ]


141
142
143
# File 'lib/mtv/artist.rb', line 141

def browse
  @browse ||= Artist.browse(uid.first)
end

#inspectObject



130
131
132
133
# File 'lib/mtv/artist.rb', line 130

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

Searchs for related artists.

Examples

MTV::Artist.find('Sonic Youth').related
> [#<MTV::Artist name: "Beck">, #<MTV::Artist name: "Big Black">, #<MTV::Artist name: "Blonde Redhead"> ... ]


151
152
153
# File 'lib/mtv/artist.rb', line 151

def related
  @related ||= Artist.related(self)
end

#videosObject

Returns all MTV::Videos for this artist.

Examples

MTV::Artist.find('Sonic Youth').videos
> [#<MTV::Video title: "Incinerate", artist_uri: "http://api.mtvnservices.com/1/artist/sonic_youth/"> ... ]


162
163
164
# File 'lib/mtv/artist.rb', line 162

def videos
  @videos ||= Video.from_artist(self)
end