Class: Napster::Models::Favorite

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

Overview

Favorite model

Constant Summary collapse

ATTRIBUTES =
[:type,
:id,
:name,
:modified,
:href,
:privacy,
:images,
:description,
:favorite_count,
:free_play_compliant].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ Favorite

Returns a new instance of Favorite.



24
25
26
27
28
29
30
31
# File 'lib/napster/models/favorite.rb', line 24

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.



22
23
24
# File 'lib/napster/models/favorite.rb', line 22

def client
  @client
end

Class Method Details

.collection(arg) ⇒ Object



33
34
35
36
37
# File 'lib/napster/models/favorite.rb', line 33

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

Instance Method Details

#add(ids) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/napster/models/favorite.rb', line 89

def add(ids)
  post_options = {
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  body = prepare_add_favorites_body(ids)
  @client.post('/me/favorites', Oj.dump(body), post_options)
  status(ids)
end

#get(params) ⇒ Object

/me



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/napster/models/favorite.rb', line 61

def get(params)
  get_options = {
    params: params,
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  response = @client.get('/me/favorites', get_options)
  Favorite.collection(data: response['favorites'], client: @client)
end

#member_favorites_for(id) ⇒ Object



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

def member_favorites_for(id)
  request_member_favorites(id)
end

#members_who_favorited_albums(id) ⇒ Object

Top level methods

Raises:

  • (ArgumentError)


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

def members_who_favorited_albums(id)
  e = 'Invalid playlist id'
  raise ArgumentError, e unless Napster::Moniker.check(id, :album)
  response = @client.get("/albums/#{id}/favorited/members")
  Member.collection(data: response['members'], client: @client)
end

#members_who_favorited_artists(id) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
# File 'lib/napster/models/favorite.rb', line 48

def members_who_favorited_artists(id)
  e = 'Invalid playlist id'
  raise ArgumentError, e unless Napster::Moniker.check(id, :artist)
  response = @client.get("/artists/#{id}/favorited/members")
  Member.collection(data: response['members'], client: @client)
end

#remove(id) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/napster/models/favorite.rb', line 102

def remove(id)
  delete_options = {
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  @client.delete("/me/favorites/#{id}", delete_options)
  status([id])
end

#status(ids) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/napster/models/favorite.rb', line 74

def status(ids)
  get_options = {
    params: {
      ids: ids
    },
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  response = @client.get('/me/favorites/status', get_options)
  FavoriteStatus.collection(data: response['status'], client: @client)
end