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
- #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!(request, response) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #log_issue(message, extra_info = {}) ⇒ Object
- #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
- #update_token_store ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
10 11 12 13 |
# File 'lib/booker/client.rb', line 10 def initialize( = {}) .each { |key, value| send(:"#{key}=", value) } self.base_url ||= get_base_url 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
122 123 124 125 126 127 128 |
# File 'lib/booker/client.rb', line 122 def access_token if self.temp_access_token && !temp_access_token_expired? self.temp_access_token else get_access_token end 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
157 158 159 |
# File 'lib/booker/client.rb', line 157 def access_token_response() send(self.class::ACCESS_TOKEN_HTTP_METHOD, self.class::ACCESS_TOKEN_ENDPOINT, , nil).parsed_response end |
#get(path, params, booker_model = nil) ⇒ Object
17 18 19 20 21 |
# File 'lib/booker/client.rb', line 17 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 154 155 |
# File 'lib/booker/client.rb', line 143 def get_access_token = response = access_token_response() raise Booker::InvalidApiCredentials.new(, response) unless response.present? 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
15 |
# File 'lib/booker/client.rb', line 15 def get_base_url; ENV[try(:env_base_url_key).to_s] || try(:default_base_url); end |
#get_booker_resources(http_method, path, params = nil, body = nil, booker_model = nil) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/booker/client.rb', line 74 def get_booker_resources(http_method, path, params=nil, body=nil, booker_model=nil) = (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!(, HTTParty.send(http_method, "#{self.base_url}#{path}", )) 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!(, HTTParty.send(http_method, "#{self.base_url}#{path}", )) if booker_resources results_from_response(booker_resources, booker_model) else raise Booker::Error.new(, booker_resources) end end end |
#handle_errors!(request, response) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/booker/client.rb', line 103 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
68 69 70 71 72 |
# File 'lib/booker/client.rb', line 68 def log_issue(, extra_info = {}) if ( = Booker.config[:log_message]) .call(, extra_info) end end |
#paginated_request(method:, path:, params:, model: nil, fetched: [], fetch_all: true) ⇒ Object
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 63 64 65 66 |
# File 'lib/booker/client.rb', line 35 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
23 24 25 26 27 |
# File 'lib/booker/client.rb', line 23 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
29 30 31 32 33 |
# File 'lib/booker/client.rb', line 29 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 |
#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 |