Class: Innologix::Client
- Inherits:
-
Object
- Object
- Innologix::Client
- Defined in:
- lib/innologix/client.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#auth ⇒ Object
Returns the value of attribute auth.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#default_headers ⇒ Object
Returns the value of attribute default_headers.
-
#innologix_url ⇒ Object
Returns the value of attribute innologix_url.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#sso_url ⇒ Object
Returns the value of attribute sso_url.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
- #call_api(path, method, options = {}) ⇒ Object
- #current_token ⇒ Object
- #get_token ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
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 ( = {}) @client_id = [:client_id] || Innologix.config['client_id'] @client_secret = [:client_secret] || Innologix.config['client_secret'] @innologix_url = [:innologix_url] || Innologix.config['innologix_url'] @sso_url = [:sso_url] || Innologix.config['sso_url'] @timeout = [:timeout] || Innologix.config['timeout'] @default_headers = {:'Content-Type' => "application/json", :'Authorization' => 'Bearer ' + current_token} return true end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
3 4 5 |
# File 'lib/innologix/client.rb', line 3 def access_token @access_token end |
#auth ⇒ Object
Returns the value of attribute auth.
3 4 5 |
# File 'lib/innologix/client.rb', line 3 def auth @auth end |
#client_id ⇒ Object
Returns the value of attribute client_id.
3 4 5 |
# File 'lib/innologix/client.rb', line 3 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
3 4 5 |
# File 'lib/innologix/client.rb', line 3 def client_secret @client_secret end |
#default_headers ⇒ Object
Returns the value of attribute default_headers.
3 4 5 |
# File 'lib/innologix/client.rb', line 3 def default_headers @default_headers end |
#innologix_url ⇒ Object
Returns the value of attribute innologix_url.
3 4 5 |
# File 'lib/innologix/client.rb', line 3 def innologix_url @innologix_url end |
#logger ⇒ Object
Returns the value of attribute logger.
3 4 5 |
# File 'lib/innologix/client.rb', line 3 def logger @logger end |
#sso_url ⇒ Object
Returns the value of attribute sso_url.
3 4 5 |
# File 'lib/innologix/client.rb', line 3 def sso_url @sso_url end |
#timeout ⇒ Object
Returns the value of attribute timeout.
3 4 5 |
# File 'lib/innologix/client.rb', line 3 def timeout @timeout end |
Class Method Details
.default ⇒ Object
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, = {}) url = innologix_url + path header_params ||= @default_headers.merge([:header_params] || {}) query_params = [:query_params] || {} header_params[:params] = query_params form_params = [: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.} end |
#current_token ⇒ Object
34 35 36 |
# File 'lib/innologix/client.rb', line 34 def current_token @access_token ||= get_token end |
#get_token ⇒ Object
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. nil end end |