class AcmeManager::Request

Simplify the process of making an API request to acme-manager

Constants

PATH_PREFIX

Public Class Methods

make(path) click to toggle source

Convenicence method to make a new instance and return the result of the request

@param [String] path The API call you wish to make. See new for more details.

# File lib/acme_manager/request.rb, line 19
def self.make(path)
  new(path).make
end
new(path) click to toggle source

@param [String] path The API call you wish to make. This will have PATH_PREFIX prepended to it when

making a request
# File lib/acme_manager/request.rb, line 12
def initialize(path)
  @path = path
end

Public Instance Methods

make() click to toggle source

Make a request to the acme-manager API. Requests will be sent to the host defined in AcmeManager.config, and sent with the API key from the same configuration.

Successfull responses are assumed to be in JSON format and are automatically parsed

@return [Array, Hash] Parsed JSON data returned from the API

@raise [Net::HTTPError] Raised when a non-2xx result is returned

# File lib/acme_manager/request.rb, line 31
def make
  AcmeManager.logger.debug "Requesting #{request_uri}"

  http = new_http_connection

  request = Net::HTTP::Get.new(request_uri.path)
  request['X-API-KEY'] = AcmeManager.config.api_key

  result = http.request(request)

  AcmeManager.logger.debug "Response Status: #{result.code}"
  AcmeManager.logger.debug "Response Body:\n#{result.body}"

  if result.is_a?(Net::HTTPSuccess)
    JSON.parse(result.body)
  else
    AcmeManager.logger.warn "Request to #{request_uri} failed"
    result.value
  end
end

Private Instance Methods

new_http_connection() click to toggle source

Build a new connection object to the configured acme-manager host.

@return [Net::HTTP] Connection object

# File lib/acme_manager/request.rb, line 57
def new_http_connection
  http = Net::HTTP.new(request_uri.host, request_uri.port)
  if request_uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end

  http
end
request_uri() click to toggle source

Build a complete URL for the request and parse it with URI

@return [URI::HTTP] Parsed request URI

# File lib/acme_manager/request.rb, line 70
def request_uri
  @request_uri ||= URI("#{AcmeManager.config.host}/#{PATH_PREFIX}#{@path}")
end