Class: Rack::Modernizr

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-modernizr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Modernizr

Returns a new instance of Modernizr.



7
8
9
10
11
12
# File 'lib/rack-modernizr.rb', line 7

def initialize(app, options = {})
  @app, @options = app, options
  @options[:modernizr_js_url] ||= "http://cachedcommons.org/cache/modernizr/1.5.0/javascripts/modernizr-min.js"
  @options[:key_name] ||= "Modernizr"
  @options[:storage] ||= "cookie"
end

Instance Attribute Details

#stashObject

Returns the value of attribute stash.



5
6
7
# File 'lib/rack-modernizr.rb', line 5

def stash
  @stash
end

Instance Method Details

#add_modernizr(response, body = "") ⇒ Object



86
87
88
89
# File 'lib/rack-modernizr.rb', line 86

def add_modernizr(response, body = "")
  response.each{ |s| body << s.to_s }
  [body.gsub(/\<\/body\>/, "#{modernizr_js}</body>")]
end

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rack-modernizr.rb', line 14

def call(env)
  @req = Rack::Request.new(env)

  load_modernizr_from_storage if have_modernizr?

  if env['PATH_INFO'] == "/rack-modernizr-endpoint.gif"
    return persist_modernizr
  end

  status, headers, response = @app.call(env)

  if should_add_modernizr?(status, headers, response)
    response = add_modernizr response 
    fix_content_length(headers, response)
  end


  [status, headers, response]
end

#fix_content_length(headers, response) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/rack-modernizr.rb', line 78

def fix_content_length(headers, response)
  # Set new Content-Length, if it was set before we mutated the response body
  if headers['Content-Length']
    length = response.to_ary.inject(0) { |len, part| len + bytesize(part) }
    headers['Content-Length'] = length.to_s
  end
end

#have_modernizr?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rack-modernizr.rb', line 34

def have_modernizr?
  !storage[@options[:key_name]].nil?
end

#load_modernizr_from_storageObject



38
39
40
41
# File 'lib/rack-modernizr.rb', line 38

def load_modernizr_from_storage
  puts "load_modernizr_from_storage"
  @req.env['X-rack-modernizr'] = Rack::Utils.parse_nested_query(storage[@options[:key_name]])
end

#modernizr_jsObject



91
92
93
94
# File 'lib/rack-modernizr.rb', line 91

def modernizr_js
  "<div id='modernizr_img'></div><script src=\"#{@options[:modernizr_js_url]}\" type=\"text/javascript\"></script>" +
  "<script type='text/javascript'>#{set_modernizr_js}</script>"
end

#persist_modernizrObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rack-modernizr.rb', line 50

def persist_modernizr
  response = Rack::Response.new ["OK"], 200, {}

  s = @req.env['QUERY_STRING']

  case @options[:storage]
  when "session"
    @req.env["rack.session"][@options[:key_name]] = s
  when "cookies"
  when "cookie"
    response.set_cookie(@options[:key_name], {:value => s, :path => "/"})
  end

  response.finish # finish writes out the response in the expected format.
end

#set_modernizr_jsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rack-modernizr.rb', line 96

def set_modernizr_js
  "var m=Modernizr,c='';" +
  "for(var f in m){" +
  "if(f[0]=='_'){continue;}" +
  "var t=typeof m[f];" +
  "if(t=='function'){continue;}" +
  "if(t=='object'){" +
  "for(var s in m[f]){" +
  "c+='&'+f+'['+s+']='+(m[f][s]?'1':'0');" +
  "}" +
  "}else{" +
  "c+='&'+f+'='+(m[f]?'1':'0');" +
  "}" +
  "}" +
  "document.getElementById('modernizr_img').innerHTML = \"<img src='/rack-modernizr-endpoint.gif?\"+c+\"'></img>\";"
end

#should_add_modernizr?(status, headers, response) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/rack-modernizr.rb', line 43

def should_add_modernizr?(status, headers, response)
  !storage.has_key?( @options[:key_name] ) &&
  status==200 &&
  headers["Content-Type"] && 
  headers["Content-Type"].include?("text/html")
end

#storageObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rack-modernizr.rb', line 66

def storage
  case @options[:storage]
  when "session"
    @req.env["rack.session"] ||= {}
    @req.env["rack.session"]
  when "cookies"
  when "cookie"
    @req.cookies ||= {}
    @req.cookies
  end
end