Class: Napster::Models::Album

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

Overview

Album model

Constant Summary collapse

ATTRIBUTES =
[:type,
:id,
:upc,
:shortcut,
:href,
:name,
:released,
:originally_released,
:label,
:copyright,
:tags,
:disc_count,
:track_count,
:explicit,
:single,
:account_partner,
:artist_name,
:contributing_artists].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ Album

Returns a new instance of Album.



32
33
34
35
36
37
38
39
# File 'lib/napster/models/album.rb', line 32

def initialize(arg)
  @client = arg[:client] if arg[:client]
  return unless arg[:data]

  ATTRIBUTES.each do |attribute|
    send("#{attribute}=", arg[:data][attribute.to_s.camel_case_lower])
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



30
31
32
# File 'lib/napster/models/album.rb', line 30

def client
  @client
end

Class Method Details

.collection(arg) ⇒ Object



41
42
43
44
45
# File 'lib/napster/models/album.rb', line 41

def self.collection(arg)
  arg[:data].map do |album|
    Album.new(data: album, client: @client)
  end
end

Instance Method Details

#find(arg) ⇒ Object



64
65
66
67
# File 'lib/napster/models/album.rb', line 64

def find(arg)
  return find_by_id(arg) if Napster::Moniker.check(arg, :album)
  find_by_name(arg)
end

#find_all_by_name(name) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/napster/models/album.rb', line 74

def find_all_by_name(name)
  options = {
    params: {
      q: name,
      type: 'album'
    }
  }
  response = @client.get('/search', options)
  Album.collection(data: response['data'])
end

#find_by_id(id) ⇒ Object



69
70
71
72
# File 'lib/napster/models/album.rb', line 69

def find_by_id(id)
  response = @client.get("/albums/#{id}")
  Album.new(data: response['albums'].first, client: @client)
end

#find_by_name(name) ⇒ Object



85
86
87
# File 'lib/napster/models/album.rb', line 85

def find_by_name(name)
  find_all_by_name(name).first
end

#new_releases(params) ⇒ Object

Top level methods



49
50
51
52
# File 'lib/napster/models/album.rb', line 49

def new_releases(params)
  response = @client.get('/albums/new', params: params)
  Album.collection(data: response['albums'], client: @client)
end

#staff_picks(params) ⇒ Object



54
55
56
57
# File 'lib/napster/models/album.rb', line 54

def staff_picks(params)
  response = @client.get('/albums/picks', params: params)
  Album.collection(data: response['albums'], client: @client)
end

#top(params) ⇒ Object



59
60
61
62
# File 'lib/napster/models/album.rb', line 59

def top(params)
  response = @client.get('/albums/top', params: params)
  Album.collection(data: response['albums'], client: @client)
end

#tracks(params) ⇒ Object

Instance methods



91
92
93
94
# File 'lib/napster/models/album.rb', line 91

def tracks(params)
  response = @client.get("/albums/#{@id}/tracks", params: params)
  Track.collection(data: response['tracks'], client: @client)
end