Class: Hokusai::Automation::App

Inherits:
Object
  • Object
show all
Defined in:
ui/src/hokusai/automation/server.rb

Constant Summary collapse

ROUTES =
[
  ["/commands/locate", DriverCommands::Locate],
  ["/commands/invoke", DriverCommands::Invoke],
  ["/commands/attribute", DriverCommands::GetAttribute],
  ["/commands/click", DriverCommands::TriggerMouseClick],
  ["/commands/drag", DriverCommands::TriggerMouseDrag],
  ["/commands/hover", DriverCommands::TriggerMouseHover],
  ["/commands/mousemove", DriverCommands::TriggerMouseMove],
  ["/commands/mousewheel", DriverCommands::TriggerMouseWheel],
  ["/commands/keyboard", DriverCommands::TriggerKeyboard]
].to_h

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver) ⇒ App

Returns a new instance of App.



18
19
20
# File 'ui/src/hokusai/automation/server.rb', line 18

def initialize(driver)
  @driver = driver
end

Instance Attribute Details

#driverObject (readonly)

Returns the value of attribute driver.



12
13
14
# File 'ui/src/hokusai/automation/server.rb', line 12

def driver
  @driver
end

Class Method Details

.queueObject



14
15
16
# File 'ui/src/hokusai/automation/server.rb', line 14

def self.queue
  @queue ||= {}
end

Instance Method Details

#call(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'ui/src/hokusai/automation/server.rb', line 34

def call(env)
  request = Rack::Request.new(env)

  unless request.post?
    return respond(404, "Not Found", {})
  end

  return respond(200, "") if request.path == "/ready"

  if command_klass = ROUTES[request.path]
    json = JSON.parse(request.body.string, symbolize_names: true)
    command = command_klass.new(json)
    driver.execute(command)

    Log.debug { "Pushed #{command.class} command #{driver.queue.commands}" }

    poll(command.request_id)
  else
    respond(403, "Bad Request", {})
  end
end

#parse_body(request) ⇒ Object



88
89
90
# File 'ui/src/hokusai/automation/server.rb', line 88

def parse_body(request)
  JSON.parse(request.body, symbolize_names: true) unless request.body.nil?
end

#poll(request_id) ⇒ Object



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
84
85
86
# File 'ui/src/hokusai/automation/server.rb', line 56

def poll(request_id)
  poll_timeout = 2
  poll_interval = 0.02
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  while (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) < poll_timeout
    begin
      if payload = driver.results[request_id]
        driver.results.delete(request_id)

        _, value = payload

        Log.debug { "request id found! sending payload #{value}" }

        if value.is_a?(Automation::Error)
          message = value.message || "Something went wrong"

          return respond(500, "Error occurred: #{message}" , {"Content-Type" => "text/plain"})
        else
          return respond(200, {"value" => value}.to_json)
        end
      end

      sleep poll_interval
    rescue ex
      respond(500, "Error occurred while processing command: #{ex.message}", {"Content-Type" => "text/plain"})
    end
  end

  respond(408, "Timeout", {"Content-Type" => "text/plain"})
end

#respond(status, message, headers = {"Content-Type" => "application/json"}) ⇒ Object



92
93
94
# File 'ui/src/hokusai/automation/server.rb', line 92

def respond(status, message, headers = {"Content-Type" => "application/json"})
  [status, headers, message]
end