class Rack::UserAgent::Filter

Public Class Methods

new(app, config = [], options = {}) click to toggle source
# File lib/rack/user_agent/filter.rb, line 8
def initialize(app, config = [], options = {})
  @app = app
  @browsers = config
  @template = options[:template]
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/user_agent/filter.rb, line 14
def call(env)
  browser = UserAgent.parse(env["HTTP_USER_AGENT"]) if env["HTTP_USER_AGENT"]
  if unsupported?(browser)
    [400, {"Content-Type" => "text/html"}, page(env['rack.locale'], browser)]
  else
    @app.call(env)
  end
end

Private Instance Methods

page(locale, browser) click to toggle source
# File lib/rack/user_agent/filter.rb, line 29
def page(locale, browser)
  return "Sorry, your browser is not supported. Please upgrade" unless template = template_file(locale)
  @browser = browser # for the template
  ERB.new(File.read(template)).result(binding)
end
template_file(locale) click to toggle source
# File lib/rack/user_agent/filter.rb, line 35
def template_file(locale)
  candidates = [ @template ]
  
  if defined?(RAILS_ROOT)
    candidates += [ File.join(RAILS_ROOT, "public", "upgrade.#{locale}.html"),
                    File.join(RAILS_ROOT, "public", "upgrade.html") ] 
  end
           
  candidates.compact.detect{ |template| File.exists?(template) }
end
unsupported?(browser) click to toggle source
# File lib/rack/user_agent/filter.rb, line 25
def unsupported?(browser)
  browser && !browser.version.nil? && @browsers.any? { |hash| browser < OpenStruct.new(hash) }
end