Class: Rack::Clicky
- Inherits:
-
Object
- Object
- Rack::Clicky
- Defined in:
- lib/rack_clicky.rb
Constant Summary collapse
- TRACKING_CODE =
<<-EOTC <script src="http://static.getclicky.com/js" type="text/javascript"></script> <script type="text/javascript">clicky.init({{CODE}});</script> <noscript><p><img alt="Clicky" width="1" height="1" src="http://in.getclicky.com/{{CODE}}ns.gif" /></p></noscript> EOT
Instance Method Summary collapse
- #_call(env) ⇒ Object
- #call(env) ⇒ Object
- #fix_content_length(headers, response) ⇒ Object
-
#initialize(app, site_id, options = {}) ⇒ Clicky
constructor
A new instance of Clicky.
- #inject_tracking(response, body = "") ⇒ Object
- #should_inject_clicky?(status, headers, response) ⇒ Boolean
Constructor Details
#initialize(app, site_id, options = {}) ⇒ Clicky
Returns a new instance of Clicky.
12 13 14 15 |
# File 'lib/rack_clicky.rb', line 12 def initialize app, site_id, ={} @app = app @site_id = site_id end |
Instance Method Details
#_call(env) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rack_clicky.rb', line 21 def _call env status, headers, response = @app.call(env) if should_inject_clicky? status, headers, response response = inject_tracking(response) fix_content_length(headers, response) end [status, headers, response] end |
#call(env) ⇒ Object
17 18 19 |
# File 'lib/rack_clicky.rb', line 17 def call env dup._call(env) end |
#fix_content_length(headers, response) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/rack_clicky.rb', line 38 def fix_content_length headers, response if headers["Content-Length"] length = response.to_ary.inject(0) { |len, part| len + Rack::Utils.bytesize(part) } headers['Content-Length'] = length.to_s end end |
#inject_tracking(response, body = "") ⇒ Object
45 46 47 48 |
# File 'lib/rack_clicky.rb', line 45 def inject_tracking response, body="" response.each { |s| body << s.to_s } [body.gsub(/<\/body>/, "#{track_code}\n</body>")] end |
#should_inject_clicky?(status, headers, response) ⇒ Boolean
32 33 34 35 36 |
# File 'lib/rack_clicky.rb', line 32 def should_inject_clicky? status, headers, response status == 200 && headers["Content-Type"] && (headers["Content-Type"].include?("text/html") || headers["Content-Type"].include?("application/xhtml")) end |