Class: Gusto::SystemAccessTokenRefresher

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

Constant Summary collapse

EXPIRY_BUFFER_MS =

1 minute buffer

60 * 1000

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret) ⇒ SystemAccessTokenRefresher

Returns a new instance of SystemAccessTokenRefresher.



248
249
250
251
252
253
# File 'lib/fern_gusto.rb', line 248

def initialize(client_id, client_secret)
  @client_id = client_id
  @client_secret = client_secret
  @token = nil
  @token_expiry = nil
end

Instance Method Details

#get_tokenObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/fern_gusto.rb', line 255

def get_token
  if @token && @token_expiry && @token_expiry > Time.now + EXPIRY_BUFFER_MS
    return @token
  end

  client = Gusto::BaseClient.new(token: nil)
  response = client.token.get_system_access_token(
    client_id: @client_id,
    client_secret: @client_secret
  )

  @token = response.access_token
  @token_expiry = Time.now + response.expires_in

  @token
end