class WarmBlanket::Requester

Issues one request per call to the configured endpoint

Constants

InvalidHTTPVerb
SUPPORTED_VERBS

Attributes

base_url[R]
connection_factory[R]
default_headers[R]
endpoints[R]
logger[R]
next_endpoint_position[RW]

Public Class Methods

new(base_url:, default_headers:, endpoints:, logger: WarmBlanket.config.logger, connection_factory: Faraday) click to toggle source
# File lib/warm_blanket/requester.rb, line 42
def initialize(base_url:, default_headers:, endpoints:, logger: WarmBlanket.config.logger, connection_factory: Faraday)
  @base_url = base_url
  @default_headers = default_headers
  @endpoints = endpoints
  @logger = logger
  @connection_factory = connection_factory
  @next_endpoint_position = 0
end

Public Instance Methods

call() click to toggle source
# File lib/warm_blanket/requester.rb, line 51
def call
  connection = connection_factory.new(url: base_url)

  endpoint = next_endpoint

  http_verb = extract_verb(endpoint)

  logger.debug "Requesting #{endpoint.fetch(http_verb)}"

  response = connection.public_send(http_verb) do |request|
    apply_headers(request, default_headers)
    apply_headers(request, endpoint[:headers])
    request.url(endpoint.fetch(http_verb))
    request.body = endpoint[:body] if endpoint[:body]
  end

  if response.status == 200
    logger.debug "Request successful"
  else
    logger.warn "Request to #{endpoint.fetch(http_verb)} failed with code #{response.status}"
  end

  nil
end

Private Instance Methods

apply_headers(request, headers) click to toggle source
# File lib/warm_blanket/requester.rb, line 78
def apply_headers(request, headers)
  headers&.each do |header, value|
    request.headers[header.to_s] = value
  end
end
extract_verb(endpoint) click to toggle source
# File lib/warm_blanket/requester.rb, line 90
def extract_verb(endpoint)
  SUPPORTED_VERBS.each do |verb|
    return verb if endpoint.key?(verb)
  end

  raise InvalidHTTPVerb, "Unsupported or missing HTTP verb for request: #{endpoint.inspect}"
end
next_endpoint() click to toggle source
# File lib/warm_blanket/requester.rb, line 84
def next_endpoint
  next_endpoint = endpoints[next_endpoint_position]
  self.next_endpoint_position = (next_endpoint_position + 1) % endpoints.size
  next_endpoint
end