Class: Censorstrike::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/censorstrike.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



15
16
17
18
# File 'lib/censorstrike.rb', line 15

def initialize(app, options = {})
  @app = app
  @options = Censorstrike::Middleware.default_options.merge(options)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



13
14
15
# File 'lib/censorstrike.rb', line 13

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/censorstrike.rb', line 13

def options
  @options
end

Class Method Details

.default_optionsObject



5
6
7
8
9
10
11
# File 'lib/censorstrike.rb', line 5

def self.default_options
  {
    :begin => Time.at(1326891600).utc,
    :end => Time.at(1326938400).utc,
    :file => File.dirname(__FILE__) + '/../message.html'
  }
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/censorstrike.rb', line 20

def call(env)
  if now > options[:begin] && now < options[:end]
    render
  else
    app.call(env)
  end
end

#nowObject



28
29
30
# File 'lib/censorstrike.rb', line 28

def now
  Time.now.utc
end

#renderObject



32
33
34
35
36
37
38
# File 'lib/censorstrike.rb', line 32

def render
  response = Rack::Response.new([], 503, 'Content-Type' => 'text/html')
  body = options[:message] ? options[:message] : File.read(options[:file])
  body.gsub!("{{footer}}", options[:footer] || "")
  response.write(body)
  response.finish
end