Class: GoogleCalendar::Normalizer

Inherits:
Object
  • Object
show all
Defined in:
app/my_lib/google_calendar/normalizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reservation) ⇒ Normalizer

Returns a new instance of Normalizer.



9
10
11
12
# File 'app/my_lib/google_calendar/normalizer.rb', line 9

def initialize(reservation)
  @reservation = reservation
  @restaurant_time_zone = reservation.restaurant&.time_zone || HungryHub::Time::DEFAULT_ZONE
end

Instance Attribute Details

#reservationObject (readonly)

Returns the value of attribute reservation.



7
8
9
# File 'app/my_lib/google_calendar/normalizer.rb', line 7

def reservation
  @reservation
end

#restaurant_time_zoneObject (readonly)

Returns the value of attribute restaurant_time_zone.



7
8
9
# File 'app/my_lib/google_calendar/normalizer.rb', line 7

def restaurant_time_zone
  @restaurant_time_zone
end

Instance Method Details

#end_dateString

Get the end date in YYYY-MM-DD format

Returns:

  • (String)

    formatted date



44
45
46
# File 'app/my_lib/google_calendar/normalizer.rb', line 44

def end_date
  Date.parse(reservation.date.to_s).strftime('%Y-%m-%d')
end

#end_date_timeString

Get the end date time in RFC3339 format with milliseconds

Returns:

  • (String)

    formatted date time



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/my_lib/google_calendar/normalizer.rb', line 64

def end_date_time
  reservation_duration = reservation.restaurant&.res_duration || 60
  end_time = reservation.end_time.presence || (reservation.start_time + reservation_duration.minutes)

  time_str = end_time.strftime('%H:%M')

  # Combine reservation date and extracted time in restaurant time zone
  combined_time = Time.use_zone(restaurant_time_zone) { Time.zone.parse("#{reservation.date} #{time_str}") }

  # If end time is earlier than start time, it means the reservation ends the next day
  start_time_obj = Time.use_zone(restaurant_time_zone) { Time.zone.parse(start_date_time) }
  if combined_time < start_time_obj
    combined_time += 1.day
  end

  # Return RFC3339 with milliseconds
  combined_time.rfc3339(3)
end

#normalized_dataHash

Note:

The time_zone should match the restaurant's time zone to ensure correct timing in Google Calendar.

Normalize reservation data to be compatible with Google Calendar event format

Examples:

{
  start: Google::Apis::CalendarV3::EventDateTime.new(date_time: '2023-10-10T12:00:00+07:00', time_zone: 'Asia/Bangkok'),
  end: Google::Apis::CalendarV3::EventDateTime.new(date_time: '2023-10-10T13:00:00+07:00', time_zone: 'Asia/Bangkok'),
  status: 'confirmed'
}

Returns:

  • (Hash)

    normalized data



24
25
26
27
28
29
30
# File 'app/my_lib/google_calendar/normalizer.rb', line 24

def normalized_data
  {
    start: Google::Apis::CalendarV3::EventDateTime.new(date_time: start_date_time, time_zone: restaurant_time_zone),
    end: Google::Apis::CalendarV3::EventDateTime.new(date_time: end_date_time, time_zone: restaurant_time_zone),
    status: status,
  }
end

#start_dateString

Get the start date in YYYY-MM-DD format

Returns:

  • (String)

    formatted date



36
37
38
# File 'app/my_lib/google_calendar/normalizer.rb', line 36

def start_date
  Date.parse(reservation.date.to_s).strftime('%Y-%m-%d')
end

#start_date_timeString

Get the start date time in RFC3339 format with milliseconds

Returns:

  • (String)

    formatted date time



51
52
53
54
55
56
57
58
59
# File 'app/my_lib/google_calendar/normalizer.rb', line 51

def start_date_time
  time_str = reservation.start_time.strftime('%H:%M')

  # Combine reservation date and extracted time in restaurant time zone
  combined_time = Time.use_zone(restaurant_time_zone) { Time.zone.parse("#{reservation.date} #{time_str}") }

  # Return RFC3339 with milliseconds
  combined_time.rfc3339(3)
end

#statusString

Map reservation status to Google Calendar event status

Returns:

  • (String)

    event status



86
87
88
89
90
91
92
93
94
95
# File 'app/my_lib/google_calendar/normalizer.rb', line 86

def status
  case reservation.status
  when 'Arrived'
    'confirmed'
  when 'Cancel'
    'cancelled'
  else
    'tentative'
  end
end