Class: MusicGraph::Album

Inherits:
Object
  • Object
show all
Defined in:
lib/musicgraph/album.rb

Constant Summary collapse

API_URL =
"http://api.musicgraph.com/api/v2/album/"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Album

Returns a new instance of Album.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/musicgraph/album.rb', line 7

def initialize(attributes)
  @title = attributes["title"]
  @title = attributes["name"] if attributes["name"]
  @release_year = attributes["release_year"]
  @entity_type = attributes["entity_type"]
  @album_artist_id = attributes["album_artist_id"]
  @id = attributes["id"]
  @number_of_tracks = attributes["number_of_tracks"]
  @album_ref_id = attributes["album_ref_id"]
  @performer_name = attributes["performer_name"]
  @main_genre = attributes["main_genre"]
  @product_form = attributes["product_form"]
end

Instance Attribute Details

#album_artist_idObject (readonly)

Returns the value of attribute album_artist_id.



3
4
5
# File 'lib/musicgraph/album.rb', line 3

def album_artist_id
  @album_artist_id
end

#album_ref_idObject (readonly)

Returns the value of attribute album_ref_id.



3
4
5
# File 'lib/musicgraph/album.rb', line 3

def album_ref_id
  @album_ref_id
end

#entity_typeObject (readonly)

Returns the value of attribute entity_type.



3
4
5
# File 'lib/musicgraph/album.rb', line 3

def entity_type
  @entity_type
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/musicgraph/album.rb', line 3

def id
  @id
end

#main_genreObject (readonly)

Returns the value of attribute main_genre.



3
4
5
# File 'lib/musicgraph/album.rb', line 3

def main_genre
  @main_genre
end

#number_of_tracksObject (readonly)

Returns the value of attribute number_of_tracks.



3
4
5
# File 'lib/musicgraph/album.rb', line 3

def number_of_tracks
  @number_of_tracks
end

#performer_nameObject (readonly)

Returns the value of attribute performer_name.



3
4
5
# File 'lib/musicgraph/album.rb', line 3

def performer_name
  @performer_name
end

#product_formObject (readonly)

Returns the value of attribute product_form.



3
4
5
# File 'lib/musicgraph/album.rb', line 3

def product_form
  @product_form
end

#release_yearObject (readonly)

Returns the value of attribute release_year.



3
4
5
# File 'lib/musicgraph/album.rb', line 3

def release_year
  @release_year
end

#titleObject (readonly)

Returns the value of attribute title.



3
4
5
# File 'lib/musicgraph/album.rb', line 3

def title
  @title
end

Class Method Details

.find(id, filters = nil) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/musicgraph/album.rb', line 43

def self.find(id, filters = nil)
  request = "#{API_URL}#{id}?#{MusicGraph.key_param}"
  request += "&fields=#{filters.join(",")}" if filters
  response = Faraday.get(request)
  attributes = JSON.parse(response.body)
  new(attributes["data"])
end

.search(params) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/musicgraph/album.rb', line 21

def self.search(params)
  if params.is_a? String
    response = Faraday.get("#{API_URL}search?#{MusicGraph.key_param}&title=#{params}")
  elsif params.is_a? Hash
    encoded_params = URI.encode_www_form(params)
    response = Faraday.get("#{API_URL}search?#{MusicGraph.key_param}&#{encoded_params}")
  end
  albums = JSON.parse(response.body)["data"]
  albums.map { |attributes| new(attributes) }
end

.suggest(params) ⇒ Object



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

def self.suggest(params)
  if params.is_a? String
    response = Faraday.get("#{API_URL}suggest?#{MusicGraph.key_param}&prefix=#{params}")
  elsif params.is_a? Hash
    encoded_params = URI.encode_www_form(params)
    response = Faraday.get("#{API_URL}suggest?#{MusicGraph.key_param}&#{encoded_params}")
  end
  albums = JSON.parse(response.body)["data"]
  albums.map { |attributes| new(attributes) }
end

Instance Method Details

#artistsObject



61
62
63
64
65
# File 'lib/musicgraph/album.rb', line 61

def artists
  response = Faraday.get("#{API_URL}#{id}/artists?#{MusicGraph.key_param}")
  artists = JSON.parse(response.body)["data"]
  artists.map { |attributes| Artist.new(attributes) }
end

#edgesObject



51
52
53
54
# File 'lib/musicgraph/album.rb', line 51

def edges
  response = Faraday.get("#{API_URL}#{id}/edges?#{MusicGraph.key_param}")
  JSON.parse(response.body)["data"]
end

#metadataObject



56
57
58
59
# File 'lib/musicgraph/album.rb', line 56

def 
  response = Faraday.get("#{API_URL}#{id}?#{MusicGraph.key_param}")
  JSON.parse(response.body)["data"]
end

#tracksObject



67
68
69
70
71
# File 'lib/musicgraph/album.rb', line 67

def tracks
  response = Faraday.get("#{API_URL}#{id}/tracks?#{MusicGraph.key_param}")
  tracks = JSON.parse(response.body)["data"]
  tracks.map { |attributes| Track.new(attributes) }
end