class Rack::ADayWithout

Constants

VERSION

Attributes

subject[RW]

Public Class Methods

const_missing(const_name) click to toggle source
# File lib/rack/a_day_without.rb, line 12
def self.const_missing const_name
  const_set const_name, self.new_subject_subclass
end
new(app, options = {}) click to toggle source
Calls superclass method
# File lib/rack/a_day_without.rb, line 18
def initialize app, options = {}
  subject = self.class.name.split('::').last
  super app, subject, options
end
new_subject_subclass() click to toggle source
# File lib/rack/a_day_without.rb, line 16
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

Public Instance Methods

allowed?(request) click to toggle source
# File lib/rack/a_day_without.rb, line 73
def allowed? request
  allowed_path? request.path_info
end
allowed_path?(path) click to toggle source
# File lib/rack/a_day_without.rb, line 77
def allowed_path? path
  allowed_paths.any? do |a|
    a.is_a?(Regexp) ? a.match(path.to_s) : a == path.to_s
  end
end
allowed_paths() click to toggle source
# File lib/rack/a_day_without.rb, line 57
def allowed_paths
  @allowed ||= parse_allowed_routes @options[:bypass]
end
call(env) click to toggle source
# File lib/rack/a_day_without.rb, line 34
def call env
  request = Request.new env

  return @app.call env unless request.get? && !request.xhr?

  if disabling_query? request
    disable! request
    @app.call env
  elsif !disabled?(request) && date == today && !allowed?(request)
    block request
  else
    @app.call env
  end
end
content() click to toggle source
# File lib/rack/a_day_without.rb, line 65
def content
  if @options[:file]
    ::File.read @options[:file]
  else
    @options[:content].to_s
  end
end
date() click to toggle source
# File lib/rack/a_day_without.rb, line 53
def date
  @date ||= parse_date @options[:on]
end
disable!(request) click to toggle source
# File lib/rack/a_day_without.rb, line 88
def disable! request
  if defined?(ActionDispatch::Request)
    request = ActionDispatch::Request.new(request.env)
    request.cookie_jar[:day_with] = { value: true, path: '/' }
  end
end
disabled?(request) click to toggle source
# File lib/rack/a_day_without.rb, line 95
def disabled? request
  if defined?(ActionDispatch::Request)
    request = ActionDispatch::Request.new(request.env)
    request.cookie_jar[:day_with] || false
  else
    false
  end
end
disabling_query?(request) click to toggle source
# File lib/rack/a_day_without.rb, line 83
def disabling_query? request
  key = @options[:disabled_on]
  key != false && request.params.keys.include?(key)
end
timezone() click to toggle source
# File lib/rack/a_day_without.rb, line 49
def timezone
  @timezone ||= parse_timezone @options[:timezone]
end
today() click to toggle source
# File lib/rack/a_day_without.rb, line 61
def today
  timezone.now.to_date
end

Private Instance Methods

block(request) click to toggle source
# File lib/rack/a_day_without.rb, line 118
def block request
  Response.new do |r|
    r["X-Day-Without"] = subject
    r.write content
  end.finish
end
parse_allowed_routes(allowed) click to toggle source
# File lib/rack/a_day_without.rb, line 125
def parse_allowed_routes allowed
  if allowed.nil?
    []
  elsif allowed.respond_to? :to_ary
    allowed.to_ary || [allowed]
  else
    [allowed]
  end
end
parse_date(dateish) click to toggle source
# File lib/rack/a_day_without.rb, line 135
def parse_date dateish
  Date.parse dateish.to_s
end
parse_timezone(timezone) click to toggle source
# File lib/rack/a_day_without.rb, line 139
def parse_timezone timezone
  TZInfo::Timezone.get timezone
end
redirect(request) click to toggle source
# File lib/rack/a_day_without.rb, line 106
def redirect request
  params = request.params
  params.delete @options[:disabled_on]
  query = URI.encode params.map {|k,v| "#{k}=#{v}"}.join("&")
  location = [request.scheme, '://', request.host, request.path].join
  location << "?#{query}" unless query.empty?

  Response.new do |r|
    r.redirect location
  end.finish
end