Class: Turntabler::AuthorizedUser

Inherits:
User show all
Defined in:
lib/turntabler/authorized_user.rb

Overview

Represents a user who has authorized with the Turntable service

Instance Attribute Summary collapse

Attributes inherited from User

#about, #acl, #avatar, #facebook_url, #fans_count, #hangout, #laptop_name, #laptop_version, #name, #points, #sticker_placements, #top_artists, #website

Instance Method Summary collapse

Methods inherited from User

#add_as_moderator, #become_fan, #block, #boot, #dj?, #listener?, #messages, #moderator?, #presence, #remove_as_dj, #remove_as_moderator, #report, #say, #stalk, #unblock, #unfan

Methods inherited from Resource

#==, attribute, #attributes=, #hash, #loaded?, #pretty_print, #pretty_print_instance_variables

Methods included from Assertions

#assert_valid_keys, #assert_valid_values

Methods included from DigestHelpers

#digest

Constructor Details

#initialize(client) ⇒ AuthorizedUser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of AuthorizedUser



44
45
46
47
48
49
# File 'lib/turntabler/authorized_user.rb', line 44

def initialize(client, *)
  @status = 'available'
  @playlists = PlaylistDirectory.new(client)
  @preferences = Preferences.new(client)
  super
end

Instance Attribute Details

#authString (readonly)

Gets the authentication token associated with this user for use with API services. This will log the user in via email / password if it's not already set.

Raises:



11
# File 'lib/turntabler/authorized_user.rb', line 11

attribute :auth, :userauth

#emailString (readonly)

The e-mail address the user registered with on Turntable. This is typically only set if the user didn't log in via Facebook or Twitter.



28
# File 'lib/turntabler/authorized_user.rb', line 28

attribute :email

#facebook_idString (readonly)

The user's unique identifier on Facebook



19
# File 'lib/turntabler/authorized_user.rb', line 19

attribute :facebook_id, :fbid

#passwordString (readonly)

The password associated with the e-mail address registered with on Turntable. This is typically only set if the user didn't log in via Facebook or Twitter.



33
# File 'lib/turntabler/authorized_user.rb', line 33

attribute :password

#playlistsTurntabler::PlaylistDirectory (readonly)

The user's custom playlists



37
38
39
# File 'lib/turntabler/authorized_user.rb', line 37

def playlists
  @playlists
end

#preferencesTurntabler::Preferences (readonly)

The user's current Turntable preferences



41
42
43
# File 'lib/turntabler/authorized_user.rb', line 41

def preferences
  @preferences
end

#statusString (readonly)

The current availability status of the user (“available”, “unavailable”, or “away”)



15
# File 'lib/turntabler/authorized_user.rb', line 15

attribute :status

#twitter_idString (readonly)

The user's unique identifier on Twitter



23
# File 'lib/turntabler/authorized_user.rb', line 23

attribute :twitter_id, :twitterid

Instance Method Details

#authenticatetrue

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Authenticates the current user with turntable.

Raises:



92
93
94
95
# File 'lib/turntabler/authorized_user.rb', line 92

def authenticate
  api('user.authenticate')
  true
end

#avatarsArray<Turntabler::Avatar>

Note:

This may load the user's data in order to get the ACL if it's not available already

Gets the avatars that can be set by this user.

Examples:

user.avatars    # => [#<Turntabler::Avatar ...>, ...]

Raises:



188
189
190
# File 'lib/turntabler/authorized_user.rb', line 188

def avatars
  client.avatars.select {|avatar| avatar.available?}
end

#blocksArray<Turntabler::User>

Gets the users that have been blocked by this user.

Examples:

user.blocks   # => [#<Turntabler::User ...>, ...]

Raises:



220
221
222
223
# File 'lib/turntabler/authorized_user.rb', line 220

def blocks
  data = api('block.list_all')
  data['blocks'].map {|attrs| User.new(client, attrs['block']['blocked'])}
end

#buddiesArray<Turntabler::User>

Loads the list of users that are connected to the current user through a social network like Facebook or Twitter.

Examples:

user.buddies    # => [#<Turntabler::User ...>, ...]

Raises:



154
155
156
157
# File 'lib/turntabler/authorized_user.rb', line 154

def buddies
  data = api('user.get_buddies')
  data['buddies'].map {|id| User.new(client, :_id => id)}
end

#fan_ofArray<Turntabler::User>

Loads the list of users that the current user is a fan of.

Examples:

user.fan_of   # => [#<Turntabler::User ...>, ...]

Raises:



165
166
167
168
# File 'lib/turntabler/authorized_user.rb', line 165

def fan_of
  data = api('user.get_fan_of')
  data['fanof'].map {|id| User.new(client, :_id => id)}
end

#fansArray<Turntabler::User>

Loads the list of users that are a fan of the current user.

Examples:

user.fans   # => [#<Turntabler::User ...>, ...]

Raises:



176
177
178
179
# File 'lib/turntabler/authorized_user.rb', line 176

def fans
  data = api('user.get_fans')
  data['fans'].map {|id| User.new(client, :_id => id)}
end

#idString

Gets the Turntable id associated with this user for use with API services. This will log the user in via email / password if it's not already set.

Raises:



56
57
58
59
# File 'lib/turntabler/authorized_user.rb', line 56

def id
   unless @id
  @id
end

#loadtrue

Loads the attributes for this user. Attributes will automatically load when accessed, but this allows data to be forcefully loaded upfront.

Examples:

user.load     # => true
user.email    # => "john.doe@gmail.com"

Raises:



105
106
107
108
109
# File 'lib/turntabler/authorized_user.rb', line 105

def load
  data = api('user.info')
  self.attributes = data
  super
end

#logintrue

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Logs the user in using the associated e-mail address / password. This will generate a user id / auth token for authentication with the API services.

Raises:



78
79
80
81
82
83
84
85
# File 'lib/turntabler/authorized_user.rb', line 78

def 
  response = EventMachine::HttpRequest.new('https://turntable.fm/api/user.email_login').get(:query => {:email => email, :password => password, :client => client.id}).response
  success, data = response.empty? ? [nil, {}] : JSON.parse(response)
  raise(ConnectionError, data['err']) unless success

  self.attributes = data
  true
end

#playlist(id = 'default') ⇒ Turntabler::Playlist

Gets the playlist with the given id.

Examples:

user.playlist             # => #<Turntabler::Playlist id="default" ...>
user.playlist("rock")     # => #<Turntabler::Playlist id="rock" ...>


199
200
201
# File 'lib/turntabler/authorized_user.rb', line 199

def playlist(id = 'default')
  playlists.build(:_id => id)
end

#stickers_purchasedArray<Turntabler::Sticker>

Gets the stickers that have been purchased by this user.

Examples:

user.stickers_purchased   # => [#<Turntabler::Sticker ...>, ...]

Raises:



209
210
211
212
# File 'lib/turntabler/authorized_user.rb', line 209

def stickers_purchased
  data = api('sticker.get_purchased_stickers')
  data['stickers'].map {|sticker_id| Sticker.new(client, :_id => sticker_id)}
end

#update(attributes = {}) ⇒ true

Updates this user's profile information.

Examples:

user.update(:status => "away")        # => true
user.update(:laptop_name => "mac")    # => true
user.update(:name => "...")           # => true

Options Hash (attributes):

  • :name (String)
  • :status (String)

    Valid values include “available”, “unavailable”, and “away”

  • :laptop_name (String)

    Valid values include “mac”, “pc”, “linux”, “chrome”, “iphone”, “cake”, “intel”, and “android”

  • :twitter_id (String)
  • :facebook_url (String)
  • :website (String)
  • :about (String)
  • :top_artists (String)
  • :hangout (String)

Raises:

  • (ArgumentError)

    if an invalid attribute or value is specified

  • (Turntabler::Error)

    if the command fails



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/turntabler/authorized_user.rb', line 130

def update(attributes = {})
  assert_valid_keys(attributes, :name, :status, :laptop_name, :twitter_id, :facebook_url, :website, :about, :top_artists, :hangout)

  # Update status
  status = attributes.delete(:status)
  update_status(status) if status

  # Update laptop
  laptop_name = attributes.delete(:laptop_name)
  update_laptop(laptop_name) if laptop_name

  # Update profile with remaining data
  update_profile(attributes) if attributes.any?

  true
end