Class: FieldReports::HttpProxy

Inherits:
Proxy
  • Object
show all
Defined in:
lib/field_reports/http_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_address) ⇒ HttpProxy

Returns a new instance of HttpProxy.



10
11
12
13
# File 'lib/field_reports/http_proxy.rb', line 10

def initialize(base_address)
  url = URI.parse(base_address)
  @http = Net::HTTP.new(url.host, url.port)
end

Instance Method Details

#parse(pdf) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/field_reports/http_proxy.rb', line 51

def parse(pdf)
  begin
    req = Net::HTTP::Post.new('/parse')
    req["Content-Type"] = "application/pdf"
    req.body = pdf
    res = @http.request(req)
    case res
    when Net::HTTPOK
      return JSON.parse(res.body)
    when Net::HTTPClientError, Net::HTTPInternalServerError
      res.value
    end
  rescue Net::ProtocolError => exn
    raise ReportsError, response_message(res)
  rescue => exn
    raise ReportsError, exn_message(exn)
  end
end

#render(param) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/field_reports/http_proxy.rb', line 32

def render(param)
  begin
    req = Net::HTTP::Post.new('/render')
    req["Content-Type"] = "application/json"
    req.body = param.is_a?(Hash) ? param.to_json : param
    res = @http.request(req)
    case res
    when Net::HTTPOK
      return res.body
    when Net::HTTPClientError, Net::HTTPInternalServerError
      res.value
    end
  rescue Net::ProtocolError => exn
    raise ReportsError, response_message(res)
  rescue => exn
    raise ReportsError, exn_message(exn)
  end
end

#versionObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/field_reports/http_proxy.rb', line 15

def version
  begin
    req = Net::HTTP::Get.new('/version')
    res = @http.request(req)
    case res
    when Net::HTTPOK
      return res.body
    when Net::HTTPClientError, Net::HTTPInternalServerError
      res.value
    end
  rescue Net::ProtocolError => exn
    raise ReportsError, response_message(res)
  rescue => exn
    raise ReportsError, exn_message(exn)
  end
end