Class: Parsec::Availability

Inherits:
Base
  • Object
show all
Defined in:
lib/parsec/availability.rb

Constant Summary collapse

MEAL_PLANS =
{
  'RO' => 'Room Only',
  'BB' => 'Breakfast is included',
  'HB' => 'Half board (two meals) included',
  'FB' => 'Full board (three meals) included',
  'AI' => 'All Inclusive'
}.freeze
PACKAGE_RATES_CODE =
'PK'

Constants inherited from Base

Base::DATE_FORMAT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hotel_code:, hotel_provider_code: nil, room_code: nil, room_name: nil, rates:, rph: nil, description: nil, extra: nil, special: nil, package: false) ⇒ Availability

rubocop:disable Metrics/ParameterLists



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/parsec/availability.rb', line 18

def initialize(hotel_code:, hotel_provider_code: nil, room_code: nil, room_name: nil, rates:,
               rph: nil, description: nil, extra: nil, special: nil, package: false)
  @hotel_code = hotel_code
  @hotel_provider_code = hotel_provider_code
  @room_code = room_code
  @room_name = room_name
  @package = package
  @rates = rates
  @rph = rph
  @description = description
  @extra = extra
  @special = special
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



15
16
17
# File 'lib/parsec/availability.rb', line 15

def description
  @description
end

#extraObject (readonly)

Returns the value of attribute extra.



15
16
17
# File 'lib/parsec/availability.rb', line 15

def extra
  @extra
end

#hotel_codeObject (readonly)

Returns the value of attribute hotel_code.



15
16
17
# File 'lib/parsec/availability.rb', line 15

def hotel_code
  @hotel_code
end

#hotel_provider_codeObject (readonly)

Returns the value of attribute hotel_provider_code.



15
16
17
# File 'lib/parsec/availability.rb', line 15

def hotel_provider_code
  @hotel_provider_code
end

#ratesObject (readonly)

Returns the value of attribute rates.



15
16
17
# File 'lib/parsec/availability.rb', line 15

def rates
  @rates
end

#room_codeObject (readonly)

Returns the value of attribute room_code.



15
16
17
# File 'lib/parsec/availability.rb', line 15

def room_code
  @room_code
end

#room_nameObject (readonly)

Returns the value of attribute room_name.



15
16
17
# File 'lib/parsec/availability.rb', line 15

def room_name
  @room_name
end

#rphObject (readonly)

Returns the value of attribute rph.



15
16
17
# File 'lib/parsec/availability.rb', line 15

def rph
  @rph
end

#specialObject (readonly)

Returns the value of attribute special.



15
16
17
# File 'lib/parsec/availability.rb', line 15

def special
  @special
end

Class Method Details

.build(room, hotel_info, nights) ⇒ Object

rubocop:disable Metrics/AbcSize



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/parsec/availability.rb', line 39

def build(room, hotel_info, nights)
  new(hotel_code: hotel_info[:@hotel_code],
      hotel_provider_code: hotel_info[:hotel_provider_code],
      room_code: room[:room_type][:@code],
      room_name: room[:room_type][:@name],
      package: package_rates?(room),
      description: room[:room_type][:description],
      extra: room[:room_type].dig(:extra_info, :msg),
      special: room[:room_type][:special],
      rates: Array.wrap(room[:room_rates][:room_rate]).map { |rate| build_rate(rate, nights) },
      rph: room[:@rph].split(',').first)
end

.build_cancellation(cancellation) ⇒ Object

rubocop:enable Metrics/AbcSize, Metrics/LineLength



73
74
75
76
77
78
79
80
81
# File 'lib/parsec/availability.rb', line 73

def build_cancellation(cancellation)
  # return if cancellation.nil?
  return 'Non-refundable rate' if cancellation[:@non_refundable] == '1'

  penalties = Array.wrap(cancellation[:cancel_penalty]).map { |p| build_penalty(p) }
  penalties << cancellation[:description] if cancellation[:description].present?

  penalties.join(' ')
end

.build_customer_cancellation(cancellation, start_date) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/parsec/availability.rb', line 89

def build_customer_cancellation(cancellation, start_date)
  # return if cancellation.nil?
  return 'Special non-refundable rate' if cancellation.slice(:@non_refundable, :@cancellation_costs_today).value?('1')

  deadline = Array.wrap(cancellation[:cancel_penalty]).max_by { |p| p[:deadline][:@units].to_i }
  return 'Free cancellation' if deadline.nil?

  deadline_date = Date.strptime(start_date, self::DATE_FORMAT) - (deadline[:deadline][:@units].to_i + 1).days

  "Free cancellation until #{deadline_date.strftime('%-d %B')} at 12:00PM, local time"
end

.build_deadline_date(cancellation, start_date) ⇒ Object



101
102
103
104
105
106
# File 'lib/parsec/availability.rb', line 101

def build_deadline_date(cancellation, start_date)
  return Time.zone.today if cancellation[:@non_refundable] == '1'

  deadline = Array.wrap(cancellation[:cancel_penalty]).max_by { |p| p[:deadline][:@units].to_i }
  Date.strptime(start_date, self::DATE_FORMAT) - (deadline&.dig(:deadline, :@units).to_i + 1).days
end

.build_penalty(penalty) ⇒ Object



83
84
85
86
87
# File 'lib/parsec/availability.rb', line 83

def build_penalty(penalty)
  p = [penalty[:deadline][:@units], penalty[:deadline][:@time_unit], '=', "$#{penalty[:charge][:@amount]};"]
  p << "(#{penalty[:msg]})" if penalty[:msg].present?
  p.join(' ')
end

.build_rate(rate, nights = 1) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/LineLength



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/parsec/availability.rb', line 60

def build_rate(rate, nights = 1)
  price = rate[:total][:@amount].to_f.ceil
  { price: price,
    price_per_night: (price.to_f / nights).round(2),
    net_price: rate[:cost][:@amount].to_f,
    cancellation: build_customer_cancellation(rate[:cancel_penalties], Array.wrap(rate[:rates][:rate])[0][:@effective_date]),
    penalties: build_cancellation(rate[:cancel_penalties]),
    deadline: build_deadline_date(rate[:cancel_penalties], Array.wrap(rate[:rates][:rate])[0][:@effective_date]),
    meal_plan: MEAL_PLANS[rate[:@meal_plan]],
    booking_code: rate[:@booking_code] }
end

.package_rates?(room) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/parsec/availability.rb', line 108

def package_rates?(room)
  room[:room_type][:@product_type_info1] == PACKAGE_RATES_CODE
end

.search(room, hotel_code) ⇒ Object

rubocop:enable Metrics/AbcSize



53
54
55
56
57
# File 'lib/parsec/availability.rb', line 53

def search(room, hotel_code)
  new(hotel_code: hotel_code,
      package: package_rates?(room),
      rates: Array.wrap(room[:room_rates][:room_rate]).map { |rate| build_rate(rate) })
end

Instance Method Details

#package_rates?Boolean

rubocop:enable Metrics/ParameterLists

Returns:

  • (Boolean)


33
34
35
# File 'lib/parsec/availability.rb', line 33

def package_rates?
  @package
end