Class: EzClient::Request

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

Defined Under Namespace

Classes: RedirectCookieState

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.



60
61
62
63
64
65
66
# File 'lib/ezclient/request.rb', line 60

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.



58
59
60
# File 'lib/ezclient/request.rb', line 58

def elapsed_seconds
  @elapsed_seconds
end

#optionsObject

Returns the value of attribute options.



58
59
60
# File 'lib/ezclient/request.rb', line 58

def options
  @options
end

#urlObject

Returns the value of attribute url.



58
59
60
# File 'lib/ezclient/request.rb', line 58

def url
  @url
end

#verbObject

Returns the value of attribute verb.



58
59
60
# File 'lib/ezclient/request.rb', line 58

def verb
  @verb
end

Instance Method Details

#add_headers!(new_headers) ⇒ Object



110
111
112
# File 'lib/ezclient/request.rb', line 110

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

#api_auth!(*args) ⇒ Object



89
90
91
92
93
94
# File 'lib/ezclient/request.rb', line 89

def api_auth!(*args)
  raise "ApiAuth gem is not loaded" unless defined?(ApiAuth)

  ApiAuth.sign!(api_auth_request, *args)
  self
end

#bodyObject



100
101
102
103
104
# File 'lib/ezclient/request.rb', line 100

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

#headersObject



106
107
108
# File 'lib/ezclient/request.rb', line 106

def headers
  http_request.headers.to_h
end

#http_optionsObject



114
115
116
# File 'lib/ezclient/request.rb', line 114

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

#performObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/ezclient/request.rb', line 68

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



79
80
81
82
83
84
85
86
87
# File 'lib/ezclient/request.rb', line 79

def perform!
  response = perform

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

#uriObject



96
97
98
# File 'lib/ezclient/request.rb', line 96

def uri
  http_request.uri
end