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.



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

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.



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

def ws
  @ws
end

Instance Method Details

#authenticate(api_key) ⇒ Object



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

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

#authentication_requiredObject



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

def authentication_required
  send_ws_msg 'global', 'authentication_required'
end

#fetch_robotObject



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

def fetch_robot
  send_ws_msg 'global', 'fetch_robot'
end

#get_robotsObject



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

def get_robots
  send_ws_msg 'global', 'get_robots'
end

#reserve(robot_model, robot_id) ⇒ Object



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

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)


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

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