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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/leisureking/api.rb', line 59 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.token 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 |
# 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 || endpoint == "authenticate" LeisureKing::Authentication.get_token end if LeisureKing::Config.verbose puts "Sending request to #{url}" puts "Body:\n#{body}" if body end begin result = RestClient.post(url, body.to_json, 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 "Body:\n#{body}" if body end RestClient.post(redirected_url, body.to_json, headers) else response.return!(request, res, &block) end end rescue RestClient::, RestClient::Forbidden => err raise "Access denied. API Response:\n#{err}" end puts "Result:\n#{result}" if LeisureKing::Config.verbose # begin return JSON.parse(result) # rescue # return result # end rescue RestClient::BadRequest => err raise "Bad Request. API Response:\n#{err.response}" end end |