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
14
15
16
17
18
# 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)
    payload = File.read(@file)
    @store = if YAML.respond_to?(:unsafe_load)
      YAML::unsafe_load(payload)
    else
      YAML::load(payload)
    end
  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



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

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

#get_calendar(calendar_id) ⇒ Object



62
63
64
# File 'lib/calendar_assistant/local_service.rb', line 62

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

#get_event(calendar_id, id) ⇒ Object



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

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

#insert_calendar(calendar) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/calendar_assistant/local_service.rb', line 54

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



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

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

#list_events(calendar_id, options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/calendar_assistant/local_service.rb', line 20

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



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

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