Class: TurntableAPI::Bot
- Inherits:
-
Object
- Object
- TurntableAPI::Bot
- Defined in:
- lib/turntable_api/bot.rb
Instance Attribute Summary collapse
-
#connected ⇒ Object
readonly
Returns the value of attribute connected.
-
#roomid ⇒ Object
readonly
Returns the value of attribute roomid.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Bot
constructor
A new instance of Bot.
- #method_missing(meth, *args) ⇒ Object
-
#on_command(command, &blk) ⇒ Object
triggered when we receive a command from Turntable.FM.
- #on_error(&blk) ⇒ Object
- #on_message(msg) ⇒ Object
- #on_ready(&blk) ⇒ Object
-
#on_response(cmd, opts = {}, &blk) ⇒ Object
sends a command and sets up a callback that triggers when the reply is received.
- #room_register(roomid) ⇒ Object
- #room_vote(opts) ⇒ Object (also: #vote)
-
#send_command(command, opts = {}) ⇒ Object
send a command to Turntable.FM.
- #start ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Bot
Returns a new instance of Bot.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/turntable_api/bot.rb', line 11 def initialize(opts = {}) @userid = opts[:userid] @clientid = opts[:clientid] || "#{Time.now.to_i}-#{rand}" @auth = opts[:auth] @logger = opts[:logger] || Logger.new(STDOUT) @msgId = 0 @command_handlers ||= {} @response_handler ||= {} @received_heartbeat = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/turntable_api/bot.rb', line 52 def method_missing(meth, *args) @logger.debug "method_missing #{meth}: #{args}" hash = args[0] unless args.empty? hash ||= {} send_command(meth, hash) end |
Instance Attribute Details
#connected ⇒ Object (readonly)
Returns the value of attribute connected.
9 10 11 |
# File 'lib/turntable_api/bot.rb', line 9 def connected @connected end |
#roomid ⇒ Object (readonly)
Returns the value of attribute roomid.
9 10 11 |
# File 'lib/turntable_api/bot.rb', line 9 def roomid @roomid end |
Instance Method Details
#on_command(command, &blk) ⇒ Object
triggered when we receive a command from Turntable.FM
66 67 68 |
# File 'lib/turntable_api/bot.rb', line 66 def on_command(command, &blk) @command_handlers[command.to_sym] = blk end |
#on_error(&blk) ⇒ Object
74 75 76 |
# File 'lib/turntable_api/bot.rb', line 74 def on_error(&blk) @error_handler = blk end |
#on_message(msg) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/turntable_api/bot.rb', line 78 def (msg) if msg == "~m~10~m~no_session" authenticate elsif msg =~ /~m~[0-9]+~m~(~h~[0-9]+)/ hb = $1 @ready_handler.call unless @received_heartbeat or @ready_handler.nil? @received_heartbeat = true send_text(hb) else msg =~ /~m~\d*~m~(\{.*\})/ json = JSON.parse($1) command = json["command"] err = json["err"] msgid = json["msgid"] @error_handler.call(err) unless @error_handler.nil? or err.nil? unless command.nil? blk = @command_handlers[command.to_sym] blk.call(json) unless blk.nil? end unless @response_handler[msgid].nil?() blk = @response_handler[msgid] blk.call(json) unless blk.nil? @response_handler.delete(msgid) end end end |
#on_ready(&blk) ⇒ Object
70 71 72 |
# File 'lib/turntable_api/bot.rb', line 70 def on_ready(&blk) @ready_handler = blk end |
#on_response(cmd, opts = {}, &blk) ⇒ Object
sends a command and sets up a callback that triggers when the reply is received
47 48 49 50 |
# File 'lib/turntable_api/bot.rb', line 47 def on_response(cmd, opts={}, &blk) msg_id = send_command cmd, opts @response_handler[msg_id] = blk end |
#room_register(roomid) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/turntable_api/bot.rb', line 26 def room_register(roomid) @ws.close unless @ws.nil? if roomid.instance_of?(Hash) then roomid = roomid[:roomid] end connect(roomid) call_api 'room.register', :roomid => roomid @roomid = roomid end |
#room_vote(opts) ⇒ Object Also known as: vote
34 35 36 37 38 39 40 41 42 |
# File 'lib/turntable_api/bot.rb', line 34 def room_vote(opts) dir = opts[:val] songid = opts[:songid] raise ArgumentError, "dir and songid are required" if songid.nil? or dir.nil? opts[:vh] ||= hash("#{@roomid}#{dir}#{songid}") opts[:th] ||= hash(rand.to_s) opts[:ph] ||= hash(rand.to_s) send_command('room_vote', opts) end |
#send_command(command, opts = {}) ⇒ Object
send a command to Turntable.FM
60 61 62 63 |
# File 'lib/turntable_api/bot.rb', line 60 def send_command(command, opts={}) command = command.to_s.sub('_', '.') call_api(command, opts) end |
#start ⇒ Object
22 23 24 |
# File 'lib/turntable_api/bot.rb', line 22 def start connect end |