Sha256: a5fc826d271585cfc4b6b5143687a13fbd8b54041df73bbb657ff777e2e90172

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require 'json'

module Calrom
  module Formatter
    # JSON format mimicking Church Calendar API v0 (https://github.com/igneus/church-calendar-api)
    class Json
      def call(calendar, date_range)
        # We build the outer JSON Array manually in order to be able to print
        # vast amounts of calendar data without risking RAM exhaustion.
        print "["

        date_range.each_with_index do |date, i|
          day = calendar[date]
          hash = {
            date: date,
            season: day.season.symbol,
            season_week: day.season_week,
            celebrations: day.celebrations.collect do |c|
              {
                title: c.title,
                symbol: c.symbol,
                colour: c.colour.symbol,
                rank: c.rank.short_desc,
                rank_num: c.rank.priority
              }
            end,
            weekday: date.strftime('%A'),
          }

          puts "," if i > 0
          print JSON.generate hash
        end

        puts "]"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
calrom-0.3.0 lib/calrom/formatter/json.rb