Class: PrioTicket::API

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

Overview

The communication layer implements all the methods available in the PrioTicket API NOTE: You need to request access to view the documentation. support.prioticket.com/docs/

Class Method Summary collapse

Class Method Details

.call(request_body, request_identifier, verbose = false) ⇒ Object

Makes a HTTP POST call to the endpoint en returns the response



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/prioticket/api.rb', line 69

def self.call(request_body, request_identifier, verbose=false)
  values   = request_body.to_json
  headers  = request_header(request_identifier)
  if verbose || PrioTicket::Config.verbose == true
    puts "Calling with:"
    puts "Identifier: #{request_identifier}"
    puts "Header: #{headers.to_json}"
    puts "Body: #{values}"
    puts "Endpoint: #{endpoint}"
  end
  raise PrioTicketError.new "Request Identifier is not present, please provide an @identifier" if request_identifier.nil? || request_identifier == ''
  begin
    response = RestClient.post endpoint, values, headers
  rescue RestClient::ExceptionWithResponse => e
    if verbose || PrioTicket::Config.verbose == true
      puts "Error: #{e.response}"
    end
    error_json = JSON.parse(e.response)
    message = "#{error_json["error_message"]} (#{error_json["error_code"]}) - #{e}"
    raise PrioTicketError.new message
  end
  object   = JSON.parse(response.body)
  return object
end

.endpointtype

Returns the API endpoint to use



12
13
14
15
16
17
18
# File 'lib/prioticket/api.rb', line 12

def self.endpoint
  if PrioTicket::Config.environment.to_s == "test"
    "https://test-api.prioticket.com/v2.4/booking_service"
  else
    "https://api.prioticket.com/v2.4/booking_service"
  end
end

.request_authentication_key(request_identifier = "") ⇒ Object

The request authentication key will contain the following items: (a) API Key Token (b) Request Identifier

The API Key Token information for the TEST and LIVE environment will be sent in a separate mail. The Request Identifier should be an unique string generated by the initiator. Common examples are a timestamp, booking reference or UUID.The request authentication key will be computed per request as follows:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/prioticket/api.rb', line 29

def self.request_authentication_key(request_identifier="")
  # 1. Create a string with format <x-request-identifier>:<apikeytoken> where x-request-identifier 
  #    and apikeytoken are respective strings.
  step_1_string = "#{request_identifier}:#{Config.api_key}"

  # 2. Convert the string from step 1 to byte array using UTF-8 encoding.
  step_2_string = step_1_string.encode('utf-8')

  # 3. Compute the SHA-256 hash for the byte array from step 2. The result will be a byte array.
  step_3_string = Digest::SHA256.digest(step_2_string).strip
  
  # 4. Base64 encode the byte array as computed in step 3. 
  #    This string will be the x-request-authentication key for this request.
  step_4_string = Base64.encode64(step_3_string).strip

  return step_4_string
end

.request_header(request_identifier = "") ⇒ Hash

Computes the request header e.g. timestamp order_id, booking_reference or UUID.



53
54
55
56
57
58
59
# File 'lib/prioticket/api.rb', line 53

def self.request_header(request_identifier="")
  { 
    content_type: "application/json", 
    x_request_authentication: request_authentication_key(request_identifier), 
    x_request_identifier: request_identifier
  }
end