Class: Paraspec::HttpClient

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HttpClient

Returns a new instance of HttpClient.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/paraspec/http_client.rb', line 6

def initialize(options={})
  @terminal = options[:terminal]

  @client = Faraday.new(url: "http://localhost:#{Paraspec::MASTER_APP_PORT}") do |client|
    client.adapter :net_http
    if @terminal
      client.options.timeout = 100000
    else
      client.options.timeout = DrbHelpers::WAIT_TIME
    end
  end
end

Instance Method Details

#request(action, payload = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/paraspec/http_client.rb', line 19

def request(action, payload=nil)
  url = '/' + action
  start_time = Time.now
  begin
    resp = @client.post(url) do |req|
      if payload
        req.headers['content-type'] = 'application/json'
        req.body = payload.to_json
      end
    end
    if resp.status != 200
      raise "Request failed: #{url} (#{resp.status})"
    end
    JSON.parse(resp.body)
  rescue Faraday::ConnectionFailed
    if !@terminal && Time.now - start_time > DrbHelpers::WAIT_TIME
      raise
    else
      sleep 0.1
      retry
    end
  end
end