Class: Globalticket::API

Inherits:
Object
  • Object
show all
Defined in:
lib/globalticket/api.rb

Overview

The communication layer implements all the methods available in the Globalticket API globalreseller.nl/documentation/

Constant Summary collapse

ENDPOINT_PREFIX =
"https://globalreseller.nl/webservices"

Class Method Summary collapse

Class Method Details

.add_contact(data = {}) ⇒ Object

Add a contact to a reservation globalreseller.nl/documentation/api/addContact



49
50
51
# File 'lib/globalticket/api.rb', line 49

def self.add_contact(data = {})
  self.make_api_call(endpoint: "addContact", data: data)
end

.cancel_reservation(userId: nil, reservationId: nil) ⇒ Object



61
62
63
# File 'lib/globalticket/api.rb', line 61

def self.cancel_reservation(userId: nil, reservationId: nil)
  self.make_api_call(endpoint: "cancelReservation", data: {userId: userId.to_s, reservationId: reservationId.to_s})
end

.complete_reservation(userId: nil, reservationId: nil) ⇒ Object



55
56
57
# File 'lib/globalticket/api.rb', line 55

def self.complete_reservation(userId: nil, reservationId: nil)
  self.make_api_call(endpoint: "completeReservation", data: {userId: userId.to_s, reservationId: reservationId.to_s})
end

.convert_date_object_to_format(date: nil, format: "%Y-%m-%d") ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/globalticket/api.rb', line 65

def self.convert_date_object_to_format(date: nil, format: "%Y-%m-%d")
  if date.is_a?(String)
    d = Date.parse(date)
  elsif date.is_a?(Date)
    d = date
  elsif date.is_a?(Time)
    d = date
  else
    raise "Invalid date. Should be of type Date, Time or String."
  end
  return d.strftime(format)
end

.create_reservation(userId: nil, tickets: [], ticketDate: nil, ticketTime: nil) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/globalticket/api.rb', line 38

def self.create_reservation(userId: nil, tickets: [], ticketDate: nil, ticketTime: nil)

  # convert dates to required string
  ticketDate = self.convert_date_object_to_format(date: ticketDate, format: "%Y-%m-%d") unless ticketDate.nil?
  ticketTime   = self.convert_date_object_to_format(date: ticketTime, format: "%H:%M") unless ticketTime.nil?

  self.make_api_call(endpoint: "createReservation", data: {userId: userId.to_s, tickets: tickets, ticketDate: ticketDate, ticketTime: ticketTime})
end

.get_availability(userId: nil, start_date: nil, end_date: nil) ⇒ Object

Fetch available periods and time slots globalreseller.nl/documentation/api/getAvailability



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/globalticket/api.rb', line 24

def self.get_availability(userId: nil, start_date: nil, end_date: nil)
  # set default dates if none are given
  start_date = Time.now if start_date.nil?
  end_date = start_date.to_time + ((3600 * 24) * 30) if end_date.nil? # add 31 days, max allowed

  # convert dates to required string
  start_date = self.convert_date_object_to_format(date: start_date, format: "%Y-%m-%d")
  end_date   = self.convert_date_object_to_format(date: end_date, format: "%Y-%m-%d")

  self.make_api_call(endpoint: "getAvailability", data: {userId: userId.to_s, startDate: start_date, endDate: end_date})
end

.get_available_usersObject

returns a list of available users



12
13
14
# File 'lib/globalticket/api.rb', line 12

def self.get_available_users
  self.make_api_call(endpoint: "getAvailableUsers", data: {})
end

.get_ticket_types(userId: nil, language: nil) ⇒ Object

return a list of ticket types for given userId Original documentation: globalreseller.nl/documentation/api/getTicketTypes



18
19
20
# File 'lib/globalticket/api.rb', line 18

def self.get_ticket_types(userId: nil, language: nil)
  self.make_api_call(endpoint: "getTicketTypes", data: {userId: userId.to_s, language: language})
end

.make_api_call(endpoint: '', data: {}) ⇒ Object

communicate with the API via POST requests and return the return message in a ruby-esque manor.



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/globalticket/api.rb', line 101

def self.make_api_call(endpoint: '', data: {})
  url = "#{ENDPOINT_PREFIX}/#{endpoint}"
  # puts "posting to URL: #{url}:\n#{self.request_data(data)}"
  result = JSON.parse(HTTParty.post(url,
            body: self.request_data(data).to_json,
            headers: { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }).body)
  if result["success"] == false
    raise result["errorMessage"]
  else
    # puts result
    return result
  end
end

.request_data(data) ⇒ Object

return the request data as needed for the API

  • sorted alphabetically

  • ending with HMAC calculation



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/globalticket/api.rb', line 81

def self.request_data(data)
  # delet data wit nil values
  data = data.select {|k, v| !v.nil? }

  # merge data with api-key and env
  unsorted_data = { apiKey: Config.api_key, environment: Config.environment }.merge(data)

  # sort alphabetically
  sorted_data = Hash[unsorted_data.sort_by{|k,v| k}]

  # calculate HMAC value
  calculated_hmac = Base64.encode64(OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), Config.api_secret, sorted_data.to_json.strip)).strip.gsub("\n", "")

  request_data = sorted_data.merge({"HMACKey" => calculated_hmac})
  # puts request_data.to_json
  return request_data
end