Class: Booker::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/booker/client.rb

Direct Known Subclasses

BusinessClient, CustomerClient

Constant Summary collapse

ACCESS_TOKEN_HTTP_METHOD =
:get
ACCESS_TOKEN_ENDPOINT =
'/access_token'.freeze
TimeZone =
'Eastern Time (US & Canada)'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
# File 'lib/booker/client.rb', line 10

def initialize(options = {})
  options.each { |key, value| send(:"#{key}=", value) }
  self.base_url ||= get_base_url
  self.client_id ||= ENV['BOOKER_CLIENT_ID']
  self.client_secret ||= ENV['BOOKER_CLIENT_SECRET']
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



3
4
5
# File 'lib/booker/client.rb', line 3

def base_url
  @base_url
end

#client_idObject

Returns the value of attribute client_id.



3
4
5
# File 'lib/booker/client.rb', line 3

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



3
4
5
# File 'lib/booker/client.rb', line 3

def client_secret
  @client_secret
end

#temp_access_tokenObject

Returns the value of attribute temp_access_token.



3
4
5
# File 'lib/booker/client.rb', line 3

def temp_access_token
  @temp_access_token
end

#temp_access_token_expires_atObject

Returns the value of attribute temp_access_token_expires_at.



3
4
5
# File 'lib/booker/client.rb', line 3

def temp_access_token_expires_at
  @temp_access_token_expires_at
end

#token_storeObject

Returns the value of attribute token_store.



3
4
5
# File 'lib/booker/client.rb', line 3

def token_store
  @token_store
end

#token_store_callback_methodObject

Returns the value of attribute token_store_callback_method.



3
4
5
# File 'lib/booker/client.rb', line 3

def token_store_callback_method
  @token_store_callback_method
end

Instance Method Details

#access_tokenObject



125
126
127
# File 'lib/booker/client.rb', line 125

def access_token
  (self.temp_access_token && !temp_access_token_expired?) ? self.temp_access_token : get_access_token
end

#access_token_optionsObject



129
130
131
132
133
134
# File 'lib/booker/client.rb', line 129

def access_token_options
  {
      client_id: self.client_id,
      client_secret: self.client_secret
  }
end

#access_token_response(http_options) ⇒ Object



164
165
166
# File 'lib/booker/client.rb', line 164

def access_token_response(http_options)
  send(self.class::ACCESS_TOKEN_HTTP_METHOD, self.class::ACCESS_TOKEN_ENDPOINT, http_options, nil)
end

#full_url(path) ⇒ Object



101
102
103
104
# File 'lib/booker/client.rb', line 101

def full_url(path)
  uri = URI(path)
  uri.scheme ? path : "#{self.base_url}#{path}"
end

#get(path, params, booker_model = nil) ⇒ Object



27
28
29
30
31
# File 'lib/booker/client.rb', line 27

def get(path, params, booker_model=nil)
  booker_resources = get_booker_resources(:get, path, params, nil, booker_model)

  build_resources(booker_resources, booker_model)
end

#get_access_tokenObject



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/booker/client.rb', line 142

def get_access_token
  http_options = access_token_options
  response = raise_invalid_api_credentials_for_empty_resp! { access_token_response(http_options) }

  self.temp_access_token_expires_at = Time.now + response['expires_in'].to_i.seconds
  self.temp_access_token = response['access_token']

  update_token_store

  self.temp_access_token
end

#get_base_urlObject



17
18
19
20
21
22
23
24
25
# File 'lib/booker/client.rb', line 17

def get_base_url
  env_key = try(:env_base_url_key)

  if env_key.present?
    ENV[env_key] || try(:default_base_url)
  else
    try(:default_base_url)
  end
end

#get_booker_resources(http_method, path, params = nil, body = nil, booker_model = nil) ⇒ Object

Raises:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/booker/client.rb', line 78

def get_booker_resources(http_method, path, params=nil, body=nil, booker_model=nil)
  http_options = request_options(params, body)
  url = full_url(path)
  puts "BOOKER REQUEST: #{http_method} #{url} #{http_options}" if ENV['BOOKER_API_DEBUG'] == 'true'

  # Allow it to retry the first time unless it is an authorization error
  begin
    booker_resources = handle_errors!(url, http_options, HTTParty.send(http_method, url, http_options))
  rescue Booker::Error, Net::ReadTimeout => ex
    if ex.is_a? Booker::InvalidApiCredentials
      raise ex
    else
      sleep 1
      booker_resources = nil
    end
  end

  return results_from_response(booker_resources, booker_model) if booker_resources.present?
  booker_resources = handle_errors!(url, http_options, HTTParty.send(http_method, url, http_options))
  return results_from_response(booker_resources, booker_model) if booker_resources.present?
  raise Booker::Error.new(url: url, request: http_options, response: booker_resources)
end

#handle_errors!(url, request, response) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/booker/client.rb', line 106

def handle_errors!(url, request, response)
  puts "BOOKER RESPONSE: #{response}" if ENV['BOOKER_API_DEBUG'] == 'true'

  ex = Booker::Error.new(url: url, request: request, response: response)
  if ex.error.present? || !response.success?
    case ex.error
      when 'invalid_client'
        raise Booker::InvalidApiCredentials.new(url: url, request: request, response: response)
      # when 'invalid access token'
      #   get_access_token
      #   return nil
      else
        raise ex
    end
  end

  response
end

#paginated_request(method:, path:, params:, model: nil, fetched: [], fetch_all: true) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/booker/client.rb', line 45

def paginated_request(method:, path:, params:, model: nil, fetched: [], fetch_all: true)
  page_size = params['PageSize']
  page_number = params['PageNumber']

  if page_size.nil? || page_size < 1 || page_number.nil? || page_number < 1 || !params['UsePaging']
    raise ArgumentError, 'params must include valid PageSize, PageNumber and UsePaging'
  end

  puts "fetching #{path} with #{params.except('access_token')}. #{fetched.length} results so far."

  results = self.send(method, path, params, model)

  unless results.is_a?(Array)
    raise StandardError, "Result from paginated request to #{path} with params: #{params} is not a collection"
  end

  fetched.concat(results)
  results_length = results.length

  if fetch_all
    if results_length > 0
      # TODO (#111186744): Add logging to see if any pages with less than expected data (as seen in the /appointments endpoint)
      new_params = params.deep_dup
      new_params['PageNumber'] = page_number + 1
      paginated_request(method: method, path: path, params: new_params, model: model, fetched: fetched)
    else
      fetched
    end
  else
    results
  end
end

#post(path, data, booker_model = nil) ⇒ Object



33
34
35
36
37
# File 'lib/booker/client.rb', line 33

def post(path, data, booker_model=nil)
  booker_resources = get_booker_resources(:post, path, nil, data.to_json, booker_model)

  build_resources(booker_resources, booker_model)
end

#put(path, data, booker_model = nil) ⇒ Object



39
40
41
42
43
# File 'lib/booker/client.rb', line 39

def put(path, data, booker_model=nil)
  booker_resources = get_booker_resources(:put, path, nil, data.to_json, booker_model)

  build_resources(booker_resources, booker_model)
end

#raise_invalid_api_credentials_for_empty_resp!Object



154
155
156
157
158
159
160
161
162
# File 'lib/booker/client.rb', line 154

def raise_invalid_api_credentials_for_empty_resp!
  yield
rescue Booker::Error => ex
  if (response = ex.response).present?
    raise ex
  else
    raise Booker::InvalidApiCredentials.new(url: ex.url, request: ex.request, response: response)
  end
end

#update_token_storeObject



136
137
138
139
140
# File 'lib/booker/client.rb', line 136

def update_token_store
  if self.token_store.present? && self.token_store_callback_method.present?
    self.token_store.send(self.token_store_callback_method, self.temp_access_token, self.temp_access_token_expires_at)
  end
end