Class: CalendarAssistant::LocalService

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

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file: nil, load_events: true) ⇒ LocalService

Returns a new instance of LocalService.



6
7
8
9
10
11
12
13
# File 'lib/calendar_assistant/local_service.rb', line 6

def initialize(file: nil, load_events: true)
  @file = file
  if (@file && File.exist?(@file) && load_events)
    @store = YAML::load_file(@file)
  else
    @store = {}
  end
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



4
5
6
# File 'lib/calendar_assistant/local_service.rb', line 4

def file
  @file
end

Instance Method Details

#delete_event(calendar_id, id) ⇒ Object



33
34
35
36
37
# File 'lib/calendar_assistant/local_service.rb', line 33

def delete_event(calendar_id, id)
  save do
    get_calendar_events(calendar_id).delete(id)
  end
end

#get_calendar(calendar_id) ⇒ Object



57
58
59
# File 'lib/calendar_assistant/local_service.rb', line 57

def get_calendar(calendar_id)
  get_calendar_and_events(calendar_id)[:calendar]
end

#get_event(calendar_id, id) ⇒ Object



45
46
47
# File 'lib/calendar_assistant/local_service.rb', line 45

def get_event(calendar_id, id)
  get_calendar_events(calendar_id)[id]
end

#insert_calendar(calendar) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/calendar_assistant/local_service.rb', line 49

def insert_calendar(calendar)
  save do
    @store[calendar.id] ||= {}
    @store[calendar.id][:calendar] = calendar
    @store[calendar.id][:events] = {}
  end
end

#insert_event(calendar_id, event) ⇒ Object



27
28
29
30
31
# File 'lib/calendar_assistant/local_service.rb', line 27

def insert_event(calendar_id, event)
  save do
    get_calendar_events(calendar_id)[event.id] = event
  end
end

#list_events(calendar_id, options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/calendar_assistant/local_service.rb', line 15

def list_events(calendar_id, options)
  time_min = Time.parse(options.fetch(:time_min))
  time_max = Time.parse(options.fetch(:time_max))
  search_range = time_min..time_max

  events = get_calendar_events(calendar_id).map do |id, event|
    event_range = (event.start.date_time || event.start.date)..(event.end.date_time || event.end.date)
    event if (event_range.first < search_range.last && event_range.last > search_range.first)
  end.compact
  Result.new(events)
end

#update_event(calendar_id, id, event) ⇒ Object



39
40
41
42
43
# File 'lib/calendar_assistant/local_service.rb', line 39

def update_event(calendar_id, id, event)
  save do
    get_calendar_events(calendar_id)[id] = event
  end
end