Class: Rack::UserAgent::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/useragent/filter.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, required_browsers = [], options = {}) ⇒ Filter

Returns a new instance of Filter.


12
13
14
15
16
17
18
# File 'lib/rack/useragent/filter.rb', line 12

def initialize(app, required_browsers = [], options = {})
  @app                = app
  @browsers           = required_browsers.map{ |b| OpenStruct.new(:browser => b[0], :version => b[1]) }
  @template           = options[:template]
  @force_with_cookie  = options[:force_with_cookie]
  @custom_message     = options[:custom_message]
end

Instance Method Details

#call(env) ⇒ Object


20
21
22
23
24
25
26
27
28
29
# File 'lib/rack/useragent/filter.rb', line 20

def call(env)
  request = Rack::Request.new(env)
  useragent = ::UserAgent.parse(env["HTTP_USER_AGENT"].to_s)
  if useragent.any? && !detection_disabled_by_cookie?(request.cookies) && unsupported?(useragent)
    content = render_page(useragent)
    [200, {"Content-Type" => "text/html", "Content-Length" => content.length.to_s}, content]
  else
    @app.call(env)
  end
end