module BookingSync::Engine::Models::BaseAccount

Public Instance Methods

api() click to toggle source
# File lib/bookingsync/engine/models/base_account.rb, line 26
def api
  @api ||= BookingSync::Engine::APIClient.new(token.token, account: self)
end
clear_token!() click to toggle source
# File lib/bookingsync/engine/models/base_account.rb, line 30
def clear_token!
  self.oauth_access_token   = nil
  self.oauth_refresh_token  = nil
  self.oauth_expires_at     = nil
  save!
end
refresh_token!(current_token = token) click to toggle source
# File lib/bookingsync/engine/models/base_account.rb, line 43
def refresh_token!(current_token = token)
  token_refresh_timeout_attempts_allowed = ::BookingSyncEngine.token_refresh_timeout_retry_count + 1

  BookingSync::Engine::Retryable.perform(times: token_refresh_timeout_attempts_allowed, errors: [Faraday::TimeoutError]) do
    @token = current_token.refresh!.tap do |new_token|
      update_token(new_token)
      save!
    end
  end
end
token() click to toggle source
# File lib/bookingsync/engine/models/base_account.rb, line 8
def token
  @token ||= begin
    token_options = {}
    if oauth_refresh_token
      token_options[:refresh_token] = oauth_refresh_token
      token_options[:expires_at]    = oauth_expires_at && oauth_expires_at.to_i
    end

    token = OAuth2::AccessToken.new(oauth_client, oauth_access_token, token_options)

    if token.expired?
      refresh_token!(token)
    else
      token
    end
  end
end
update_token(token) click to toggle source
# File lib/bookingsync/engine/models/base_account.rb, line 37
def update_token(token)
  self.oauth_access_token   = token.token
  self.oauth_refresh_token  = token.refresh_token
  self.oauth_expires_at     = token.expires_at
end