Class: Napster::Models::Profile

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

Overview

Profile model

Constant Summary collapse

ATTRIBUTES =
[:id,
:real_name,
:screen_name,
:bio,
:location,
:gender,
:visibility,
:type,
:href,
:favorite_albums_count,
:favorite_artists_count,
:favorite_tracks_count,
:playlists_total_count,
:playlists_published_count,
:stations_count,
:radio_count,
:follower_count,
:following_count,
:avatar,
:avatar_id,
:default_avatar,
:avatar_version,
:links].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ Profile

Returns a new instance of Profile.



37
38
39
40
41
42
43
# File 'lib/napster/models/profile.rb', line 37

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.



35
36
37
# File 'lib/napster/models/profile.rb', line 35

def client
  @client
end

Class Method Details

.collection(arg) ⇒ Object



45
46
47
48
49
# File 'lib/napster/models/profile.rb', line 45

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

Instance Method Details

#getObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/napster/models/profile.rb', line 51

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

#update(body) ⇒ Object



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

def update(body)
  put_options = {
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  @client.put('/me', Oj.dump(body), put_options)
end