Module: BookingSync::Engine::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/bookingsync/engine/model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#apiObject


42
43
44
# File 'lib/bookingsync/engine/model.rb', line 42

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

#clear_token!Object


57
58
59
60
61
62
# File 'lib/bookingsync/engine/model.rb', line 57

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


38
39
40
# File 'lib/bookingsync/engine/model.rb', line 38

def refresh_token!(current_token = token)
  @token = current_token.refresh!.tap { |new_token| update_token!(new_token) }
end

#tokenObject


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bookingsync/engine/model.rb', line 19

def token
  @token ||= begin
    token_options = {}
    if oauth_refresh_token
      token_options[:refresh_token] = oauth_refresh_token
      token_options[:expires_at]    = oauth_expires_at
    end

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

    if token.expired?
      refresh_token!(token)
    else
      token
    end
  end
end

#update_token(token) ⇒ Object


46
47
48
49
50
# File 'lib/bookingsync/engine/model.rb', line 46

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

#update_token!(token) ⇒ Object


52
53
54
55
# File 'lib/bookingsync/engine/model.rb', line 52

def update_token!(token)
  update_token(token)
  save!
end