Class: QuickTravel::Api
- Inherits:
-
Object
- Object
- QuickTravel::Api
- Includes:
- HTTParty
- Defined in:
- lib/quick_travel/adapter.rb
Defined Under Namespace
Classes: FilterQuery
Class Method Summary collapse
- .call_and_validate(http_method, path, query = {}, opts = {}) ⇒ Object
- .response_contains_error?(response) ⇒ Boolean
-
.validate!(response) ⇒ Object
Do standard validations on response.
Class Method Details
.call_and_validate(http_method, path, query = {}, opts = {}) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/quick_travel/adapter.rb', line 211 def self.call_and_validate(http_method, path, query = {}, opts = {}) http_params = opts.clone # Set default token http_params[:query] ||= FilterQuery.new(query).call http_params[:headers] ||= {} http_params[:headers]['Content-length'] = '0' if http_params[:body].blank? http_params[:headers]['x-api-key'] = QuickTravel.config.access_key http_params[:headers]['user-agent'] = 'quicktravel_client/' + QuickTravel::VERSION; expect = http_params.delete(:expect) # Use :body instead of :query for put/post. # # Causes webrick to give back error - -maybe other servers too. http_params[:body] ||= {} if [:put, :post].include?(http_method.to_sym) http_params[:body].merge!(http_params.delete(:query)) end http_params[:follow_redirects] = false begin response = self.send(http_method, path, http_params) rescue Errno::ECONNREFUSED raise ConnectionError.new('Connection refused') rescue SocketError raise ConnectionError.new('Socket error') rescue Timeout::Error raise ConnectionError.new('Timeout error') end if expect && expect == :json && !response.parsed_response.is_a?(Hash) fail AdapterError, <<-FAIL Request expected to be json but failed. Debug information below: http_method: #{http_method.inspect} path: #{path.inspect} http_params: #{http_params.inspect} response object: #{response.inspect} parsed_response: #{response.parsed_response.inspect} FAIL end validate!(response) response end |
.response_contains_error?(response) ⇒ Boolean
274 275 276 277 |
# File 'lib/quick_travel/adapter.rb', line 274 def self.response_contains_error?(response) parsed_response = response.parsed_response parsed_response.is_a?(Hash) && parsed_response.key?('error') end |
.validate!(response) ⇒ Object
Do standard validations on response
Firstly, check if a valid HTTP code was returned Secondly, check for presence of “error” key in returned hash
261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/quick_travel/adapter.rb', line 261 def self.validate!(response) case response.code when 300..399 # redirects fail ConnectionError.new('We were redirected. QT YML configuration appears to be incorrect. Verify your URL and API.') when 400..599 # client and server errors fail AdapterError.new(response) end if response_contains_error?(response) fail AdapterError, response end end |