Class: Moltin::Api::Client

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

Constant Summary collapse

@@authenticated_until =
nil
@@access_token =
nil

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.access_tokenObject

Returns the value of attribute access_token.



14
15
16
# File 'lib/moltin/api/client.rb', line 14

def access_token
  @access_token
end

.authenticated_untilObject

Returns the value of attribute authenticated_until.



13
14
15
# File 'lib/moltin/api/client.rb', line 13

def authenticated_until
  @authenticated_until
end

Class Method Details

.authenticate(grant_type = nil, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/moltin/api/client.rb', line 17

def self.authenticate(grant_type = nil, options = {})
  grant_type ||= 'client_credentials'
  data ={
    grant_type: grant_type,
    client_id: options[:client_id] || ENV['MOLTIN_CLIENT_ID'],
    client_secret: options[:client_secret] || ENV['MOLTIN_CLIENT_SECRET'],
  }
  request = RestClient::Resource.new(
    "https://#{Moltin::Config.api_host}/oauth/access_token",
    {
      verify_ssl: OpenSSL::SSL::VERIFY_NONE,
    }
  )
  request.post(data) do |response|
    json = JSON.parse(response.to_s)
    self.access_token = json['access_token']
    self.authenticated_until = DateTime.strptime(json['expires'].to_s, '%s')
  end
end

.authenticated?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/moltin/api/client.rb', line 37

def self.authenticated?
  @@access_token && @@authenticated_until > DateTime.now
end