Class: Innologix::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/innologix/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



3
4
5
# File 'lib/innologix/client.rb', line 3

def access_token
  @access_token
end

#authObject

Returns the value of attribute auth.



3
4
5
# File 'lib/innologix/client.rb', line 3

def auth
  @auth
end

#client_idObject

Returns the value of attribute client_id.



3
4
5
# File 'lib/innologix/client.rb', line 3

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



3
4
5
# File 'lib/innologix/client.rb', line 3

def client_secret
  @client_secret
end

#default_headersObject

Returns the value of attribute default_headers.



3
4
5
# File 'lib/innologix/client.rb', line 3

def default_headers
  @default_headers
end

#innologix_urlObject

Returns the value of attribute innologix_url.



3
4
5
# File 'lib/innologix/client.rb', line 3

def innologix_url
  @innologix_url
end

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/innologix/client.rb', line 3

def logger
  @logger
end

#sso_urlObject

Returns the value of attribute sso_url.



3
4
5
# File 'lib/innologix/client.rb', line 3

def sso_url
  @sso_url
end

#timeoutObject

Returns the value of attribute timeout.



3
4
5
# File 'lib/innologix/client.rb', line 3

def timeout
  @timeout
end

Class Method Details

.defaultObject



30
31
32
# File 'lib/innologix/client.rb', line 30

def self.default
  @default ||= Client.new
end

.set_user_id(user_id) ⇒ Object



24
25
26
27
28
# 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

Instance Method Details

#call_api(path, method, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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_tokenObject



34
35
36
# File 'lib/innologix/client.rb', line 34

def current_token
  @access_token ||= get_token
end

#get_tokenObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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