Sha256: f30bf08edf28e94f42073ffd28e53a6af535dd3f8f67a304c2fdb51f7cf55e78

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

module CalendariumRomanum
  module Remote
    # Mostly API-compatible with CalendariumRomanum::Calendar
    # (only constructor differs).
    # Instead of computing calendar data, obtains them
    # from a remote calendar API
    # https://github.com/igneus/church-calendar-api
    class Calendar
      extend Forwardable

      def initialize(year, calendar_uri, api_version: :v0, driver: :net_http)
        @year = year
        @calendar_uri = calendar_uri
        @driver =
          if driver.is_a? Symbol
            # built-in driver specified by a Symbol
            Drivers.get(api_version, driver)
          else
            # driver instance
            driver
          end
        @deserializer = V0::Deserializer.new

        # only for most fundamental computations made locally
        @temporale = Temporale.new(year)
        # only for API compatibility
        @sanctorale = nil
      end

      attr_reader :year
      attr_reader :temporale
      attr_reader :sanctorale
      attr_reader :calendar_uri

      def_delegators :@temporale, :range_check, :season

      def day(*args)
        # TODO code copied from CalendariumRomanum::Calendar -
        # extract to a separate method
        if args.size == 2
          date = Date.new(@year, *args)
          unless @temporale.date_range.include? date
            date = Date.new(@year + 1, *args)
          end
        else
          date = CalendariumRomanum::Calendar.mk_date *args
          #range_check date
        end

        serialized = @driver.get date, @calendar_uri
        @deserializer.call serialized
      end

      def lectionary
        year_spec['lectionary'].to_sym
      end

      def ferial_lectionary
        year_spec['ferial_lectionary'].to_i
      end

      def ==(obj)
        self.class == obj.class &&
          self.year == obj.year &&
          self.calendar_uri == obj.calendar_uri
      end

      private

      def year_spec
        @year_spec ||= JSON.parse(@driver.year(@year, @calendar_uri))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
calendarium-romanum-remote-0.1.0 lib/calendarium-romanum/remote/calendar.rb