Class: MetalArchives::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/metal_archives/http_client.rb

Overview

Generic HTTP client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint = MetalArchives.config.endpoint) ⇒ HTTPClient

Returns a new instance of HTTPClient.



12
13
14
15
# File 'lib/metal_archives/http_client.rb', line 12

def initialize(endpoint = MetalArchives.config.endpoint)
  @endpoint = endpoint
  @metrics = { hit: 0, miss: 0 }
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



10
11
12
# File 'lib/metal_archives/http_client.rb', line 10

def endpoint
  @endpoint
end

#metricsObject (readonly)

Returns the value of attribute metrics.



10
11
12
# File 'lib/metal_archives/http_client.rb', line 10

def metrics
  @metrics
end

Instance Method Details

#get(path, params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/metal_archives/http_client.rb', line 17

def get(path, params = {})
  response = http
    .get(url_for(path), params: params)

  # Log cache status
  status = response.headers["x-cache-status"]&.downcase&.to_sym
  MetalArchives.config.logger.info "Cache #{status} for #{path}" if status

  case status
  when :hit
    metrics[:hit] += 1
  when :miss, :bypass, :expired, :stale, :updating, :revalidated
    metrics[:miss] += 1
  end
  raise Errors::InvalidIDError, response if response.code == 404
  raise Errors::APIError, response unless response.status.success?

  response
end