faithfulgeek (owner)

Revisions

gist: 223902 Download_button fork
public
Public Clone URL: git://gist.github.com/223902.git
Embed All Files: show embed
naked_rack.rb #
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
56
57
58
59
60
61
62
63
require 'hpricot'
 
class NakedRack
 
  DEFAULT_OPTIONS = { :display_banner => true, :remove_js => false, :permanent => false }
 
  def initialize(app, options={})
    @app = app
    @options = options.reverse_merge(DEFAULT_OPTIONS)
  end
 
  def call(env)
    return @app.call(env) unless today_is_naked_css_day? or @options[:permanent]
    status, headers, response = @app.call(env)
    body = ''
    response.each do |str|
      body << remove_all_tags(str)
    end
    headers['Content-Length'] = body.length.to_s
    [status, headers, body]
  end
 
private
 
  def remove_all_tags body
    body = remove_link_tags(body).to_html
    body = remove_style_tags(body).to_html
    body = remove_js_tags(body).to_html if @options[:remove_js]
    body
  end
 
  def remove_link_tags body
    remove_tags body, "//link[@rel='stylesheet']"
  end
 
  def remove_style_tags body
    remove_tags body, "style"
  end
 
  def remove_js_tags body
    remove_tags body, "script"
  end
 
  def remove_tags body, search_str
    h_doc = Hpricot(body)
    elems = h_doc.search(search_str)
    if @options[:display_banner]
      replace_with_banner! elems
    else
      elems.remove
    end
    h_doc
  end
 
  def today_is_naked_css_day?
    Date.today.month == 4 and Date.today.day == 9
  end
 
  def replace_with_banner! h_elems
    h_elems.each { |elem| elem.swap("<!-- Happy CSS Naked Day! Brought to you by naked_rack and Joe Fiorini -->") }
  end
 
end