Sha256: 781d6e446931f232921d1fd7e2caa89eb4f8131b02aed2cfc020c513a7552c69

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
JSONiCal-1.0.0 lib/jsonical/vevent_repo.rb