Class: BookingSync::Engine::APIClient

Inherits:
API::Client
  • Object
show all
Defined in:
lib/bookingsync/engine/api_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(token, options = {}) ⇒ APIClient

Returns a new instance of APIClient


2
3
4
5
# File 'lib/bookingsync/engine/api_client.rb', line 2

def initialize(token, options = {})
  super
  @account = options[:account]
end

Instance Method Details

#call(method, path, data = nil, options = nil) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/bookingsync/engine/api_client.rb', line 7

def call(method, path, data = nil, options = nil)
  tries ||= 1
  super
rescue BookingSync::API::Unauthorized => error
  if refresh_token?(error) && (tries -= 1) >= 0
    @token = @account.refresh_token!.token
    retry
  else
    raise
  end
end

#refresh_token?(error) ⇒ Boolean (private)

Returns:

  • (Boolean)

21
22
23
# File 'lib/bookingsync/engine/api_client.rb', line 21

def refresh_token?(error)
  error.headers["www-authenticate"].include?("The access token expired")
end