class Namira::Request

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

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

Attributes

body[R]
config[R]
headers[R]
http_method[R]
uri[R]

Public Class Methods

new(uri:, http_method: :get, headers: {}, body: nil, config: {}) click to toggle source

Create a new request

@param uri [String] The request URL @param http_method [Symbol] The HTTP method for the request. (Default `:get`) @param headers [Hash] Additional headers to send with the request. (Default: `{}`) @param body [String, to_s] The body to send. (Default: nil) @param config [Hash] {Namira::Config} overrides

# 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

Public Instance Methods

response() click to toggle source

The {Namira::Response} for the request.

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

@return {Namira::Response}

# File lib/namira/request.rb, line 47
def response
  send_request if @response.nil?
  @response
end
send_async(queue_name: nil, adapter: nil) click to toggle source
# 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_request() click to toggle source

Sends the request.

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

# File lib/namira/request.rb, line 33
def send_request
  @response = _send_request
end

Private Instance Methods

_send_request() click to toggle source
# File lib/namira/request.rb, line 64
def _send_request
  @stack.call(env).response
rescue Addressable::URI::InvalidURIError => e
  raise Namira::Errors::InvalidURIError.new(e.message)
end
env() click to toggle source
# File lib/namira/request.rb, line 54
def env
  Namira::Env.new(
    uri: @uri,
    method: @http_method,
    body: @body,
    headers: @headers,
    config: @config
  )
end