Class: Rack::ADayWithout

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/a_day_without.rb,
lib/rack/a_day_without/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, subject, options = {}) ⇒ ADayWithout

Returns a new instance of ADayWithout.



25
26
27
28
29
30
31
32
# File 'lib/rack/a_day_without.rb', line 25

def initialize app, subject, options = {}
  @app = app
  @subject = subject
  @options = {
    timezone: 'GMT',
    disabled_on: false
  }.merge options
end

Instance Attribute Details

#subjectObject

Returns the value of attribute subject.



10
11
12
# File 'lib/rack/a_day_without.rb', line 10

def subject
  @subject
end

Class Method Details

.const_missing(const_name) ⇒ Object



12
13
14
# 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_subject_subclassObject



16
17
18
19
20
21
22
23
# 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

Instance Method Details

#allowed?(request) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/rack/a_day_without.rb', line 73

def allowed? request
  allowed_path? request.path_info
end

#allowed_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# 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_pathsObject



57
58
59
# File 'lib/rack/a_day_without.rb', line 57

def allowed_paths
  @allowed ||= parse_allowed_routes @options[:bypass]
end

#call(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

#contentObject



65
66
67
68
69
70
71
# 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

#dateObject



53
54
55
# File 'lib/rack/a_day_without.rb', line 53

def date
  @date ||= parse_date @options[:on]
end

#disable!(request) ⇒ Object



88
89
90
91
92
93
# 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) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
# 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) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
# 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

#timezoneObject



49
50
51
# File 'lib/rack/a_day_without.rb', line 49

def timezone
  @timezone ||= parse_timezone @options[:timezone]
end

#todayObject



61
62
63
# File 'lib/rack/a_day_without.rb', line 61

def today
  timezone.now.to_date
end