Class: Booker::Client

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

Direct Known Subclasses

BusinessClient, CustomerClient

Constant Summary collapse

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.



7
8
9
10
11
# File 'lib/booker/client.rb', line 7

def initialize(options = {})
  options.each do |key, value|
    send(:"#{key}=", value)
  end
end

Instance Attribute Details

#base_url ⇒ Object

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#client_id ⇒ Object

Returns the value of attribute client_id.



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

def client_id
  @client_id
end

#client_secret ⇒ Object

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_token ⇒ Object

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_at ⇒ Object

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_store ⇒ Object

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_method ⇒ Object

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_token ⇒ Object



118
119
120
121
122
123
124
# File 'lib/booker/client.rb', line 118

def access_token
  if self.temp_access_token && !temp_access_token_expired?
    self.temp_access_token
  else
    get_access_token
  end
end

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



13
14
15
16
17
# File 'lib/booker/client.rb', line 13

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_booker_resources(http_method, path, params = nil, body = nil, booker_model = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/booker/client.rb', line 70

def get_booker_resources(http_method, path, params=nil, body=nil, booker_model=nil)
  http_options = request_options(params, body)
  puts "BOOKER REQUEST: #{http_method} #{path} #{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!(http_options, HTTParty.send(http_method, "#{self.base_url}#{path}", http_options))
  rescue Booker::Error, Net::ReadTimeout => ex
    if ex.is_a? Booker::InvalidApiCredentials
      raise ex
    else
      sleep 1
      booker_resources = nil
    end
  end

  if booker_resources
    results_from_response(booker_resources, booker_model)
  else
    booker_resources = handle_errors!(http_options, HTTParty.send(http_method, "#{self.base_url}#{path}", http_options))

    if booker_resources
      results_from_response(booker_resources, booker_model)
    else
      raise Booker::Error.new(http_options, booker_resources)
    end
  end
end

#handle_errors!(request, response) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/booker/client.rb', line 99

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

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

  response
end

#log_issue(message, extra_info = {}) ⇒ Object



64
65
66
67
68
# File 'lib/booker/client.rb', line 64

def log_issue(message, extra_info = {})
  if (log_message_block = Booker.config[:log_message])
    log_message_block.call(message, extra_info)
  end
end

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/booker/client.rb', line 31

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



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

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



25
26
27
28
29
# File 'lib/booker/client.rb', line 25

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