Class: Turntabler::RoomDirectory

Inherits:
Object
  • Object
show all
Includes:
Assertions
Defined in:
lib/turntabler/room_directory.rb

Overview

Provides a set of helper methods for interacting with Turntable's directory of rooms.

Instance Method Summary collapse

Methods included from Assertions

#assert_valid_keys, #assert_valid_values

Constructor Details

#initialize(client) ⇒ RoomDirectory

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 RoomDirectory



10
11
12
# File 'lib/turntabler/room_directory.rb', line 10

def initialize(client)
  @client = client
end

Instance Method Details

#all(options = {}) ⇒ Array<Turntabler::Room>

Gets the list of available rooms.

Examples:

rooms.all                                           # => [#<Turntabler::Room ...>, ...]
rooms.all(:favorites => true)                       # => [#<Turntabler::Room ...>, ...]
rooms.all(:available_djs => true, :genre => :rock)  # => [#<Turntabler::Room ...>, ...]
rooms.all(:sort => :random)                         # => [#<Turntabler::Room ...>, ...]

Parameters:

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

    The search options

Options Hash (options):

  • :limit (Fixnum) — default: 20

    The total number of rooms to list

  • :skip (Fixnum) — default: 0

    The number of rooms to skip when loading the list

  • :favorites (Fixnum) — default: false

    Whether to only include rooms marked as favorites

  • :available_djs (Boolean) — default: false

    Whether to only include rooms that have dj spots available

  • :genre (Symbol)

    The genre of music being played in the room, . Possible values are :rock, :electronica, :indie, :hiphop, :pop, and :dubstep.

  • :minimum_listeners (Fixnum) — default: 1

    The minimum number of listeners in the room

  • :sort (Symbol) — default: :listeners

    The order to list rooms in. Possible values are :created, :listeners, and :random.

Returns:

Raises:

  • (ArgumentError)

    if an invalid option or value is specified

  • (Turntabler::Error)

    if the command fails



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/turntabler/room_directory.rb', line 61

def all(options = {})
  assert_valid_keys(options, :limit, :skip, :favorites, :available_djs, :genre, :minimum_listeners, :sort)
  assert_valid_values(options[:genre], :rock, :electronic, :indie, :hiphop, :pop, :dubstep) if options[:genre]
  assert_valid_values(options[:sort], :created, :listeners, :random) if options[:sort]
  options = {
    :limit => 20,
    :skip => 0,
    :favorites => false,
    :available_djs => false,
    :minimum_listeners => 1,
    :sort => :listeners
  }.merge(options)

  constraints = []
  constraints << :favorites if options[:favorites]
  constraints << :available_djs if options[:available_djs]
  constraints << :has_people=#{options[:minimum_listeners]}"
  if options[:genre]
    constraints << :genre=#{options[:genre]}"
    options[:sort] = "#{options[:sort]},genre:#{options[:genre]}"
  end

  data = api('room.directory_rooms',
    :section_aware => true,
    :limit => options[:limit],
    :skip => options[:skip],
    :constraints => constraints * ',',
    :sort => options[:sort]
  )
  data['rooms'].map {|attrs| Room.new(client, attrs)}
end

#create(name, attributes = {}) ⇒ Turntabler::Room

Note:

This will automatically enter the room when it is created

Creates a new room with the given name and configuration. This should only be used if the room doesn't already exist.

Examples:

rooms.create("Rock Awesomeness")    # => #<Turntabler::Room ...>

Parameters:

  • name (String)

    The name of the room

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

    The initial attributes for the room

Options Hash (attributes):

  • :privacy (String) — default: "public"

    TheThe level which the room will be made available to others (“public” or “unlisted”)

  • :dj_capacity (Fixnum) — default: 5

    The maximum number of DJs allowed

  • :dj_minimum_points (Fixnum) — default: 0

    The minimum number of points required for a user to DJ

Returns:

Raises:

  • (ArgumentError)

    if an invalid attribute is specified

  • (Turntabler::Error)

    if the command fails



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/turntabler/room_directory.rb', line 28

def create(name, attributes = {})
  assert_valid_keys(attributes, :privacy, :dj_capacity, :dj_minimum_points)
  attributes = {:privacy => 'public', :dj_capacity => 5, :dj_minimum_points => 0}.merge(attributes)

  # Convert attribute names over to their Turntable equivalent
  {:dj_capacity => :max_djs, :dj_minimum_points => :djthreshold}.each do |from, to|
    attributes[to] = attributes.delete(from) if attributes[from]
  end

  data = api('room.create', attributes.merge(:room_name => name))
  room = Room.new(client, attributes.merge(:_id => data['roomid'], :shortcut => data['shortcut'], :name => name))
  room.enter
  room
end

#find(query, options = {}) ⇒ Array<Turntabler::Room>

Finds rooms that match the given query string.

Parameters:

  • query (String)

    The query string to search with

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

    The search options

Options Hash (options):

  • :limit (Fixnum) — default: 20

    The maximum number of rooms to query for

  • :skip (Fixnum) — default: 0

    The number of rooms to skip when loading the results

Returns:

Raises:

  • (ArgumentError)

    if an invalid option is specified

  • (Turntabler::Error)

    if the command fails rooms.find('indie') # => [#<Turntabler::Room …>, …]



116
117
118
119
120
121
122
# File 'lib/turntabler/room_directory.rb', line 116

def find(query, options = {})
  assert_valid_keys(options, :limit, :skip)
  options = {:limit => 20, :skip => 0}.merge(options)

  data = api('room.search', :query => query, :skip => options[:skip])
  data['rooms'].map {|(attrs, *)| Room.new(client, attrs)}
end

#with_friendsArray<Turntabler::Room>

Gets the rooms where the current user's friends are currently listening.

Examples:

rooms.with_friends   # => [#<Turntabler::Room ...>, ...]

Returns:

Raises:



99
100
101
102
103
104
# File 'lib/turntabler/room_directory.rb', line 99

def with_friends
  data = api('room.directory_graph')
  data['rooms'].map do |(attrs, friends)|
    Room.new(client, attrs.merge(:friends => friends))
  end
end