Class: Turntabler::Room
Overview
Represents an individual room in Turntable
Instance Attribute Summary collapse
-
#created_at ⇒ Time
readonly
The time at which this room was created.
-
#creator ⇒ Turntabler::User
readonly
The user that created the room.
-
#current_dj ⇒ Turntabler::User
readonly
The current DJ playing.
-
#current_song ⇒ Turntabler::Song
readonly
The current song being played.
-
#description ⇒ String
readonly
A longer description of the room (sometimes includes rules, guidelines, etc.).
-
#dj_capacity ⇒ Fixnum
readonly
The maximum number of users that can DJ.
-
#dj_minimum_points ⇒ Fixnum
readonly
The minimum number of points required to DJ.
-
#djs ⇒ Array<Turntabler::User>
readonly
The users that are currently DJ’ing in the room.
-
#featured ⇒ Boolean
readonly
Whether this room is being featured by Turntable.
-
#friends ⇒ Array<Turntabler::User>
readonly
The current user’s friends who are also known to be in the room.
-
#genre ⇒ String
readonly
The type of music being played in the room.
-
#host ⇒ String
readonly
Uses the configured chat host or attempts to look it up based on the room id.
-
#id ⇒ String
readonly
Allow the id to be set via the “roomid” attribute.
-
#listener_capacity ⇒ Fixnum
readonly
The maximum number of listeners that can be in the room (including DJs).
-
#listeners ⇒ Array<Turntabler::User>
readonly
The listeners currently in the rom.
-
#moderators ⇒ Array<Turntabler::User>
readonly
The users that are appointed to moderate the room.
-
#name ⇒ String
readonly
The human-readable name for the room.
-
#privacy ⇒ String
readonly
The privacy level for the room (either “public” or “unlisted”).
-
#section ⇒ String
readonly
The section of the room that the user is in.
-
#shortcut ⇒ String
readonly
The path which can be used in the url to load the room.
-
#songs_played ⇒ Array<Turntabler::Song>
readonly
The list of songs that have been played in this room.
Instance Method Summary collapse
-
#add_as_favorite ⇒ true
Add this room to the current user’s favorites.
-
#attributes=(attrs) ⇒ Object
private
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.
-
#become_dj ⇒ true
Adds the current user to the list of DJs.
-
#build_user(attrs) ⇒ Turntabler::User
private
Gets the user represented by the given attributes.
-
#can_dj? ⇒ Boolean
Determines whether the current user can dj based on the minimum points required and spot availability.
-
#dj(user_id) ⇒ Turntabler::User?
Gets the dj with the given user id.
-
#enter ⇒ true
Enters the current room.
-
#friend(user_id) ⇒ Turntabler::User?
Gets the friend in the room with the given user id.
-
#initialize ⇒ Room
constructor
private
A new instance of Room.
-
#leave ⇒ true
Leaves from the room.
-
#listener(user_id) ⇒ Turntabler::User?
Gets the listener with the given user id.
-
#load(options = {}) ⇒ true
Loads the attributes for this room.
-
#moderator(user_id) ⇒ Turntabler::User?
Gets the moderator with the given user id.
-
#remove_as_favorite ⇒ true
Remove this room from current user’s favorites.
-
#report(reason = '') ⇒ true
Reports abuse by a room.
-
#say(content) ⇒ true
Braodcasts a message in the chat.
-
#update(attributes = {}) ⇒ true
Updates this room’s information.
-
#url ⇒ String
Gets the configured chat url.
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
Constructor Details
#initialize ⇒ Room
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_at ⇒ Time (readonly)
The time at which this room was created
51 52 53 |
# File 'lib/turntabler/room.rb', line 51 attribute :created_at, :created do |value| Time.at(value) end |
#creator ⇒ Turntabler::User (readonly)
The user that created the room
67 68 69 |
# File 'lib/turntabler/room.rb', line 67 attribute :creator do |attrs| build_user(attrs) end |
#current_dj ⇒ Turntabler::User (readonly)
The current DJ playing
106 107 108 |
# File 'lib/turntabler/room.rb', line 106 attribute :current_dj do |id| build_user(:_id => id) end |
#current_song ⇒ Turntabler::Song (readonly)
The current song being played
100 101 102 |
# File 'lib/turntabler/room.rb', line 100 attribute :current_song do |attrs| Song.new(client, attrs) end |
#description ⇒ String (readonly)
A longer description of the room (sometimes includes rules, guidelines, etc.)
23 |
# File 'lib/turntabler/room.rb', line 23 attribute :description |
#dj_capacity ⇒ Fixnum (readonly)
The maximum number of users that can DJ
39 |
# File 'lib/turntabler/room.rb', line 39 attribute :dj_capacity, :max_djs |
#dj_minimum_points ⇒ Fixnum (readonly)
The minimum number of points required to DJ
43 |
# File 'lib/turntabler/room.rb', line 43 attribute :dj_minimum_points, :djthreshold |
#djs ⇒ Array<Turntabler::User> (readonly)
The users that are currently DJ’ing in the room
79 80 81 |
# File 'lib/turntabler/room.rb', line 79 attribute :djs do |ids| Set.new(ids.map {|id| build_user(:_id => id)}) end |
#featured ⇒ Boolean (readonly)
Whether this room is being featured by Turntable
63 |
# File 'lib/turntabler/room.rb', line 63 attribute :featured |
#friends ⇒ Array<Turntabler::User> (readonly)
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.
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 |
#genre ⇒ String (readonly)
The type of music being played in the room
47 |
# File 'lib/turntabler/room.rb', line 47 attribute :genre |
#host ⇒ String (readonly)
Uses the configured chat host or attempts to look it up based on the room id
57 58 59 |
# File 'lib/turntabler/room.rb', line 57 attribute :host, :chatserver do |(host, *)| host end |
#id ⇒ String (readonly)
Allow the id to be set via the “roomid” attribute
10 |
# File 'lib/turntabler/room.rb', line 10 attribute :id, :roomid, :load => false |
#listener_capacity ⇒ Fixnum (readonly)
The maximum number of listeners that can be in the room (including DJs)
35 |
# File 'lib/turntabler/room.rb', line 35 attribute :listener_capacity, :max_size |
#listeners ⇒ Array<Turntabler::User> (readonly)
The listeners currently in the rom
73 74 75 |
# File 'lib/turntabler/room.rb', line 73 attribute :listeners, :users do |users| Set.new(users.map {|attrs| build_user(attrs)}) end |
#moderators ⇒ Array<Turntabler::User> (readonly)
The users that are appointed to moderate the room
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 |
#name ⇒ String (readonly)
The human-readable name for the room
19 |
# File 'lib/turntabler/room.rb', line 19 attribute :name |
#privacy ⇒ String (readonly)
The privacy level for the room (either “public” or “unlisted”)
31 |
# File 'lib/turntabler/room.rb', line 31 attribute :privacy |
#section ⇒ String (readonly)
The section of the room that the user is in. This only applies to overflow rooms.
15 |
# File 'lib/turntabler/room.rb', line 15 attribute :section, :load => false |
#shortcut ⇒ String (readonly)
The path which can be used in the url to load the room
27 |
# File 'lib/turntabler/room.rb', line 27 attribute :shortcut |
#songs_played ⇒ Array<Turntabler::Song> (readonly)
This is not an exhaustive list
The list of songs that have been played in this room.
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_favorite ⇒ true
Add this room to the current user’s favorites.
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.
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_dj ⇒ true
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.
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.
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
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.
322 323 324 |
# File 'lib/turntabler/room.rb', line 322 def dj(user_id) djs.detect {|dj| dj.id == user_id} end |
#enter ⇒ true
This will leave any room the user is already currently in (unless the same room is being entered)
Enters the current room.
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?
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.
350 351 352 |
# File 'lib/turntabler/room.rb', line 350 def friend(user_id) friends.detect {|friend| friend.id == user_id} end |
#leave ⇒ true
Leaves from the room.
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.
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
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.
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( = {}) assert_valid_keys(, :song_log) = {:song_log => false}.merge() # 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 => [: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.
340 341 342 |
# File 'lib/turntabler/room.rb', line 340 def moderator(user_id) moderators.detect {|moderator| moderator.id == user_id} end |
#remove_as_favorite ⇒ true
Remove this room from current user’s favorites.
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.
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.
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.
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 |
#url ⇒ String
Gets the configured chat url
142 143 144 |
# File 'lib/turntabler/room.rb', line 142 def url "ws://#{host}/socket.io/websocket" end |