class PrioTicket::Booking

Describes a Booking This API is called to get a ticket booked and get a barcode/QR-code in response. The booking API provides a booking reference in response. There are 5 different booking options:

Attributes

booking_details[RW]
booking_email[RW]
booking_name[RW]
booking_reference[RW]

completed booking attrs

booking_status[RW]
booking_type[RW]

attr_accessor :booking_type

city[RW]
contact[RW]
distributor_id[RW]
distributor_reference[RW]
identifier[RW]
notes[RW]
phone_number[RW]

contact details

postal_code[RW]
product_language[RW]
reservation_reference[RW]
street[RW]

Public Class Methods

cancel(distributor_id: nil, booking_reference: nil, distributor_reference: nil, identifier: nil) click to toggle source

Cancels a Booking

@return Booking

# File lib/prioticket/booking.rb, line 86
def self.cancel(distributor_id: nil, booking_reference: nil, distributor_reference: nil, identifier: nil)
  body = {
    request_type: "cancel_booking",
    data: {
      distributor_id: distributor_id,
      booking_reference: booking_reference,
      distributor_reference: booking_reference  
    }
  }
  result = PrioTicket::API.call(body, identifier)
  booking = PrioTicket::Booking.new(result["data"])
  return booking
end
get_status(distributor_id: nil, booking_reference: nil, distributor_reference: nil, identifier: nil) click to toggle source

Gets the status from a Booking

@return Booking

# File lib/prioticket/booking.rb, line 104
def self.get_status(distributor_id: nil, booking_reference: nil, distributor_reference: nil, identifier: nil)
  body = {
    request_type: "booking_status",
    data: {
      distributor_id: distributor_id,
      booking_reference: booking_reference,
      distributor_reference: booking_reference  
    }
  }
  result = PrioTicket::API.call(body, identifier)
  booking = PrioTicket::Booking.new(result["data"])
  return booking
end
new(args) click to toggle source
# File lib/prioticket/booking.rb, line 39
def initialize(args)
  return if args.nil?
  args.each do |k,v|
    PrioTicket.parse_json_value(self, k,v)
  end
end

Public Instance Methods

canceled() click to toggle source
# File lib/prioticket/booking.rb, line 54
def canceled
  booking_status == "Cancelled"
end
Also aliased as: canceled?
canceled?()
Alias for: canceled
save() click to toggle source

Sends the reservation request tot the API

# File lib/prioticket/booking.rb, line 49
def save
  request_booking
end
success() click to toggle source
# File lib/prioticket/booking.rb, line 60
def success
    booking_status == "Confirmed"
end
ticket() click to toggle source

Fetches information from ticket_details, to validate the input for this booking. e.g. if ticket_type == 2, from/to date are required.

@return [type] [description]

# File lib/prioticket/booking.rb, line 73
def ticket
  begin
    @ticket ||= PrioTicket::TicketDetails.find(distributor_id: distributor_id, ticket_id: booking_type['ticket_id'], identifier: identifier)
  rescue
    false
  end
end

Private Instance Methods

parse_result(result) click to toggle source

Parses the return value from the API

# File lib/prioticket/booking.rb, line 192
def parse_result(result)
  self.booking_status     = result["data"]["booking_status"]
  self.booking_reference    = result["data"]["booking_reference"]
  PrioTicket.parse_json_value(self, :booking_details, result["data"]["booking_details"])
end
request_body() click to toggle source

Computes the details to send to the API

@return Hash

# File lib/prioticket/booking.rb, line 134
def request_body
  for att in [:distributor_id, :booking_type, :booking_name, :booking_email, :contact, :notes, :product_language, :distributor_reference]
    raise PrioTicketError.new("Booking is missing attribute `#{att}` (Hash)") unless send(att)
  end
  body = {
    request_type: "booking",
    data: {
            distributor_id: distributor_id,
            booking_type: validated_booking_type_hash,
            booking_name: booking_name,
            booking_email: booking_email,
            contact: PrioTicket.openstruct_to_hash(contact).to_h,
            notes: notes.to_a,
            product_language: product_language,
            distributor_reference: distributor_reference
    }
  }
  
  # add pickuppoint to body, if present
  # body[:data][:pickup_point_id] = pickup_point_id if pickup_point_id
  return body
end
request_booking() click to toggle source

Sends the reservation request to the API endpoint and enriches current object with status and reference.

# File lib/prioticket/booking.rb, line 124
def request_booking
  result = PrioTicket::API.call(request_body, identifier)
  parse_result(result)
end
validated_booking_type_hash() click to toggle source

Calculates and validates the information for 'booking_type'

@return Hash

# File lib/prioticket/booking.rb, line 161
def validated_booking_type_hash
  
  # loops through all the booking details and raises error
  # if from/to date_time are not present.
  if ticket
    if [2,3].include?(ticket.ticket_class)
      unless booking_type.from_date_time && booking_type.to_date_time
        unless booking_type.reservation_reference && booking_type.reservation_reference != ''
          puts booking_type.inspect
          err_msg = "The `booking_type` attribute requires a from_date_time and to_date_time for a ticket of ticket_class #{ticket.ticket_class}."
          raise PrioTicketError.new(err_msg)
        end
      end
    end
  end
  data = {}

  data[:ticket_id]              = booking_type.ticket_id unless booking_type.reservation_reference
  data[:booking_details]        = booking_type.booking_details.map{|bd| PrioTicket.openstruct_to_hash(bd)} unless booking_type.reservation_reference
  data[:reservation_reference]  = booking_type.reservation_reference if booking_type.reservation_reference
  
  if booking_type.from_date_time && booking_type.to_date_time
    data[:from_date_time] = booking_type.from_date_time
    data[:to_date_time]   = booking_type.to_date_time
  end
  return data
end