class TACore::Auth

Authorization class that will create the client token and authenticate with the API

Attributes

client[RW]
token[RW]

Public Class Methods

login() click to toggle source

Used to retrieve the TOKEN and Authenticate the application

# File lib/tacore.rb, line 72
def self.login

  # use rest-client for auth post to get token
  @@token = RestClient::Request.execute(method: :post, url: TACore.configuration.api_url + "/application/token",
    headers: {
        "uid": TACore.configuration.client_id,
        "secret": TACore.configuration.client_secret,
        "x-api-key": TACore.configuration.api_key
    }
  )

  if JSON.parse(@@token).include? "error"
    # The responce included an error, stop and show it!
    raise JSON.parse(@@token)["error"]
  end

  if @@token.nil?
    raise "Authentication Failed"
  end
  JSON.parse(@@token)
end
request(method, uri, payload, headers) click to toggle source

Internal request only. Request method @param method [Symbol<:get, :post, :put, :delete>] @param uri [String] @param payload [Hash] Changes to document object (optional) @param headers [Hash] token, client_id,…

# File lib/tacore.rb, line 100
def self.request(method, uri, payload, headers)

  # Add static API-KEY to headers from config
  headers["x-api-key"] = TACore.configuration.api_key

  begin
    response = RestClient::Request.execute(method: method, url: TACore.configuration.api_url + uri, payload: payload, headers: headers)
    case response.code
    when 200
      JSON.parse(response.body)
    else
      return { "error": { "code": response.code, "body": JSON.parse(response.body) }}
    end

  rescue RestClient::Gone
    {deleted: true}

  # Rest Client exceptions
  rescue RestClient::ExceptionWithResponse => e
    # Raise TokenError on all other exceptions
    raise TACore::TokenError.new "#{e.message}"


  # Rescue for unauthorized/token expired
  # rescue AuthenticationError
  #   self.login
  # Rescue from rest-client exception due to 410 status from deleted objects
  rescue NotThereError
    {deleted: true}
  end
end