Class: CalendarAssistant::EventRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/calendar_assistant/event_repository.rb

Direct Known Subclasses

LintEventRepository, LocationEventRepository

Defined Under Namespace

Classes: CalendarNotFoundException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, calendar_id, config: CalendarAssistant::Config.new) ⇒ EventRepository

Returns a new instance of EventRepository.



8
9
10
11
12
13
14
15
16
# File 'lib/calendar_assistant/event_repository.rb', line 8

def initialize(service, calendar_id, config: CalendarAssistant::Config.new)
  @service = service
  @config = config
  @calendar_id = calendar_id
  @calendar = @service.get_calendar @calendar_id
rescue Google::Apis::ClientError => e
  raise CalendarNotFoundException, "Calendar for #{@calendar_id} not found" if e.status_code == 404
  raise
end

Instance Attribute Details

#calendarObject (readonly)

Returns the value of attribute calendar.



6
7
8
# File 'lib/calendar_assistant/event_repository.rb', line 6

def calendar
  @calendar
end

#calendar_idObject (readonly)

Returns the value of attribute calendar_id.



6
7
8
# File 'lib/calendar_assistant/event_repository.rb', line 6

def calendar_id
  @calendar_id
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/calendar_assistant/event_repository.rb', line 6

def config
  @config
end

Instance Method Details

#create(event_attributes) ⇒ Object



43
44
45
46
47
# File 'lib/calendar_assistant/event_repository.rb', line 43

def create(event_attributes)
  new(event_attributes).tap do |event|
    @service.insert_event @calendar_id, event.__getobj__
  end
end

#delete(event) ⇒ Object



49
50
51
52
# File 'lib/calendar_assistant/event_repository.rb', line 49

def delete(event)
  @service.delete_event @calendar_id, event.id
  event
end

#find(time_range, predicates: {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/calendar_assistant/event_repository.rb', line 24

def find(time_range, predicates: {})
  events = @service.list_events(@calendar_id,
                                time_min: time_range.first.iso8601,
                                time_max: time_range.last.iso8601,
                                order_by: "startTime",
                                single_events: true,
                                max_results: 2000)
  events = events.items.map { |e| CalendarAssistant::Event.new(e, config: config) }

  events = filter_by_predicates(events, predicates) unless predicates.empty?
  CalendarAssistant::EventSet.new self, events
end

#in_tz(&block) ⇒ Object



18
19
20
21
22
# File 'lib/calendar_assistant/event_repository.rb', line 18

def in_tz(&block)
  CalendarAssistant.in_tz calendar.time_zone do
    yield
  end
end

#new(event_attributes) ⇒ Object



37
38
39
40
41
# File 'lib/calendar_assistant/event_repository.rb', line 37

def new(event_attributes)
  event = Google::Apis::CalendarV3::Event.new(**DateHelpers.cast_dates(event_attributes))
  event.visibility ||= config.event_visibility
  CalendarAssistant::Event.new(event, config: config)
end

#update(event, attributes) ⇒ Object



54
55
56
57
58
# File 'lib/calendar_assistant/event_repository.rb', line 54

def update(event, attributes)
  event.update!(**DateHelpers.cast_dates(attributes))
  updated_event = @service.update_event @calendar_id, event.id, event
  CalendarAssistant::Event.new(updated_event, config: config)
end