Class: PartyGoerClient

Inherits:
Object
  • Object
show all
Defined in:
lib/partygoer-client.rb

Overview

Connects to a partygoer instance, performs an initial poll, and then polls every 5 minutes. In between polls the client stays in sync using faye to recieve push updates

Constant Summary collapse

@@logger =
Logger.new('partygoerclient.log', 2, 1_000_000)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ PartyGoerClient

Returns a new instance of PartyGoerClient.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/partygoer-client.rb', line 21

def initialize(server)
  @server = server

  @played = []
  @queued = []
  @playing = nil

  @played_lock = Mutex.new
  @queued_lock = Mutex.new
  @playing_lock = Mutex.new

  @event_loop = nil

  @api = RestClient::Resource.new("#{@server}/api/queue", headers:
             { content_type: 'application/json', accept: 'application/json' })
  @client = Faye::Client.new("#{@server}:9292/faye")
  logger.info { "Client initialized with server: #{server}" }
  @is_playing = false
  start
end

Instance Attribute Details

#playedObject (readonly)

Returns the value of attribute played.



13
14
15
# File 'lib/partygoer-client.rb', line 13

def played
  @played
end

#playingObject

Returns the value of attribute playing.



13
14
15
# File 'lib/partygoer-client.rb', line 13

def playing
  @playing
end

#queuedObject (readonly)

Returns the value of attribute queued.



13
14
15
# File 'lib/partygoer-client.rb', line 13

def queued
  @queued
end

Instance Method Details

#currently_queuedObject



105
106
107
# File 'lib/partygoer-client.rb', line 105

def currently_queued
  queued
end

#loggerObject



113
114
115
# File 'lib/partygoer-client.rb', line 113

def logger
  @@logger
end

#now_playingObject

Aliases



101
102
103
# File 'lib/partygoer-client.rb', line 101

def now_playing
  playing
end

#recently_playedObject



109
110
111
# File 'lib/partygoer-client.rb', line 109

def recently_played
  played
end

#skip?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
# File 'lib/partygoer-client.rb', line 65

def skip?
  if @playing['num_upvotes'] == 0
    @playing['num_downvotes'] >= 2
  else
    @playing['num_downvotes'] >= (2 * @playing['num_upvotes'])
  end
rescue
  logger.error("Failed to check skip criteria:\n#{$!}\n#{$@.join("\n\t")}")
end

#startObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/partygoer-client.rb', line 81

def start
  pull_update
  return if @event_loop
  logger.info { 'Starting event loop' }
  start_event_loop

  at_exit do
    stop
  end
end

#stopObject



92
93
94
95
96
97
98
# File 'lib/partygoer-client.rb', line 92

def stop
  return unless @event_loop
  logger.info { 'Stopping event loop' }
  EM.stop_event_loop
  @event_loop.kill
  @event_loop = nil
end

#suggest(track) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/partygoer-client.rb', line 52

def suggest(track)
  case track
  when String
    payload = { track: { spotify_uri: track } }
  when Hash
    payload = { track: track }
  end
  EM.defer(proc { @api['/tracks'].post(payload) })
  logger.info { "Suggested #{track.inspect}" }
rescue
  logger.error("Failed to suggest track: #{track.inspect}\n#{$!}\n#{$@.join("\n\t")}")
end

#up_nextObject



75
76
77
78
79
# File 'lib/partygoer-client.rb', line 75

def up_next
  @queued[1]
rescue
  logger.error("Failed to check up next:\n#{$!}\n#{$@.join("\n\t")}")
end