Class: GoogleCalendar::Base

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

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



20
21
22
23
24
25
26
# File 'app/my_lib/google_calendar/base.rb', line 20

def initialize
  client.client_options.application_name = config.app_name
  client.client_options.application_version = config.app_version
  if Figaro.bool_env!('SIDEKIQ_CREATE_GOOGLE_CALENDAR_EVENT')
    client.authorization = Google::Auth.get_application_default config.scope
  end
end

Instance Method Details

#add_event(reservation, _restaurant, _visibility = 'public') ⇒ Object Also known as: add_reservation

Setup the payload and then execute a worker to insert event



55
56
57
58
# File 'app/my_lib/google_calendar/base.rb', line 55

def add_event(reservation, _restaurant, _visibility = 'public')
  GcalWorker.perform_async CALENDAR_ID, :insert_event, reservation.id
  true
end

#add_user_access(email, role = 'writer') ⇒ Object

Give user access to the calendar



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/my_lib/google_calendar/base.rb', line 38

def add_user_access(email, role = 'writer')
  unless client.list_acls(CALENDAR_ID).items.select { |item| item.id.include?(email) }.present?
    acl = {
      role: role,
      scope: {
        type: 'user',
        value: email,
      },
    }
    rule = GCalendar::AclRule.new acl
    return client.insert_acl CALENDAR_ID, rule
  end

  BUSINESS_LOGGER.info("#{email} already has access")
end

#build_add_event_payload(reservation, restaurant) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/my_lib/google_calendar/base.rb', line 95

def build_add_event_payload(reservation, restaurant)
  event_data = GCalendar::Event.new normalize(reservation)
  event_data.update!(
    visibility: VISIBILITY,
    summary: "Reservation for #{restaurant.name}",
    attendees: [
      {
        email: reservation.email.to_s.strip,
        additional_guests: reservation.party_size,
        comment: reservation.special_request,
        display_name: reservation.username,
        response_status: 'accepted',
      },
    ],
    description: "-- Reservations Details --\n" \
          "Diner's Name: #{reservation.username}\n" \
          "Diner's Phone: #{reservation.phone}\n" \
          "Date: #{reservation.date_format}\n" \
          "Time: #{reservation.start_time_format} (Restaurant's local time)\n" \
          "Party Size: #{reservation.party_size}\n" \
          "Special Request: #{reservation.special_request.presence || '-'}\n" \
          "Reservation No: #{reservation.id}",
  )
  event_data
end

#build_update_event_payload(reservation, restaurant) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/my_lib/google_calendar/base.rb', line 121

def build_update_event_payload(reservation, restaurant)
  event_data = GCalendar::Event.new normalize(reservation)
  event_data.update!(
    visibility: VISIBILITY,
    summary: "Reservation for #{restaurant.name}",
    attendees: [
      {
        email: reservation.email.to_s.strip,
        additional_guests: reservation.party_size,
        comment: reservation.special_request,
        display_name: reservation.username,
        response_status: 'accepted',
      },
    ],
    description: "-- Reservations Details --\n" \
        "Diner's Name: #{reservation.username}\n" \
        "Diner's Phone: #{reservation.phone}\n" \
        "Date: #{reservation.date_format}\n" \
        "Time: #{reservation.start_time_format} (Restaurant's local time)\n" \
        "Party Size: #{reservation.party_size}\n" \
        "Special Request: #{reservation.special_request.presence || '-'}\n" \
        "Reservation No: #{reservation.id}",
  )
  event_data
end

#clientObject

Actual Google Calendar instance



33
34
35
# File 'app/my_lib/google_calendar/base.rb', line 33

def client
  @client ||= GCalendar::CalendarService.new
end

#configObject



28
29
30
# File 'app/my_lib/google_calendar/base.rb', line 28

def config
  @config ||= GoogleCalendar.config
end

#delete_event(event_id) ⇒ Object Also known as: delete_reservation

Execute a worker to delete an event



74
75
76
# File 'app/my_lib/google_calendar/base.rb', line 74

def delete_event(event_id)
  GcalWorker.perform_async CALENDAR_ID, :delete_event, event_id
end

#get_event_detail(event_id) ⇒ Object Also known as: get_reservation_detail



79
80
81
# File 'app/my_lib/google_calendar/base.rb', line 79

def get_event_detail(event_id)
  client.get_event CALENDAR_ID, event_id
end

#normalize(reservation) ⇒ Object

Normalize given reservation data



85
86
87
88
# File 'app/my_lib/google_calendar/base.rb', line 85

def normalize(reservation)
  normalizer = GoogleCalendar::Normalizer.new(reservation)
  normalizer.normalized_data
end

#setup_domain_aclObject

used at initializer



91
92
93
# File 'app/my_lib/google_calendar/base.rb', line 91

def setup_domain_acl
  add_user_access 'surasit@hungryhub.com'
end

#update_event(event_id, reservation, _restaurant, _visibility = 'public') ⇒ Object Also known as: update_reservation

Setup the payload and then execute a worker to update an event



62
63
64
65
66
67
68
69
70
# File 'app/my_lib/google_calendar/base.rb', line 62

def update_event(event_id, reservation, _restaurant, _visibility = 'public')
  if event_id.nil? && !reservation.gcal_id.nil?
    event_id = reservation.gcal_id
  end
  return true if event_id.nil?

  GcalWorker.perform_async CALENDAR_ID, :update_event, reservation.id
  true
end