Class: Nexo::GoogleCalendarService

Inherits:
CalendarService show all
Defined in:
app/lib/nexo/api_client/google_calendar_service.rb

Overview

Wrapper around Google::Apis::CalendarV3

TODO: handle ServerError to be retried TODO! when event to update was deleted, create a new one and warn

Raises:

  • (Google::Apis::ServerError)

    An error occurred on the server and the request can be retried

  • (Google::Apis::AuthorizationError)

    Authorization is required

  • (Google::Apis::ClientError)

    The request is invalid and should not be retried without modification possible messages:

    • duplicate: The requested identifier already exists.

    • notFound: Not Found (calendar not exists or was deleted)

    • forbidden: Forbidden (event to update was deleted)

    • conditionNotMet: Precondition Failed (etag / if-match header verification failed)

    • invalid: Invalid sequence value. The specified sequence number is below

      the current sequence number of the resource. Re-fetch the resource and
      use its sequence number on the following request.
      

Direct Known Subclasses

GoogleCalendarSyncService

Instance Attribute Summary

Attributes inherited from CalendarService

#integration

Instance Method Summary collapse

Methods inherited from CalendarService

#initialize

Constructor Details

This class inherits a constructor from Nexo::CalendarService

Instance Method Details

#fields_from_payload(payload) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/lib/nexo/api_client/google_calendar_service.rb', line 118

def fields_from_payload(payload)
  event = validate_version!(payload)

  {
    date_from: parse_date(event.start),
    date_to: parse_date(event.end),
    time_from: parse_time(event.start),
    time_to: parse_time(event.end),
    summary: event.summary,
    description: event.description,

    # Posible status values:
    # - confirmed
    # - tentative
    # - cancelled (deleted)
    status: event.status
  }
end

#get_calendar(folder) ⇒ Object



86
87
88
89
90
91
# File 'app/lib/nexo/api_client/google_calendar_service.rb', line 86

def get_calendar(folder)
  validate_folder_state!(folder)

  response = client.get_calendar(folder.external_identifier)
  ApiResponse.new(payload: response.to_h, status: :ok, etag: response.etag, id: response.id)
end

#get_event(element) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/lib/nexo/api_client/google_calendar_service.rb', line 67

def get_event(element)
  validate_folder_state!(element.folder)
  # sidebranch
  # TODO!: validate uuid presence

  # would be nice to send If-None-Match header, but Google API doesn't seem
  # to accept it
  response = client.get_event(element.folder.external_identifier, element.uuid)

  ApiResponse.new(payload: response.to_h, status: :ok, etag: response.etag, id: response.id)
rescue Google::Apis::ClientError => e
  if e.message.match? "notFound"
    Nexo.logger.warn("Event not found for #{element.to_gid}")
    nil
  else
    raise
  end
end

#insert(element) ⇒ Object

Create an event in a Google Calendar

Parameters:



23
24
25
26
27
28
29
# File 'app/lib/nexo/api_client/google_calendar_service.rb', line 23

def insert(element)
  validate_folder_state!(element.folder)

  event = build_event(element)
  response = client.insert_event(element.folder.external_identifier, event)
  ApiResponse.new(payload: response.to_h, status: :ok, etag: response.etag, id: response.id)
end

#insert_calendar(folder) ⇒ Object

Create a Google calendar



94
95
96
97
98
99
100
# File 'app/lib/nexo/api_client/google_calendar_service.rb', line 94

def insert_calendar(folder)
  validate_folder_state!(folder, verify_external_identifier_presence: false)

  cal = build_calendar(folder)
  response = client.insert_calendar(cal)
  ApiResponse.new(payload: response.to_h, status: :ok, etag: response.etag, id: response.id)
end

#remove(element) ⇒ Object

Delete an event in a Google Calendar



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/lib/nexo/api_client/google_calendar_service.rb', line 51

def remove(element)
  validate_folder_state!(element.folder)
  # sidebranch
  # TODO!: validate uuid presence

  # TODO: try with cancelled / maybe its the same
  client.delete_event(element.folder.external_identifier, element.uuid, options: ifmatch_options(element))
  ApiResponse.new(payload: nil, status: :ok, etag: nil)
rescue Google::Apis::ClientError => e
  if e.message.match? /conditionNotMet/
    raise Errors::ConflictingRemoteElementChange, e
  else
    raise
  end
end

#remove_calendar(folder) ⇒ Object



111
112
113
114
115
116
# File 'app/lib/nexo/api_client/google_calendar_service.rb', line 111

def remove_calendar(folder)
  validate_folder_state!(folder)

  client.delete_calendar(folder.external_identifier)
  ApiResponse.new(status: :ok)
end

#update(element) ⇒ Object

Update an event in a Google Calendar



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/lib/nexo/api_client/google_calendar_service.rb', line 32

def update(element)
  validate_folder_state!(element.folder)
  # sidebranch
  # TODO!: validate uuid presence

  event = build_event(element)

  response = client.update_event(element.folder.external_identifier, element.uuid, event, options: ifmatch_options(element))

  ApiResponse.new(payload: response.to_h, status: :ok, etag: response.etag)
rescue Google::Apis::ClientError => e
  if e.message.match? /conditionNotMet/
    raise Errors::ConflictingRemoteElementChange, e
  else
    raise
  end
end

#update_calendar(folder) ⇒ Object

Update a Google calendar



103
104
105
106
107
108
109
# File 'app/lib/nexo/api_client/google_calendar_service.rb', line 103

def update_calendar(folder)
  validate_folder_state!(folder)

  cal = build_calendar(folder)
  response = client.update_calendar(folder.external_identifier, cal)
  ApiResponse.new(payload: response.to_h, status: :ok, etag: response.etag, id: response.id)
end