class Innologix::Client

Attributes

access_token[RW]
auth[RW]
client_id[RW]
client_secret[RW]
default_headers[RW]
innologix_url[RW]
logger[RW]
sso_url[RW]
timeout[RW]

Public Class Methods

default() click to toggle source
# File lib/innologix/client.rb, line 30
def self.default
  @default ||= Client.new
end
new(options = {}) click to toggle source
# File lib/innologix/client.rb, line 13
def initialize (options = {})
  @client_id = options[:client_id] || Innologix.config['client_id']
  @client_secret = options[:client_secret] || Innologix.config['client_secret']
  @innologix_url = options[:innologix_url] || Innologix.config['innologix_url']
  @sso_url = options[:sso_url] || Innologix.config['sso_url']
  @timeout = options[:timeout] || Innologix.config['timeout']
  @default_headers = {:'Content-Type' => "application/json",
                      :'Authorization' => 'Bearer ' + current_token}
  return true
end
set_user_id(user_id) click to toggle source
# File lib/innologix/client.rb, line 24
def self.set_user_id(user_id)
  @default.default_headers = {:'Content-Type' => "application/json",
                              :'X-User-ID' => user_id,
                              :'Authorization' => 'Bearer ' + @default.access_token}
end

Public Instance Methods

call_api(path, method, options = {}) click to toggle source
# File lib/innologix/client.rb, line 60
def call_api(path, method, options = {})
  url = innologix_url + path
  header_params ||= @default_headers.merge(options[:header_params] || {})
  query_params = options[:query_params] || {}
  header_params[:params] = query_params
  form_params = options[:form_params] || {}
  RestClient::Request.execute(method: method.to_sym.downcase,
                              url: url,
                              payload: form_params,
                              timeout: timeout,
                              :verify_ssl => false,
                              headers: header_params) do |response, request, result, &block|

    JSON.parse response, :symbolize_names => true
  end
rescue Exception => e
  {error: 500, message: e.message}
end
current_token() click to toggle source
# File lib/innologix/client.rb, line 34
def current_token
  @access_token ||= get_token
end
get_token() click to toggle source
# File lib/innologix/client.rb, line 38
def get_token
  url = @sso_url + '/oauth/access_token'
  begin
    RestClient.post url, {client_id: client_id, client_secret: client_secret, grant_type: 'client_credentials'} do |response, request, result, &block|
      case response.code
        when 200
          result = JSON.parse response, :symbolize_names => true
          if result[:access_token].nil?
            nil
          else
            result[:access_token]
          end
        else
          nil
      end
    end
  rescue Exception => e
    Innologix::Logger.error e.message
    nil
  end
end