Class: Croesus::WebClient

Inherits:
Object show all
Defined in:
lib/croesus/web_client/web_client.rb

Class Method Summary collapse

Class Method Details

.internal_request(request, timeout) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/croesus/web_client/web_client.rb', line 34

def self.internal_request(request, timeout)
  HTTP_HEADERS.each { |key, value| request.add_header(key, value) }
  response = nil

  begin
    case request.method
    when :get
      response = RestClient::Request.execute(
        method: :get,
        url:     request.url,
        headers: request.headers,
        timeout: timeout
      )
    when :post
      response = RestClient::Request.execute(
        method: :post,
        url:     request.url,
        payload: request.body,
        headers: request.headers,
        timeout: timeout
      )
    when :delete
      response = RestClient::Request.execute(
        method: :delete,
        url:     request.url,
        payload: request.body,
        headers: request.headers,
        timeout: timeout
      )
    end
  rescue RestClient::RequestTimeout
    raise 'Request Timeout'
  rescue RestClient::Exception => e
    response = e.response
  end

  Croesus::WebResponse.new(response)
end

.request(method, url, headers, body, timeout, &callback) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/croesus/web_client/web_client.rb', line 22

def self.request(method, url, headers, body, timeout, &callback)
  request = Croesus::WebRequest.new(method, url, headers, body)

  if callback
    Thread.new do
      callback.call(self.internal_request(request, timeout))
    end
  else
    self.internal_request(request, timeout)
  end
end