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



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

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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



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

def http_method
  @http_method
end

#uriObject (readonly)

Returns the value of attribute uri.



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

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:



47
48
49
50
# File 'lib/namira/request.rb', line 47

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

#send_async(queue_name: nil, adapter: nil) ⇒ Object



37
38
39
# File 'lib/namira/request.rb', line 37

def send_async(queue_name: nil, adapter: nil)
  Namira::Async::Performer.schedule(self, adapter, queue_name)
end

#send_requestObject

Sends the request.

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



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

def send_request
  @response = _send_request
end