module JSONiCal module VEVENTRepo class << self def find(orn) database[:vevents].where(orn: orn).first end def insert(vevent) raise ArgumentError unless vevent.is_a?(JSONiCal::VEVENTModel) unless database[:vcalendars].where(orn: vevent.calendar_orn).first database[:vcalendars].insert( [:orn, :token], [vevent.calendar_orn, SecureRandom.hex(30)] ) end database[:vevents].insert_conflict( target: :orn, update: vevent.to_h, update_where: Sequel.qualify(:vevents, :updated) < vevent.updated ).insert(vevent.to_h) end def delete(vevent) raise ArgumentError unless vevent.is_a?(JSONiCal::VEVENTModel) database[:vevents].where(orn: vevent.orn).delete end def events_with_token(token) # The 7 days rules is tempory and is here to fix a problem with google # calendar not syncing more than a certain number of events. database[:vevents]. where(calendar_orn: calendar_with_token(token).select_map(:orn)). where { begin_date > Time.now - 604800 }. # > 7 days ago order(:begin_date) end private def calendar_with_token(token) database[:vcalendars].where(token: token) end def database JSONiCal.database end end end end