Class: GoogleCalendar::Normalizer
- Inherits:
-
Object
- Object
- GoogleCalendar::Normalizer
- Defined in:
- app/my_lib/google_calendar/normalizer.rb
Instance Attribute Summary collapse
-
#reservation ⇒ Object
readonly
Returns the value of attribute reservation.
-
#restaurant_time_zone ⇒ Object
readonly
Returns the value of attribute restaurant_time_zone.
Instance Method Summary collapse
-
#end_date ⇒ String
Get the end date in YYYY-MM-DD format.
-
#end_date_time ⇒ String
Get the end date time in RFC3339 format with milliseconds.
-
#initialize(reservation) ⇒ Normalizer
constructor
A new instance of Normalizer.
-
#normalized_data ⇒ Hash
Normalize reservation data to be compatible with Google Calendar event format.
-
#start_date ⇒ String
Get the start date in YYYY-MM-DD format.
-
#start_date_time ⇒ String
Get the start date time in RFC3339 format with milliseconds.
-
#status ⇒ String
Map reservation status to Google Calendar event status.
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
#reservation ⇒ Object (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_zone ⇒ Object (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_date ⇒ String
Get the end date in YYYY-MM-DD format
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_time ⇒ String
Get the end date time in RFC3339 format with milliseconds
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_data ⇒ Hash
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
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_date ⇒ String
Get the start date in YYYY-MM-DD format
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_time ⇒ String
Get the start date time in RFC3339 format with milliseconds
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 |
#status ⇒ String
Map reservation status to Google Calendar 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 |