Module: EuPago::Api::Auth

Defined in:
lib/ruby-eupago/api/auth.rb

Class Method Summary collapse

Class Method Details

.ensure_valid_tokenObject



13
14
15
16
17
# File 'lib/ruby-eupago/api/auth.rb', line 13

def self.ensure_valid_token
  if EuPago::Current.access_token.nil? || EuPago::Current.token_expired?
    fetch_client_credentials_token
  end
end

.fetch_client_credentials_tokenObject



19
20
21
22
23
24
25
# File 'lib/ruby-eupago/api/auth.rb', line 19

def self.fetch_client_credentials_token
  token(
    grant_type: EuPago::Constants::GRANT_TYPES[:client_credentials],
    client_id: ENV["EUPAGO_CLIENT_ID"],
    client_secret: ENV["EUPAGO_CLIENT_SECRET"],
  )
end

.token(body) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/ruby-eupago/api/auth.rb', line 5

def self.token(body)
  result = EuPago::Client.new({ prefix_base_url: "/api" }).post("/auth/token", body: body, headers: {
    "Content-Type" => "application/x-www-form-urlencoded",
  })
  EuPago::Current.auth = result
  result
end