class Caldera::Node
Constants
- LOGGER
Attributes
client[R]
@return [Caldera::Client]
http[R]
@return [Net::HTTP]
stats[R]
@return [Caldera::Model::Stats]
websocket[R]
@return [Caldera::WebSocket]
Public Class Methods
new(rest_uri, ws_uri, auth, client)
click to toggle source
# File lib/caldera/node.rb, line 27 def initialize(rest_uri, ws_uri, auth, client) @client = client @authorization = auth @websocket = WebSocket.new(ws_uri, auth, @client.num_shards, @client.user_id) register_handlers @stats = nil @available = false @http = Net::HTTP.new(rest_uri.host, rest_uri.port) end
Public Instance Methods
create_player(guild_id, session_id, event)
click to toggle source
# File lib/caldera/node.rb, line 47 def create_player(guild_id, session_id, event) LOGGER.info { "Creating player for #{guild_id}" } @websocket.send_json({ op: :voiceUpdate, guildId: guild_id, sessionId: session_id, event: event }) player = Player.new(guild_id, self, client) @client.players[guild_id] = player end
load_tracks(id)
click to toggle source
# File lib/caldera/node.rb, line 60 def load_tracks(id) resp = get('/loadtracks', query: { identifier: id }) Model::LoadTracks.new(resp) end
send_json(data)
click to toggle source
# File lib/caldera/node.rb, line 73 def send_json(data) @websocket.send_json(data) end
soundcloud_search(search_string)
click to toggle source
# File lib/caldera/node.rb, line 69 def soundcloud_search(search_string) load_tracks("scsearch:#{search_string}") end
start()
click to toggle source
# File lib/caldera/node.rb, line 37 def start LOGGER.info { "Connecting to #{@websocket.url}" } @websocket.start end
stop()
click to toggle source
# File lib/caldera/node.rb, line 42 def stop LOGGER.info { "Disconnecting from #{@websocket.url}" } @websocket.close end
youtube_search(search_string)
click to toggle source
# File lib/caldera/node.rb, line 65 def youtube_search(search_string) load_tracks("ytsearch:#{search_string}") end
Private Instance Methods
get(path, query: {})
click to toggle source
# File lib/caldera/node.rb, line 108 def get(path, query: {}) req_path = "#{path}?#{URI.encode_www_form(query)}" resp = @http.get(req_path, { Authorization: @authorization }) case resp.code.to_i when 200...300 JSON.load(resp.body) else # TODO resp.error! end end
handle_event(data)
click to toggle source
# File lib/caldera/node.rb, line 99 def handle_event(data) type = transform_type(data['type']) emit(type, data) end
handle_player_update(data)
click to toggle source
# File lib/caldera/node.rb, line 87 def handle_player_update(data) @client.get_player(data['guildId']).state = data['state'] end
handle_stats(data)
click to toggle source
# File lib/caldera/node.rb, line 91 def handle_stats(data) data_without_op = data.clone data_without_op.delete('op') @stats = data_without_op emit(:stats_update, Events::StatsEvent.new(data, self)) end
register_handlers()
click to toggle source
# File lib/caldera/node.rb, line 79 def register_handlers @websocket.on(:playerUpdate, &method(:handle_player_update)) @websocket.on(:stats, &method(:handle_stats)) @websocket.on(:event, &method(:handle_event)) @websocket.on(:open) { @available = true } @websocket.on(:close) { @available = false } end
transform_type(type)
click to toggle source
# File lib/caldera/node.rb, line 104 def transform_type(type) (type[0] + type[1..-1].sub(/Event$/, '').gsub(/([A-Z])/, '_\1')).downcase end