class Alloy::Api

Public Class Methods

api_secret=(value) click to toggle source
# File lib/alloy-api.rb, line 17
def self.api_secret=(value)
  @@api_endpoints[:main][:secret] = value
end
api_token=(value) click to toggle source
# File lib/alloy-api.rb, line 13
def self.api_token=(value)
  @@api_endpoints[:main][:token] = value
end
api_uri=(value) click to toggle source
# File lib/alloy-api.rb, line 9
def self.api_uri=(value)
  @@api_endpoints[:main][:uri] = value
end
entity_details(entity_id, options = {}) click to toggle source
# File lib/alloy-api.rb, line 33
def self.entity_details(entity_id, options = {})
  perform "entities/#{entity_id}", options.merge(method: :get)
end
evaluation_details(entity_id, evaluation_id, options = {}) click to toggle source
# File lib/alloy-api.rb, line 37
def self.evaluation_details(entity_id, evaluation_id, options = {})
  perform "entities/#{entity_id}/evaluations/#{evaluation_id}", options.merge(method: :get)
end
method_missing(m, *args, &block) click to toggle source
# File lib/alloy-api.rb, line 41
def self.method_missing(m, *args, &block)
  perform(m, args[0] || {})
end
parameters(options={}) click to toggle source
# File lib/alloy-api.rb, line 29
def self.parameters(options={})
  perform "parameters", options.merge(method: :get)
end
perform(path, options={}) click to toggle source
# File lib/alloy-api.rb, line 45
def self.perform(path, options={})
  method = options[:method] || "post"
  endpoint = options[:endpoint] || :main
  uri = "#{@@api_endpoints[endpoint][:uri]}#{path}"
  body_encoding = options[:body_encoding] || :to_json
  headers = {
    "Content-Type" => "application/json",
    "Authorization" => auth_param(endpoint)
  }.merge(options[:headers].to_h)
  if options[:body_stream].present?
    response = HTTParty.send(method, uri, { headers: headers, body_stream: options[:body_stream] })
  else
    response = HTTParty.send(method, uri, { headers: headers, body: (options[:body] || {}).send(body_encoding) })
  end
  return response if options[:raw]
  JSON.parse(response.body)
  # TODO: Error handling
end
set_endpoint(name, token, secret, uri = 'https://api.alloy.co/') click to toggle source
# File lib/alloy-api.rb, line 21
def self.set_endpoint(name, token, secret, uri = 'https://api.alloy.co/')
  @@api_endpoints[name] = {
    uri: uri,
    token: token,
    secret: secret
  }
end

Private Class Methods

auth_param(endpoint) click to toggle source
# File lib/alloy-api.rb, line 66
def self.auth_param(endpoint)
  "Basic #{Base64.strict_encode64("#{@@api_endpoints[endpoint][:token]}:#{@@api_endpoints[endpoint][:secret]}")}"
end