Class: Turntabler::Room

Inherits:
Resource show all
Defined in:
lib/turntabler/room.rb

Overview

Represents an individual room in Turntable

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#==, attribute, #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

#initializeRoom

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 Room



118
119
120
121
122
# File 'lib/turntabler/room.rb', line 118

def initialize(*)
  @friends = Set.new
  @songs_played = []
  super
end

Instance Attribute Details

#created_atTime (readonly)

The time at which this room was created

Returns:

  • (Time)


51
52
53
# File 'lib/turntabler/room.rb', line 51

attribute :created_at, :created do |value|
  Time.at(value)
end

#creatorTurntabler::User (readonly)

The user that created the room

Returns:



67
68
69
# File 'lib/turntabler/room.rb', line 67

attribute :creator do |attrs|
  build_user(attrs)
end

#current_djTurntabler::User (readonly)

The current DJ playing

Returns:



106
107
108
# File 'lib/turntabler/room.rb', line 106

attribute :current_dj do |id|
  build_user(:_id => id)
end

#current_songTurntabler::Song (readonly)

The current song being played

Returns:



100
101
102
# File 'lib/turntabler/room.rb', line 100

attribute :current_song do |attrs|
  Song.new(client, attrs)
end

#descriptionString (readonly)

A longer description of the room (sometimes includes rules, guidelines, etc.)

Returns:

  • (String)


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

attribute :description

#dj_capacityFixnum (readonly)

The maximum number of users that can DJ

Returns:

  • (Fixnum)


39
# File 'lib/turntabler/room.rb', line 39

attribute :dj_capacity, :max_djs

#dj_minimum_pointsFixnum (readonly)

The minimum number of points required to DJ

Returns:

  • (Fixnum)


43
# File 'lib/turntabler/room.rb', line 43

attribute :dj_minimum_points, :djthreshold

#djsArray<Turntabler::User> (readonly)

The users that are currently DJ'ing in the room

Returns:



79
80
81
# File 'lib/turntabler/room.rb', line 79

attribute :djs do |ids|
  Set.new(ids.map {|id| build_user(:_id => id)})
end

Whether this room is being featured by Turntable

Returns:

  • (Boolean)


63
# File 'lib/turntabler/room.rb', line 63

attribute :featured

#friendsArray<Turntabler::User> (readonly)

Note:

This is only available when the room is discovered via Turntabler::RoomDirectory#with_friends

The current user's friends who are also known to be in the room. These friends must be connected through a separate network like Facebook or Twitter.

Returns:



94
95
96
# File 'lib/turntabler/room.rb', line 94

attribute :friends, :load => false do |users|
  Set.new(users.map {|attrs| build_user(attrs)})
end

#genreString (readonly)

The type of music being played in the room

Returns:

  • (String)


47
# File 'lib/turntabler/room.rb', line 47

attribute :genre

#hostString (readonly)

Uses the configured chat host or attempts to look it up based on the room id

Returns:

  • (String)

Raises:



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

attribute :host, :chatserver do |(host, *)|
  host
end

#idString (readonly)

Allow the id to be set via the “roomid” attribute

Returns:

  • (String)


10
# File 'lib/turntabler/room.rb', line 10

attribute :id, :roomid, :load => false

#listener_capacityFixnum (readonly)

The maximum number of listeners that can be in the room (including DJs)

Returns:

  • (Fixnum)


35
# File 'lib/turntabler/room.rb', line 35

attribute :listener_capacity, :max_size

#listenersArray<Turntabler::User> (readonly)

The listeners currently in the rom

Returns:



73
74
75
# File 'lib/turntabler/room.rb', line 73

attribute :listeners, :users do |users|
  Set.new(users.map {|attrs| build_user(attrs)})
end

#moderatorsArray<Turntabler::User> (readonly)

The users that are appointed to moderate the room

Returns:



85
86
87
# File 'lib/turntabler/room.rb', line 85

attribute :moderators, :moderator_id do |ids|
  Set.new(ids.map {|id| build_user(:_id => id)})
end

#nameString (readonly)

The human-readable name for the room

Returns:

  • (String)


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

attribute :name

#privacyString (readonly)

The privacy level for the room (either “public” or “unlisted”)

Returns:

  • (String)


31
# File 'lib/turntabler/room.rb', line 31

attribute :privacy

#sectionString (readonly)

The section of the room that the user is in. This only applies to overflow rooms.

Returns:

  • (String)


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

attribute :section, :load => false

#shortcutString (readonly)

The path which can be used in the url to load the room

Returns:

  • (String)


27
# File 'lib/turntabler/room.rb', line 27

attribute :shortcut

#songs_playedArray<Turntabler::Song> (readonly)

Note:

This is not an exhaustive list

The list of songs that have been played in this room.

Returns:



113
114
115
# File 'lib/turntabler/room.rb', line 113

attribute :songs_played, :songlog, :load => false do |songs|
  songs.map {|attrs| Song.new(client, attrs)}
end

Instance Method Details

#add_as_favoritetrue

Add this room to the current user's favorites.

Examples:

room.add_as_favorite    # => true

Returns:

  • (true)

Raises:



254
255
256
257
# File 'lib/turntabler/room.rb', line 254

def add_as_favorite
  api('room.add_favorite')
  true
end

#attributes=(attrs) ⇒ Object

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.

Sets the current attributes for this room, ensuring that the full list of listeners gets set first so that we can use those built users to then fill out the collection of djs, moderators, etc.

Parameters:

  • attrs (Hash)

    The attributes to set



181
182
183
184
185
186
187
188
189
190
# File 'lib/turntabler/room.rb', line 181

def attributes=(attrs)
  if attrs
    super('users' => attrs.delete('users')) if attrs['users']
    super
    
    # Set room-level attributes that are specific to the song
    song_attributes = attrs['metadata'] && attrs['metadata'].select {|key, value| %w(upvotes downvotes votelog).include?(key)}
    current_song.attributes = song_attributes if @current_song
  end
end

#become_djtrue

Note:

This will cause the user to enter the current room if that isn't already the case

Adds the current user to the list of DJs.

Examples:

room.become_dj    # => true

Returns:

  • (true)

Raises:



311
312
313
314
315
# File 'lib/turntabler/room.rb', line 311

def become_dj
  enter
  api('room.add_dj')
  true
end

#build_user(attrs) ⇒ Turntabler::User

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.

Gets the user represented by the given attributes. This can either pull the user from:

  • The currently authorized user

  • The room's creator

  • The room's listeners

  • The room's moderators

If the user isn't present in any of those, then a new User instance will get created.

Parameters:

  • attrs (Hash)

    The attributes representing the user

Returns:



283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/turntabler/room.rb', line 283

def build_user(attrs)
  user = User.new(client, attrs)
  user = if client.user == user
    client.user
  elsif @creator == user
    creator
  elsif result = @listeners && listener(user.id) || @moderators && moderator(user.id) || friend(user.id)
    result
  else
    user
  end
  user.attributes = attrs
  user
end

#can_dj?Boolean

Determines whether the current user can dj based on the minimum points required and spot availability

Returns:

  • (Boolean)


300
301
302
# File 'lib/turntabler/room.rb', line 300

def can_dj?
  dj_capacity > djs.length && dj_minimum_points <= client.user.points
end

#dj(user_id) ⇒ Turntabler::User?

Gets the dj with the given user id.

Examples:

room.dj('4fd8...')    # => #<Turntabler::User ...>

Returns:



322
323
324
# File 'lib/turntabler/room.rb', line 322

def dj(user_id)
  djs.detect {|dj| dj.id == user_id}
end

#entertrue

Note:

This will leave any room the user is already currently in (unless the same room is being entered)

Enters the current room.

Examples:

room.enter    # => true

Returns:

  • (true)

Raises:



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/turntabler/room.rb', line 216

def enter
  if client.room != self
    # Leave the old room
    client.room.leave if client.room

    # Connect and register with this room
    client.connect(url)
    begin
      client.room = self
      data = api('room.register', :section => nil)
      self.attributes = {'section' => data['section']}
    rescue Exception
      client.room = nil
      raise
    end
  end

  true
end

#friend(user_id) ⇒ Turntabler::User?

Note:

This is only available when the room is discovered via Turntabler::RoomDirectory#with_friends

Gets the friend in the room with the given user id.

Examples:

room.friend('4fd8...')    # => #<Turntabler::User ...>

Returns:



350
351
352
# File 'lib/turntabler/room.rb', line 350

def friend(user_id)
  friends.detect {|friend| friend.id == user_id}
end

#leavetrue

Leaves from the room.

Examples:

room.leave    # => true

Returns:

  • (true)

Raises:



242
243
244
245
246
# File 'lib/turntabler/room.rb', line 242

def leave
  api('room.deregister')
  client.room = nil
  true
end

#listener(user_id) ⇒ Turntabler::User?

Gets the listener with the given user id.

Examples:

room.listener('4fd8...')    # => #<Turntabler::User ...>

Returns:



331
332
333
# File 'lib/turntabler/room.rb', line 331

def listener(user_id)
  listeners.detect {|listener| listener.id == user_id}
end

#load(options = {}) ⇒ true

Note:

This will open a connection to the chat server the room is hosted on if the client is not already connected to it

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

Examples:

room.load                       # => true
room.load(:song_log => true)    # => true

Parameters:

  • options (Hash) (defaults to: {})

    The configuration options

Options Hash (options):

  • :song_log (Boolean) — default: false

    Whether to include the song log

Returns:

  • (true)

Raises:

  • (ArgumentError)

    if an invalid option is specified

  • (Turntabler::Error)

    if the command fails



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/turntabler/room.rb', line 158

def load(options = {})
  assert_valid_keys(options, :song_log)
  options = {:song_log => false}.merge(options)

  # Use a client that is connected on the same url this room is hosted on
  client = @client.url == url ? @client : Turntabler::Client.new(@client.user.id, @client.user.auth, :url => url, :timeout => @client.timeout)

  begin
    data = api('room.info', :extended => options[:song_log])
    self.attributes = data['room'].merge('users' => data['users'])
    super()
  ensure
    # Close the client if it was only opened for use in this API call
    client.close if client != @client
  end
end

#moderator(user_id) ⇒ Turntabler::User?

Gets the moderator with the given user id.

Examples:

room.moderator('4fd8...')   # => #<Turntabler::User ...>

Returns:



340
341
342
# File 'lib/turntabler/room.rb', line 340

def moderator(user_id)
  moderators.detect {|moderator| moderator.id == user_id}
end

#remove_as_favoritetrue

Remove this room from current user's favorites.

Examples:

room.remove_as_favorite   # => true

Returns:

  • (true)

Raises:



265
266
267
268
# File 'lib/turntabler/room.rb', line 265

def remove_as_favorite
  api('room.rem_favorite')
  true
end

#report(reason = '') ⇒ true

Reports abuse by a room.

Examples:

room.report('Name abuse ...')   # => true

Parameters:

  • reason (String) (defaults to: '')

    The reason the room is being reported

Returns:

  • (true)

Raises:



374
375
376
377
# File 'lib/turntabler/room.rb', line 374

def report(reason = '')
  api('room.report', :reason => reason)
  true
end

#say(content) ⇒ true

Braodcasts a message in the chat.

Examples:

room.say("What's up guys?")   # => true

Parameters:

  • content (String)

    The text to send to the chat

Returns:

  • (true)

Raises:



361
362
363
364
365
# File 'lib/turntabler/room.rb', line 361

def say(content)
  enter
  api('room.speak', :text => content)
  true
end

#update(attributes = {}) ⇒ true

Updates this room's information.

Examples:

room.update(:description => '...')    # => true

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to update

Options Hash (attributes):

  • :description (String)

Returns:

  • (true)

Raises:

  • (ArgumentError)

    if an invalid attribute is specified

  • (Turntabler::Error)

    if the command fails



201
202
203
204
205
206
207
# File 'lib/turntabler/room.rb', line 201

def update(attributes = {})
  assert_valid_keys(attributes, :description)

  api('room.modify', attributes)
  self.attributes = attributes
  true
end

#urlString

Gets the configured chat url

Returns:

  • (String)


142
143
144
# File 'lib/turntabler/room.rb', line 142

def url
  "ws://#{host}/socket.io/websocket"
end