Sha256: c2b47606252d03cbd1e87f6e17b7d4da80fcb4922fb14b11df59b1d7eb22758b

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require "date"
require "rack"
require "rack/a_day_without/version"

module Rack
  class ADayWithout

    def self.const_missing const_name
      const_set const_name, self.new_subject_subclass
    end

    def self.new_subject_subclass
      Class.new(self) do
        def initialize app, options = {}
          subject = self.class.name.split('::').last
          super app, subject, options
        end
      end
    end

    def initialize app, subject, options = {}
      @app = app
      @subject = subject
      @content = options[:content]
      @file = options[:file]
      @date = parse_date options[:on]
      @allowed = parse_allowed_routes options[:bypass]
    end

    def call env
      allowed = allowed_path? env['PATH_INFO']
      if @date == Date.today && !allowed
        res = Response.new
        res["X-Day-Without"] = @subject
        res.write content
        res.finish
      else
        @app.call env
      end
    end

    private

    def content
      if @file
        ::File.read @file
      else
        @content.to_s
      end
    end

    def parse_allowed_routes allowed
      if allowed.nil?
        []
      elsif allowed.respond_to? :to_ary
        allowed.to_ary || [allowed]
      else
        [allowed]
      end
    end

    def parse_date dateish
      Date.parse dateish.to_s
    end

    def allowed_path? path
      @allowed.any? do |a|
        a.is_a?(Regexp) ? a.match(path.to_s) : a == path.to_s
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-a_day_without-0.0.1 lib/rack/a_day_without.rb