Class: ShakeTheCounter::API
- Inherits:
-
Object
- Object
- ShakeTheCounter::API
- Defined in:
- lib/shake_the_counter/api.rb
Overview
The communication layer implements all the methods available in the ShakeTheCounter API ticketstest.ticketcounter.nl/swagger/ui/index
Class Method Summary collapse
-
.call(path, http_method: :get, body: {}, header: {}) ⇒ Object
Makes a HTTP POST call to the endpoint en returns the response.
-
.endpoint ⇒ type
Returns the API endpoint to use.
-
.ping ⇒ Object
Checks the connection GET /api/v1/ping.
-
.url(path) ⇒ Object
Returns the complete API URL.
Class Method Details
.call(path, http_method: :get, body: {}, header: {}) ⇒ Object
Makes a HTTP POST call to the endpoint en returns the response
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 67 68 69 70 71 72 73 74 |
# File 'lib/shake_the_counter/api.rb', line 39 def self.call(path, http_method: :get, body: {}, header: {}) values = body unless header[:content_type] if body && body != {} header[:content_type] = "application/json" else # blank body requires no JSON header[:content_type] = "application/x-www-form-urlencoded" end end begin if ShakeTheCounter::Config.verbose puts "Calling #{http_method.upcase} to #{url(path)}" puts "Header: #{header.to_json}" puts "Body:" puts values.to_json end if http_method == :post response = RestClient.post url(path), values, header else response = RestClient.get url(path), header end rescue RestClient::ExceptionWithResponse => e raise ShakeTheCounterError.new "#{e} #{e.response}" end if ShakeTheCounter::Config.verbose puts "Result:\n#{response.inspect}" end if response.body != '' object = JSON.parse(response.body) else object = response end return object end |
.endpoint ⇒ type
Returns the API endpoint to use
11 12 13 14 15 16 17 |
# File 'lib/shake_the_counter/api.rb', line 11 def self.endpoint if ShakeTheCounter::Config.environment.to_s == "test" "https://apitest.shakethecounter.com" else "https://api.shakethecounter.com" end end |
.ping ⇒ Object
Checks the connection GET /api/v1/ping
80 81 82 |
# File 'lib/shake_the_counter/api.rb', line 80 def self.ping call("ping", http_method: :get) end |
.url(path) ⇒ Object
Returns the complete API URL
23 24 25 26 27 28 29 30 |
# File 'lib/shake_the_counter/api.rb', line 23 def self.url(path) # Don't change the path if it already is a full url if path.include?('http') return path else "#{endpoint}/api/v#{ShakeTheCounter::Config.version}/#{path}" end end |