Class: GoogleCalendar::Event

Inherits:
Object
  • Object
show all
Extended by:
Connection
Defined in:
lib/google_calendar_oauth2/event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Connection

client, connection

Constructor Details

#initialize(attrs) ⇒ Event Also known as: attributes=

Returns a new instance of Event.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/google_calendar_oauth2/event.rb', line 7

def initialize(attrs)
  @id = attrs['id']
  @etag = attrs['etag']
  @summary = attrs['summary']
  @status = attrs['status']
  @html_link = attrs['htmlLink']
  @created_at = attrs['created']
  @updated_at = attrs['updated']
  @calendar_id = attrs['calendar_id']
  @sequence = attrs['sequence']
  @start_time = attrs['start']['dateTime']
  @end_time = attrs['end']['dateTime']
end

Instance Attribute Details

#calendar_idObject

Returns the value of attribute calendar_id.



3
4
5
# File 'lib/google_calendar_oauth2/event.rb', line 3

def calendar_id
  @calendar_id
end

#created_atObject

Returns the value of attribute created_at.



3
4
5
# File 'lib/google_calendar_oauth2/event.rb', line 3

def created_at
  @created_at
end

#end_timeObject

Returns the value of attribute end_time.



3
4
5
# File 'lib/google_calendar_oauth2/event.rb', line 3

def end_time
  @end_time
end

#etagObject

Returns the value of attribute etag.



3
4
5
# File 'lib/google_calendar_oauth2/event.rb', line 3

def etag
  @etag
end

Returns the value of attribute html_link.



3
4
5
# File 'lib/google_calendar_oauth2/event.rb', line 3

def html_link
  @html_link
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/google_calendar_oauth2/event.rb', line 3

def id
  @id
end

#sequenceObject

Returns the value of attribute sequence.



3
4
5
# File 'lib/google_calendar_oauth2/event.rb', line 3

def sequence
  @sequence
end

#start_timeObject

Returns the value of attribute start_time.



3
4
5
# File 'lib/google_calendar_oauth2/event.rb', line 3

def start_time
  @start_time
end

#statusObject

Returns the value of attribute status.



3
4
5
# File 'lib/google_calendar_oauth2/event.rb', line 3

def status
  @status
end

#summaryObject

Returns the value of attribute summary.



3
4
5
# File 'lib/google_calendar_oauth2/event.rb', line 3

def summary
  @summary
end

#updated_atObject

Returns the value of attribute updated_at.



3
4
5
# File 'lib/google_calendar_oauth2/event.rb', line 3

def updated_at
  @updated_at
end

Class Method Details

.create(calendar_id, attrs) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/google_calendar_oauth2/event.rb', line 75

def self.create(calendar_id, attrs)
  new connection.execute(
    api_method: client.events.insert, 
    parameters: { 'calendarId' => calendar_id }, 
    body: [JSON.dump(attrs)], 
    headers: {'Content-Type' => 'application/json'}
  ).data.to_hash.merge 'calendar_id' => calendar_id
end

.delete(calendar_id, event_id) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/google_calendar_oauth2/event.rb', line 100

def self.delete(calendar_id, event_id)
  connection.execute(
    api_method: client.events.delete, 
    parameters: { 
      'calendarId' => calendar_id, 
      'eventId' => event_id 
    }
  )
end

.find_by_id(calendar_id, id) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/google_calendar_oauth2/event.rb', line 64

def self.find_by_id(calendar_id, id)
  event = connection.execute(
    api_method: client.events.get, 
    parameters: { 
      'calendarId' => calendar_id, 
      'eventId' => id 
    }
  )
  new event.data.to_hash.merge 'calendar_id' => calendar_id
end

.find_by_name(calendar_id, query) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/google_calendar_oauth2/event.rb', line 55

def self.find_by_name(calendar_id, query)
  list(calendar_id).each do |event|
    if event.summary == query
      return @event = new(event.merge({ 'calendar_id' => calendar_id }))
    end
  end
  @event
end

.list(calendar_id) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/google_calendar_oauth2/event.rb', line 46

def self.list(calendar_id)
  list = connection.execute(api_method: client.events.list, parameters: { 'calendarId' => calendar_id })
  events = []
  list.data.items.each do |event|
    events << new(event)
  end
  events
end

Instance Method Details

#attributesObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/google_calendar_oauth2/event.rb', line 27

def attributes
  {
    id: id,
    etag: etag,
    status: status,
    html_link: html_link,
    created_at: created_at,
    updated_at: updated_at,
    calendar_id: calendar_id,
    sequence: sequence,
    start: {
      :dateTime => start_time
    },
    end: { 
      :dateTime => end_time
    }
  }
end

#to_sObject



23
24
25
# File 'lib/google_calendar_oauth2/event.rb', line 23

def to_s
"#&lt;Event id: #{self.id}, start_time: #{self.start_time}, end_time: #{self.end_time}, calendar_id: #{self.calendar_id}, sequence: #{self.sequence}, etag: #{self.etag}, status: #{self.status}, html_link: #{self.html_link}, created_at: #{self.created_at}, updated_at: #{self.updated_at}&gt;"
end

#update(attrs = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/google_calendar_oauth2/event.rb', line 84

def update(attrs = {})
  self.sequence = self.sequence.nil? ? 1 : self.sequence + 1
  attrs = self.attributes.merge(attrs)
  result = Event.connection.execute( 
    api_method: Event.client.events.update, 
    parameters: { 
      'calendarId' => self.calendar_id, 
      'eventId' => self.id 
    }, 
    body: [JSON.dump(attrs)], 
    headers: {'Content-Type' => 'application/json'}
  ).data.to_hash.merge('calendar_id' => self.calendar_id)
  self.attributes = result
  self
end