Class: EzClient::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb, url, options) ⇒ Request

Returns a new instance of Request.



6
7
8
9
10
# File 'lib/ezclient/request.rb', line 6

def initialize(verb, url, options)
  self.verb = verb.to_s.upcase
  self.url = url
  self.options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/ezclient/request.rb', line 4

def options
  @options
end

#urlObject

Returns the value of attribute url.



4
5
6
# File 'lib/ezclient/request.rb', line 4

def url
  @url
end

#verbObject

Returns the value of attribute verb.



4
5
6
# File 'lib/ezclient/request.rb', line 4

def verb
  @verb
end

Instance Method Details

#api_auth!(*args) ⇒ Object



33
34
35
36
37
# File 'lib/ezclient/request.rb', line 33

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

#bodyObject



39
40
41
42
43
44
# File 'lib/ezclient/request.rb', line 39

def body
  http_request.body.source.rewind if http_request.body.source.respond_to?(:rewind)
  body = +""
  http_request.body.each { |chunk| body << chunk }
  body
end

#headersObject



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

def headers
  http_request.headers.to_h
end

#performObject



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

def perform
  http_response = perform_request

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

#perform!Object



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

def perform!
  response = perform

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