module Evvnt::Api::ClassMethods

Class methods to define on {Evvnt::Base}

Private Instance Methods

api_request(verb, path, params: {}, options: {}) click to toggle source

Make a request of the EVVNT API.

verb - The HTTP verb for the request. path - The path to request from the API. params - A Hash of query params to send with the request. options - A Hash of additional options for the request.

Returns HTTParty::Response Raises ArgumentError if the verb is not valid

# File lib/evvnt/api.rb, line 36
def api_request(verb, path, params: {},  options: {})
  unless verb.in?(HTTP_VERBS)
    raise ArgumentError, "Unrecognised HTTP verb '#{verb}'"
  end
  path = sanitize_path(path)
  log_request(verb, path, params)
  response = public_send(verb, path, query: params, headers: headers)
  log_response(response)
  case response.code
  when 200, 201
    parse_response(response, options)
  else
    raise ApiError, parse_error_response(response.body)
  end
end
auth() click to toggle source

Key and secret as Base64 string.

# File lib/evvnt/api.rb, line 130
def auth
  Base64.encode64([Evvnt.configuration.api_key,
                   Evvnt.configuration.api_secret].join(":"))
end
headers() click to toggle source

Headers to be sent with every request.

Returns Hash

# File lib/evvnt/api.rb, line 120
def headers
  {
    "Authorization" => "Basic #{auth}",
    "Content-Type"  => "application/json",
    "Accept"        => "application/json"
  }
end
log_request(verb, path, params = {}) click to toggle source

Log the request being sent to the API

verb - A Symbol of the HTTP verb for the request path - A String of the path of the request params - A Hash of the request params being sent to the server

# File lib/evvnt/api.rb, line 58
        def log_request(verb, path, params = {})
          return unless Evvnt.configuration.debug
          debug <<~TEXT
            Headers: #{headers}")
            Request: #{verb.to_s.upcase} #{base_uri}#{path} #{params}
          TEXT
        end
log_response(response) click to toggle source

Log the response from the API

response - The Response object from the API

# File lib/evvnt/api.rb, line 70
        def log_response(response)
          return unless Evvnt.configuration.debug
          debug <<~TEXT
            Response: #{response}
            Status: #{response.code}
          TEXT
        end
parse_body(body) click to toggle source
# File lib/evvnt/api.rb, line 78
def parse_body(body)
  Oj.load(body)
end
parse_error_response(body) click to toggle source
# File lib/evvnt/api.rb, line 96
def parse_error_response(body)
  body_json = parse_body(body)
  if body_json.key?("error")
    body_json["error"]
  else
    body_json["errors"].join(", ")
  end
end
parse_response(response, **options) click to toggle source

Parse a response from the API and create objects from local classes.

response - Response object from HTTParty request. options - A Hash of options

Returns Array Returns Evvnt::Base subclass

# File lib/evvnt/api.rb, line 90
def parse_response(response, **options)
  json = parse_body(response.body)
  json = json[options[:object]] if options.key?(:object)
  json.is_a?(Array) ? json.map { |a| new(a) } : new(json)
end
sanitize_path(path) click to toggle source

Ensure the path is the correct format with a leading slash and “.json” extension

path - A String with the API request path.

Returns String

# File lib/evvnt/api.rb, line 110
def sanitize_path(path)
  path = "/"  + path    unless path.starts_with?('/')
  path += ".json" unless path.ends_with?(".json")
  path
end