Class: AcmeManager::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/acme_manager/request.rb

Overview

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

Constant Summary collapse

PATH_PREFIX =
'~acmemanager/'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Request

Returns a new instance of Request.

Parameters:

  • path (String)

    The API call you wish to make. This will have PATH_PREFIX prepended to it when making a request



12
13
14
# File 'lib/acme_manager/request.rb', line 12

def initialize(path)
  @path = path
end

Class Method Details

.make(path) ⇒ Object

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

Parameters:

  • path (String)

    The API call you wish to make. See #new for more details.



19
20
21
# File 'lib/acme_manager/request.rb', line 19

def self.make(path)
  new(path).make
end

Instance Method Details

#makeArray, Hash

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

Returns:

  • (Array, Hash)

    Parsed JSON data returned from the API

Raises:

  • (Net::HTTPError)

    Raised when a non-2xx result is returned



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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