File: sample_app.rb

package info (click to toggle)
ruby-browser 5.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 872 kB
  • sloc: ruby: 4,752; makefile: 13
file content (55 lines) | stat: -rw-r--r-- 1,422 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

require "rails"
require "browser"

class PagesController < ActionController::Base
  def home
    render html: "#{browser.name}:#{browser.accept_language.first.full}"
  end
end

class PotsController < ActionController::API
  def index
    render json: {
      isBot: browser.bot?,
      acceptLanguages: browser.accept_language.map(&:full)
    }
  end
end

class SampleApp < Rails::Application
  config.secret_token = "99f19f08db7a37bdcb9d6701f54dca"
  config.secret_key_base = "99f19f08db7a37bdcb9d6701f54dca"
  config.eager_load = true
  config.active_support.deprecation = :log

  # Introduced by Rails 6.
  config.hosts << "example.org" if config.respond_to?(:hosts)

  routes.append do
    default_headers = {"Content-Type" => "text/html"}

    root to: ->(_env) { [200, default_headers, ["ROOT"]] }
    get "upgrade", to: lambda {|env|
      browser = Rack::Request.new(env).params["browser"]
      [200, default_headers, ["UPGRADE: #{browser}"]]
    }, as: "upgrade"

    get "/asset", to: proc {
      [200, {"Content-Type" => "image/png"}, []]
    }

    get "/home", to: "pages#home"

    get "/api/pages", to: "pots#index"
  end

  config.middleware.use Browser::Middleware do
    redirect_to upgrade_path(browser: "ie6") if browser.ie?(6)
    redirect_to upgrade_path(browser: "ie7") if browser.ie?(7)
    redirect_to "/invalid" if browser.ie?(8)
  end
end

SampleApp.initialize!