Class: Genius::Blocker

Inherits:
Object
  • Object
show all
Defined in:
lib/genius/blocker.rb,
lib/genius/blocker/version.rb

Constant Summary collapse

REDIRECT_SCRIPT =
<<-EOF
  <script>
    var annotated = window.location.href.indexOf("genius.it/") != -1;

    if (annotated) {
      window.location.href = window.location.href.replace("genius.it/", "")
    }
  </script>
EOF
VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Blocker

Returns a new instance of Blocker.



5
6
7
# File 'lib/genius/blocker.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/genius/blocker.rb', line 19

def call(env)
  status, headers, body = @app.call(env)
  return [status, headers, body] unless html?(headers)

  response = Rack::Response.new([], status, headers)
  body.each do |fragment|
    response.write fragment.gsub(%r{</head>}, "#{REDIRECT_SCRIPT}</head>")
  end

  response.finish
end