Module: LeisureKing::API
- Defined in:
- lib/leisureking/api.rb
Class Method Summary collapse
-
.base_uri ⇒ Object
Sends a request to the LeisureKing API and returns the parsed results.
-
.headers ⇒ Object
HTTP headers to send in each request.
- .send_request(endpoint, body = {}) ⇒ Object
Class Method Details
.base_uri ⇒ Object
Sends a request to the LeisureKing API and returns the parsed results
11 12 13 |
# File 'lib/leisureking/api.rb', line 11 def base_uri "https://api.leisureking.eu/v#{LeisureKing::Config.api_version}" end |
.headers ⇒ Object
HTTP headers to send in each request
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/leisureking/api.rb', line 64 def headers h = { content_type: :json, accept: :json, cache_control: 'no-cache' } # add requesters email address for human communication if LeisureKing::Config.client_email && LeisureKing::Config.client_email != '' h[:from] = LeisureKing::Config.client_email # email address for human communication end if LeisureKing::Authentication.has_valid_api_key h["Authorization"] = "Token #{LeisureKing::Authentication.token}" end return h end |
.send_request(endpoint, body = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 |
# File 'lib/leisureking/api.rb', line 15 def send_request(endpoint, body = {}) url = "#{base_uri}/#{endpoint}" body[:environment] = LeisureKing::Config.environment begin unless LeisureKing::Authentication.has_valid_api_key unless endpoint == "authenticate" puts "Requesting new API Token" if LeisureKing::Config.verbose LeisureKing::Authentication.get_token end end if LeisureKing::Config.verbose puts "Sending request to #{url}" puts "Body:\n#{body.to_json}" if body end begin result = RestClient.post(url, body, headers) do |response, request, res, &block| if [301, 302, 307].include? response.code redirected_url = response.headers[:location] if LeisureKing::Config.verbose puts "Redirecting request to #{redirected_url}" puts "Headers:\n#{headers.to_json}" if headers puts "Body:\n#{body.to_json}" if body end RestClient.post(redirected_url, body.to_json, headers) else response.return!(request, res, &block) end end rescue RestClient::, RestClient::Forbidden => err if LeisureKing::Config.verbose puts "Backtrace:" puts err.backtrace end raise "Access denied. API Response:\n#{err}." end puts "Result:\n#{result}" if LeisureKing::Config.verbose return JSON.parse(result) rescue RestClient::BadRequest => err raise "Bad Request. API Response:\n#{err.response}" end end |