Class: XRemoteBot::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/xremotebot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, path, api_key) ⇒ Server

Returns a new instance of Server.



51
52
53
54
55
56
# File 'lib/xremotebot.rb', line 51

def initialize(host, port, path, api_key)
  @ws = WS.new(host, port, path)
  if authentication_required 
    raise Exception, 'Authentication failed' unless authenticate(api_key)
  end
end

Instance Attribute Details

#wsObject (readonly)

Returns the value of attribute ws.



50
51
52
# File 'lib/xremotebot.rb', line 50

def ws
  @ws
end

Instance Method Details

#authenticate(api_key) ⇒ Object



74
75
76
# File 'lib/xremotebot.rb', line 74

def authenticate(api_key)
  send_ws_msg 'global', 'authenticate', api_key
end

#authentication_requiredObject



70
71
72
# File 'lib/xremotebot.rb', line 70

def authentication_required
  send_ws_msg 'global', 'authentication_required'
end

#fetch_robotObject



82
83
84
# File 'lib/xremotebot.rb', line 82

def fetch_robot
  send_ws_msg 'global', 'fetch_robot'
end

#get_robotsObject



78
79
80
# File 'lib/xremotebot.rb', line 78

def get_robots
  send_ws_msg 'global', 'get_robots'
end

#reserve(robot_model, robot_id) ⇒ Object



86
87
88
# File 'lib/xremotebot.rb', line 86

def reserve(robot_model, robot_id)
  send_ws_msg 'global', 'reserve', robot_model, robot_id
end

#send_ws_msg(entity, method, *args) ⇒ Object

Raises:

  • (Exception)


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/xremotebot.rb', line 58

def send_ws_msg(entity, method, *args)
  msg = JSON.dump({
    entity: entity,
    method: method,
    args: args,
  })
  ws.send(msg)
  response = JSON.load ws.receive
  raise Exception, response['message'] if response['response'] == 'error'
  response['value']
end