Class: Alloy::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/alloy-api.rb

Constant Summary collapse

@@api_endpoints =
{ main: { uri: 'https://api.alloy.co/' } }

Class Method Summary collapse

Class Method Details

.api_secret=(value) ⇒ Object



17
18
19
# File 'lib/alloy-api.rb', line 17

def self.api_secret=(value)
  @@api_endpoints[:main][:secret] = value
end

.api_token=(value) ⇒ Object



13
14
15
# File 'lib/alloy-api.rb', line 13

def self.api_token=(value)
  @@api_endpoints[:main][:token] = value
end

.api_uri=(value) ⇒ Object



9
10
11
# File 'lib/alloy-api.rb', line 9

def self.api_uri=(value)
  @@api_endpoints[:main][:uri] = value
end

.entity_details(entity_id, options = {}) ⇒ Object



33
34
35
# 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 = {}) ⇒ Object



37
38
39
# 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) ⇒ Object



41
42
43
# File 'lib/alloy-api.rb', line 41

def self.method_missing(m, *args, &block)
  perform(m, args[0] || {})
end

.parameters(options = {}) ⇒ Object



29
30
31
# File 'lib/alloy-api.rb', line 29

def self.parameters(options={})
  perform "parameters", options.merge(method: :get)
end

.perform(path, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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/') ⇒ Object



21
22
23
24
25
26
27
# 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