Class: Booker::Client
- Inherits:
-
Object
- Object
- Booker::Client
- Defined in:
- lib/booker/client.rb
Direct Known Subclasses
Constant Summary collapse
- ACCESS_TOKEN_HTTP_METHOD =
:get
- ACCESS_TOKEN_ENDPOINT =
'/access_token'.freeze
- TimeZone =
'Eastern Time (US & Canada)'.freeze
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#temp_access_token ⇒ Object
Returns the value of attribute temp_access_token.
-
#temp_access_token_expires_at ⇒ Object
Returns the value of attribute temp_access_token_expires_at.
-
#token_store ⇒ Object
Returns the value of attribute token_store.
-
#token_store_callback_method ⇒ Object
Returns the value of attribute token_store_callback_method.
Instance Method Summary collapse
- #access_token ⇒ Object
- #access_token_options ⇒ Object
- #access_token_response(http_options) ⇒ Object
- #full_url(path) ⇒ Object
- #get(path, params, booker_model = nil) ⇒ Object
- #get_access_token ⇒ Object
- #get_base_url ⇒ Object
- #get_booker_resources(http_method, path, params = nil, body = nil, booker_model = nil) ⇒ Object
- #handle_errors!(url, request, response) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #paginated_request(method:, path:, params:, model: nil, fetched: [], fetch_all: true) ⇒ Object
- #post(path, data, booker_model = nil) ⇒ Object
- #put(path, data, booker_model = nil) ⇒ Object
- #raise_invalid_api_credentials_for_empty_resp! ⇒ Object
- #update_token_store ⇒ Object
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( = {}) .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_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
126 127 128 |
# File 'lib/booker/client.rb', line 126 def access_token (self.temp_access_token && !temp_access_token_expired?) ? self.temp_access_token : get_access_token end |
#access_token_options ⇒ Object
130 131 132 133 134 135 |
# File 'lib/booker/client.rb', line 130 def { client_id: self.client_id, client_secret: self.client_secret } end |
#access_token_response(http_options) ⇒ Object
165 166 167 |
# File 'lib/booker/client.rb', line 165 def access_token_response() send(self.class::ACCESS_TOKEN_HTTP_METHOD, self.class::ACCESS_TOKEN_ENDPOINT, , nil) end |
#full_url(path) ⇒ Object
102 103 104 105 |
# File 'lib/booker/client.rb', line 102 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_token ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/booker/client.rb', line 143 def get_access_token = response = raise_invalid_api_credentials_for_empty_resp! { access_token_response() } 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_url ⇒ Object
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
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/booker/client.rb', line 78 def get_booker_resources(http_method, path, params=nil, body=nil, booker_model=nil) = (params, body) [:verify] = false # This should be removed so SSL can be verified url = full_url(path) puts "BOOKER REQUEST: #{http_method} #{url} #{}" 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, , HTTParty.send(http_method, url, )) 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, , HTTParty.send(http_method, url, )) return results_from_response(booker_resources, booker_model) if booker_resources.present? raise Booker::Error.new(url: url, request: , response: booker_resources) end |
#handle_errors!(url, request, response) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/booker/client.rb', line 107 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
155 156 157 158 159 160 161 162 163 |
# File 'lib/booker/client.rb', line 155 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_store ⇒ Object
137 138 139 140 141 |
# File 'lib/booker/client.rb', line 137 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 |