Class: EzClient::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/ezclient/request.rb

Constant Summary collapse

OPTION_KEYS =
%i[
  body
  form
  json
  metadata
  params
  query
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb, url, options) ⇒ Request

Returns a new instance of Request.



15
16
17
18
19
20
21
# File 'lib/ezclient/request.rb', line 15

def initialize(verb, url, options)
  self.verb = verb.to_s.upcase
  self.url = url
  self.client = options.delete(:client)
  self.options = options
  EzClient::CheckOptions.call(options, OPTION_KEYS + EzClient::Client::REQUEST_OPTION_KEYS)
end

Instance Attribute Details

#elapsed_secondsObject

Returns the value of attribute elapsed_seconds.



13
14
15
# File 'lib/ezclient/request.rb', line 13

def elapsed_seconds
  @elapsed_seconds
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/ezclient/request.rb', line 13

def options
  @options
end

#urlObject

Returns the value of attribute url.



13
14
15
# File 'lib/ezclient/request.rb', line 13

def url
  @url
end

#verbObject

Returns the value of attribute verb.



13
14
15
# File 'lib/ezclient/request.rb', line 13

def verb
  @verb
end

Instance Method Details

#add_headers!(new_headers) ⇒ Object



64
65
66
# File 'lib/ezclient/request.rb', line 64

def add_headers!(new_headers)
  http_request.headers.merge!(new_headers)
end

#api_auth!(*args) ⇒ Object



44
45
46
47
48
# File 'lib/ezclient/request.rb', line 44

def api_auth!(*args)
  raise "ApiAuth gem is not loaded" unless defined?(ApiAuth)
  ApiAuth.sign!(http_request, *args)
  self
end

#bodyObject



54
55
56
57
58
# File 'lib/ezclient/request.rb', line 54

def body
  body = +""
  http_request.body.each { |chunk| body << chunk }
  body
end

#headersObject



60
61
62
# File 'lib/ezclient/request.rb', line 60

def headers
  http_request.headers.to_h
end

#http_optionsObject



68
69
70
# File 'lib/ezclient/request.rb', line 68

def http_options
  @http_options ||= http_client.default_options.merge(ssl_context: options[:ssl_context])
end

#performObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/ezclient/request.rb', line 23

def perform
  http_response = perform_request

  EzClient::Response.new(http_response, http_request).tap do |response|
    on_complete.call(self, response, options[:metadata])
  end
rescue => error
  on_error.call(self, error, options[:metadata])
  error_wrapper.call(self, error, options[:metadata])
end

#perform!Object



34
35
36
37
38
39
40
41
42
# File 'lib/ezclient/request.rb', line 34

def perform!
  response = perform

  if response.error?
    raise EzClient::ResponseStatusError, response
  else
    response
  end
end

#uriObject



50
51
52
# File 'lib/ezclient/request.rb', line 50

def uri
  http_request.uri
end