Class: Hokusai::Automation::Client

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

Defined Under Namespace

Classes: Block

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Client

Returns a new instance of Client.



90
91
92
93
94
95
96
# File 'ui/src/hokusai/automation/client.rb', line 90

def initialize(uri)
  @host = uri.host
  @port = uri.port
  @http = RestClient::Resource.new("#{uri.host}:#{uri.port}")

  wait_until_ready
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



80
81
82
# File 'ui/src/hokusai/automation/client.rb', line 80

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



80
81
82
# File 'ui/src/hokusai/automation/client.rb', line 80

def port
  @port
end

Class Method Details

.start(host = "localhost", port = 3000) ⇒ Object



82
83
84
# File 'ui/src/hokusai/automation/client.rb', line 82

def self.start(host = "localhost", port=3000)
  new URI.parse("http://#{host}:#{port}")
end

.start_unix(path) ⇒ Object



86
87
88
# File 'ui/src/hokusai/automation/client.rb', line 86

def self.start_unix(path)
  new URI.parse("unix://#{path}")
end

Instance Method Details

#block(selector) ⇒ Object



98
99
100
101
102
103
104
105
# File 'ui/src/hokusai/automation/client.rb', line 98

def block(selector)
  block = Client::Block.locate(selector, self)
  if block_given?
    yield block
  else
    block
  end
end

#blocks(selector) ⇒ Object



107
108
109
# File 'ui/src/hokusai/automation/client.rb', line 107

def blocks(selector)
  Client::Block.locate_all(selector, self)
end

#headersObject



162
163
164
# File 'ui/src/hokusai/automation/client.rb', line 162

def headers
  {"Content-Type" => "application/json" }
end

#make_request(command, json) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'ui/src/hokusai/automation/client.rb', line 111

def make_request(command, json)
  begin
    res = @http[command].post(json.to_json, headers)

    if res.code == 200
      JSON.parse(res.body)["value"]
    else
      raise Automation::Error.new("Got status code #{res.code} - #{res.body}")
    end
  rescue StandardError => ex
    raise Automation::Error.new("Failed to make request: #{ex}")
  end
end

#wait_until_ready(timeout: 2, interval: 0.2) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ui/src/hokusai/automation/client.rb', line 143

def wait_until_ready(timeout: 2, interval: 0.2)
  time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  val = nil

  while (Process.clock_gettime(Process::CLOCK_MONOTONIC) - time) < timeout
    begin
      @http.start
      res = @http["ready"].post
      if res.code == 200
        break
      else
        sleep interval
      end
    rescue
      sleep interval
    end
  end
end

#wait_until_stopped(timeout: 2, interval: 0.2) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'ui/src/hokusai/automation/client.rb', line 125

def wait_until_stopped(timeout: 2, interval: 0.2)
  time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  val = nil

  while (Process.clock_gettime(Process::CLOCK_MONOTONIC) - time) < timeout
    begin
      res = @http["ready"].post
      if res.code != 200
        break
      else
        sleep interval
      end
    rescue
      sleep interval
    end
  end
end