Sha256: edd718b8973ea2b314bdd71e7c994857870e4269b477b19307aff0e19d50ffeb

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

module BookingSync::Engine::Models::BaseAccount
  extend ActiveSupport::Concern

  included do
    scope :authorized, -> { where.not(oauth_access_token: nil) }
  end

  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

  def api
    @api ||= BookingSync::Engine::APIClient.new(token.token, account: self)
  end

  def clear_token!
    self.oauth_access_token   = nil
    self.oauth_refresh_token  = nil
    self.oauth_expires_at     = nil
    save!
  end

  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

  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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bookingsync-engine-6.0.1 lib/bookingsync/engine/models/base_account.rb
bookingsync-engine-6.0.0 lib/bookingsync/engine/models/base_account.rb
bookingsync-engine-5.1.0 lib/bookingsync/engine/models/base_account.rb