class Copperegg::Alerts::Client

Constants

JSON

Attributes

api_base_uri[R]

Public Class Methods

new() click to toggle source
# File lib/copperegg/alerts/client.rb, line 12
def initialize
  @api_base_uri = 'https://api.copperegg.com/v2/'
end

Public Instance Methods

auth_setup(api_key) click to toggle source
# File lib/copperegg/alerts/client.rb, line 16
def auth_setup(api_key)
  @auth = { basic_auth: { username: api_key, password: 'U' } }
  self
end
delete(path) click to toggle source
# File lib/copperegg/alerts/client.rb, line 43
def delete(path)
  _send(:delete, path)
end
get(path) click to toggle source
# File lib/copperegg/alerts/client.rb, line 31
def get(path)
  _send(:get, path)
end
method_missing(method, resource, *args) click to toggle source
# File lib/copperegg/alerts/client.rb, line 23
def method_missing(method, resource, *args)
  if method.to_s =~ /\?$/
    method = method.to_s.sub!(/\?$/, '')
    result = send(method.to_sym, resource, *args)
    return result if result.code == 200
  end
end
post(path, body) click to toggle source
# File lib/copperegg/alerts/client.rb, line 35
def post(path, body)
  _send(:post, path, body)
end
put(path, body) click to toggle source
# File lib/copperegg/alerts/client.rb, line 39
def put(path, body)
  _send(:put, path, body)
end

Private Instance Methods

_send(method, path, body = {}) click to toggle source
# File lib/copperegg/alerts/client.rb, line 49
def _send(method, path, body = {})
  auth = @auth.clone
  unless body.empty?
    auth.merge!({ headers: JSON }.merge!(body: body.to_json))
  end
  response = HTTParty.send(method.to_sym, @api_base_uri + path, auth.to_hash)
  if response.code != 200
    raise("HTTP/#{method} Request failed. Response code `#{response.code}`, message `#{response.message}`, body `#{response.body}`")
  end
  response
end