Class: DigifiApi::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/digifi_api/base.rb

Direct Known Subclasses

MLModel, MLRulesEngine

Class Method Summary collapse

Class Method Details

.headersObject



7
8
9
10
11
# File 'lib/digifi_api/base.rb', line 7

def self.headers
  headers = {content_type: :json, accept: :json}
  headers['x-access-token'] = DigifiApi.configuration.x_access_token unless DigifiApi.configuration.x_access_token.nil?
  return headers
end

.payload(elements) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/digifi_api/base.rb', line 13

def self.payload(elements)
  body_hash = Hash.new
  body_hash['client_id'] = DigifiApi.configuration.client_id
  body_hash['client_public_key'] = DigifiApi.configuration.client_public_key
  body_hash['client_secret'] = DigifiApi.configuration.secret
  body_hash.merge!(elements)
end

.post(resource_uri, elements) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/digifi_api/base.rb', line 21

def self.post(resource_uri, elements)
  if DigifiApi.configuration.configured?
    RestClient::Request.execute(:method => :post, url: (DigifiApi.configuration.base_uri + resource_uri), payload: payload(elements), headers: headers, use_ssl: true, verify_ssl: OpenSSL::SSL::VERIFY_NONE) do |response, request, result|
      case response.code
      when 200 # Success
        return response
      when 400 # Bad request
        return { error: "#{response.code}: The request was incorrectly formed" }
      when 401 # Unauthorized
        return { error: "#{response.code}: The authentication credentials were incorrect" }
      when 404 # Strategy not found
        return { error: "#{response.code}: The requested strategy or model cannot be found" }
      when 500 # Internal server error
        return { error: "#{response.code}: There was an error on the server" }
      else
        return { error: "#{response.code}" }
      end
    end
  else
    return { error: "Configuration must be valid before making a request" }
  end
end