Module: BookingSync::Engine::Models::BaseAccount

Extended by:
ActiveSupport::Concern
Included in:
Account, MultiApplicationsAccount
Defined in:
lib/bookingsync/engine/models/base_account.rb

Instance Method Summary collapse

Instance Method Details

#apiObject


26
27
28
# File 'lib/bookingsync/engine/models/base_account.rb', line 26

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

#clear_token!Object


30
31
32
33
34
35
# 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) ⇒ Object


43
44
45
46
47
48
49
50
51
52
# 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

#tokenObject


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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) ⇒ Object


37
38
39
40
41
# 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