Class: TACore::Auth

Inherits:
Configuration show all
Defined in:
lib/tacore.rb

Overview

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

Direct Known Subclasses

App, Client, Device, Gateway, Test, Venue, Webhook

Instance Attribute Summary collapse

Attributes inherited from Configuration

#api_key, #api_url, #client_id, #client_secret

Class Method Summary collapse

Methods inherited from Configuration

#initialize

Constructor Details

This class inherits a constructor from TACore::Configuration

Instance Attribute Details

#clientObject

Returns the value of attribute client.



69
70
71
# File 'lib/tacore.rb', line 69

def client
  @client
end

#tokenObject

Returns the value of attribute token.



68
69
70
# File 'lib/tacore.rb', line 68

def token
  @token
end

Class Method Details

.loginObject

Used to retrieve the TOKEN and Authenticate the application



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tacore.rb', line 72

def self.

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

Internal request only. Request method

Parameters:

  • method (Symbol<:get, :post, :put, :delete>)
  • uri (String)
  • payload (Hash)

    Changes to document object (optional)

  • headers (Hash)

    token, client_id,…



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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.
  # Rescue from rest-client exception due to 410 status from deleted objects
  rescue NotThereError
    {deleted: true}
  end
end