Class: Maze::Servlets::CommandServlet
- Inherits:
-
BaseServlet
- Object
- WEBrick::HTTPServlet::AbstractServlet
- BaseServlet
- Maze::Servlets::CommandServlet
- Defined in:
- lib/maze/servlets/command_servlet.rb
Overview
Allows clients to queue up “commands”, in the form of Ruby hashes, using Maze::Server.commands.add. GET requests made to the /command endpoint will then respond with each queued command in turn.
Constant Summary collapse
- NOOP_COMMAND =
'{"action": "noop", "message": "No commands queued"}'
Instance Method Summary collapse
- #command_after(uuid, response) ⇒ Object
-
#do_GET(request, response) ⇒ Object
Serves the next command, if these is one.
-
#do_OPTIONS(request, response) ⇒ Object
Logs and returns a set of valid headers for this servlet.
- #send_current_command(response) ⇒ Object
Instance Method Details
#command_after(uuid, response) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/maze/servlets/command_servlet.rb', line 52 def command_after(uuid, response) commands = Maze::Server.commands if uuid.empty? index = -1 else index = commands.all.find_index {|command| command[:uuid] == uuid } end if index.nil? # If the UUID given matches the last UUID sent by the server, we can assume the fixture has failed to reset if uuid.eql?(Server.last_command_uuid) send_current_command(response) else msg = "Request invalid - there is no command with a UUID of #{uuid} to follow on from" $logger.error msg response.body = msg response.status = 400 end else if index + 1 < commands.size_all # Respond with the next command in the queue command = commands.get(index + 1) Server.last_command_uuid = command[:uuid] command_json = JSON.pretty_generate(command) response.body = command_json response.status = 200 else # The UUID given was for the last command in the list response.body = NOOP_COMMAND response.status = 200 end end end |
#do_GET(request, response) ⇒ Object
Serves the next command, if these is one.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/maze/servlets/command_servlet.rb', line 18 def do_GET(request, response) if request.query.empty? # Non-idempotent mode - return the "current" command send_current_command(response) else # Idempotent mode after_uuid = request.query['after'] if after_uuid.nil? response.body = "'after' is the only recognised query parameter" response.status = 400 else command_after(after_uuid, response) end end response.header['Access-Control-Allow-Origin'] = '*' end |
#do_OPTIONS(request, response) ⇒ Object
Logs and returns a set of valid headers for this servlet.
89 90 91 92 93 94 |
# File 'lib/maze/servlets/command_servlet.rb', line 89 def do_OPTIONS(request, response) super response.header['Access-Control-Allow-Methods'] = 'POST, OPTIONS' response.status = Server.status_code('OPTIONS') end |
#send_current_command(response) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/maze/servlets/command_servlet.rb', line 37 def send_current_command(response) commands = Maze::Server.commands if commands.size_remaining == 0 response.body = NOOP_COMMAND response.status = 200 else command = commands.current Server.last_command_uuid = command[:uuid] response.body = JSON.pretty_generate(command) response.status = 200 commands.next end end |