Class: Namira::Request

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

Overview

The request class is used to create and send HTTP requests.

response = Namira::Request.new(uri: 'https://httpbin.org/headers').response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, http_method: :get, headers: {}, body: nil, config: {}) ⇒ Request

Create a new request

Parameters:

  • uri (String)

    The request URL

  • http_method (Symbol) (defaults to: :get)

    The HTTP method for the request. (Default ‘:get`)

  • headers (Hash) (defaults to: {})

    Additional headers to send with the request. (Default: ‘{}`)

  • body (String, #to_s) (defaults to: nil)

    The body to send. (Default: nil)

  • config (Hash) (defaults to: {})

    Config overrides



19
20
21
22
23
24
25
26
# File 'lib/namira/request.rb', line 19

def initialize(uri:, http_method: :get, headers: {}, body: nil, config: {})
  @uri         = uri
  @http_method = http_method
  @headers     = Hash(headers)
  @body        = body
  @config      = Namira.configure.to_h.merge(Hash(config))
  @stack       = Namira::Stack.default
end

Instance Attribute Details

#http_methodObject (readonly)

Returns the value of attribute http_method.



9
10
11
# File 'lib/namira/request.rb', line 9

def http_method
  @http_method
end

#uriObject (readonly)

Returns the value of attribute uri.



9
10
11
# File 'lib/namira/request.rb', line 9

def uri
  @uri
end

Instance Method Details

#responseNamira::Response

The Namira::Response for the request.

If the request hasn’t been sent yet calling this will get the request.

Returns:



42
43
44
45
# File 'lib/namira/request.rb', line 42

def response
  send_request if @response.nil?
  @response
end

#send_requestObject

Sends the request.

Every time this method is called a network request will be sent.



32
33
34
# File 'lib/namira/request.rb', line 32

def send_request
  @response = _send_request
end