henkm/prioticket

View on GitHub
lib/prioticket/api.rb

Summary

Maintainability
A
1 hr
Test Coverage
module PrioTicket

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

    # 
    # Returns the API endpoint to use
    # 
    # @return [type] [description]
    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

    # 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: 
    # 
    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)
      
      # 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

    # 
    # Computes the request header
    # @param request_identifier="" [type] A unique request identifier, 
    # e.g. timestamp order_id, booking_reference or UUID.
    # 
    # @return [Hash] the header details for API calls
    def self.request_header(request_identifier="")
      { 
        content_type: "application/json", 
        x_request_authentication: request_authentication_key(request_identifier), 
        x_request_identifier: request_identifier
      }
    end
    

    # 
    # Makes a HTTP POST call to the endpoint en returns the response
    # 
    # @param request_body [type] [description]
    # @param request_identifier [type] [description]
    # 
    # @return OpenStruct object with nested methods.
    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
        if verbose || PrioTicket::Config.verbose == true
          puts "Response:"
          puts response.body
        end
      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
  end
end