Class: WarmBlanket::Requester

Inherits:
Object
  • Object
show all
Defined in:
lib/warm_blanket/requester.rb

Overview

Issues one request per call to the configured endpoint

Constant Summary collapse

InvalidHTTPVerb =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, default_headers:, endpoints:, logger: WarmBlanket.config.logger, connection_factory: Faraday) ⇒ Requester

Returns a new instance of Requester.



42
43
44
45
46
47
48
49
# 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

Instance Method Details

#callObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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