Class: Gupshup::HTTP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gupshup-rb/http/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



9
10
11
# File 'lib/gupshup-rb/http/http_client.rb', line 9

def initialize
  @adapter = Faraday.default_adapter
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



7
8
9
# File 'lib/gupshup-rb/http/http_client.rb', line 7

def adapter
  @adapter
end

#last_requestObject (readonly)

Returns the value of attribute last_request.



8
9
10
# File 'lib/gupshup-rb/http/http_client.rb', line 8

def last_request
  @last_request
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



8
9
10
# File 'lib/gupshup-rb/http/http_client.rb', line 8

def last_response
  @last_response
end

Instance Method Details

#_request(request) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gupshup-rb/http/http_client.rb', line 13

def _request(request)
  @connection = Faraday.new(url: "#{request.host}:#{request.port.to_s}", ssl: { verify: true }) do |f|
    f.request :url_encoded
    f.adapter @adapter
    f.headers = request.headers
  end
  @last_request = request
  @last_response = nil
  response = send(request)
  if response.status == 504
    object = { message: 'Request timeout', code: 504 }.to_json
  elsif response.body && !response.body.empty?
    object = response.body
  elsif response.status == 400
    object = { message: 'Bad request', code: 400 }.to_json
  end

  gupshup_response = Gupshup::Response.new(response.status, object, headers: response.headers)
  @last_response = gupshup_response

  gupshup_response
end

#request(host, port, method, url, data = {}, headers = {}, params = {}, auth = nil, timeout = nil) ⇒ Object



44
45
46
47
48
# File 'lib/gupshup-rb/http/http_client.rb', line 44

def request(host, port, method, url, data = {}, headers = {}, params = {}, auth = nil, timeout = nil)
  request = Gupshup::Request.new(host, port, method, url, params, data, headers)
  _request(request)

end

#send(request) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/gupshup-rb/http/http_client.rb', line 36

def send(request)
  @connection.send(request.method.downcase.to_sym,
                   request.url,
                   request.method == 'GET' ? request.params : request.data)
rescue Faraday::Error => e
  raise Gupshup::REST::GupshupError, e
end