Permalink
Browse files

Initial commit.

  • Loading branch information...
dpoggi committed May 5, 2011
0 parents commit 07842e1d1d89521d468cb547a1ccab524e8b05d7
Showing with 118 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +1 −0 .rvmrc
  3. +3 −0 Gemfile
  4. +25 −0 LICENSE
  5. +15 −0 README.md
  6. +2 −0 Rakefile
  7. +1 −0 VERSION
  8. +41 −0 lib/rack/olark.rb
  9. +6 −0 lib/rack/templates/olark.haml
  10. +19 −0 rack-olark.gemspec
@@ -0,0 +1,5 @@
+*.DS_Store
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
1 .rvmrc
@@ -0,0 +1 @@
+rvm use --create ruby-1.9.2-p180@rack-olark
@@ -0,0 +1,3 @@
+source 'http://rubygems.org/'
+
+gemspec
25 LICENSE
@@ -0,0 +1,25 @@
+Copyright 2011 Dan Poggi. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are
+permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice, this list of
+ conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ of conditions and the following disclaimer in the documentation and/or other materials
+ provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
+CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+The views and conclusions contained in the software and documentation are those of the
+authors and should not be interpreted as representing official policies, either expressed
+or implied, of Dan Poggi.
@@ -0,0 +1,15 @@
+# Rack Olark
+Rack middleware which injects the Olark JavaScript code before the end of the body of any HTML document being sent to the client. Code from rack/google-analytics has been used liberally and expanded/trimmed down where needed. Mucho thanks to Lee Hambley and other cool rack/google-analytics bros.
+
+## Usage
+
+ require 'rack/olark'
+ use Rack::Olark, :id => '1234-567-89-0123', :options => 'like', :this => true, :orthis => 45
+
+Options become this line of JavaScript:
+ olark.configure('key', value);
+Strings are quoted for you, you needn't worry about that.
+
+## Copyright
+
+Copyright (c) 2011 Dan Poggi. License is 2-clause BSD, see LICENSE for details.
@@ -0,0 +1,2 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
@@ -0,0 +1 @@
+0.0.1
@@ -0,0 +1,41 @@
+require 'rack'
+require 'haml'
+
+module Rack
+ class Olark
+ Defaults = {:format => :html5}
+
+ def initialize(app, options = {})
+ raise ArgumentError, "Need a valid Olark ID!" unless options[:id] and options[:id].length.eql? 16
+ @app, @options = app, Defaults.merge(options)
+ @id, @format = [@options.delete(:id), @options.delete(:format)]
+ @option_js = "olark.identify('#{@id}');"
+ @options.each do |key, val|
+ @option_js << "olark.configure('#{key.to_s}', #{[String, Symbol].include? val.class ? "'#{val.to_s}'" : val.to_s});"
+ end
+ end
+
+ def call(env)
+ dup._call(env)
+ end
+
+ def _call(env)
+ @status, @headers, @response = @app.call(env)
+ return [@status, @headers, @response] unless html?
+ response = Rack::Response.new [], @status, @headers
+ @response.each {|fragment| response.write inject(fragment)}
+ response.finish
+ end
+
+ private
+
+ def html?
+ @headers['Content-Type'] =~ /html/
+ end
+
+ def inject(response)
+ @template = Haml::Engine.new(::File.read(::File.expand_path('../templates/olark.haml', __FILE__)), {:format => @format}).render self
+ response.gsub '</body>', @template + '</body>'
+ end
+ end
+end
@@ -0,0 +1,6 @@
+/ Begin Olark code
+:javascript
+ window.olark||(function(i){var e=window,h=document,a=e.location.protocol=="https:"?"https:":"http:",g=i.name,b="load";(function(){e[g]=function(){(c.s=c.s||[]).push(arguments)};var c=e[g]._={},f=i.methods.length; while(f--){(function(j){e[g][j]=function(){e[g]("call",j,arguments)}})(i.methods[f])} c.l=i.loader;c.i=arguments.callee;c.f=setTimeout(function(){if(c.f){(new Image).src=a+"//"+c.l.replace(".js",".png")+"&"+escape(e.location.href)}c.f=null},20000);c.p={0:+new Date};c.P=function(j){c.p[j]=new Date-c.p[0]};function d(){c.P(b);e[g](b)}e.addEventListener?e.addEventListener(b,d,false):e.attachEvent("on"+b,d); (function(){function l(j){j="head";return["<",j,"></",j,"><",z,' onload="var d=',B,";d.getElementsByTagName('head')[0].",y,"(d.",A,"('script')).",u,"='",a,"//",c.l,"'",'"',"></",z,">"].join("")}var z="body",s=h[z];if(!s){return setTimeout(arguments.callee,100)}c.P(1);var y="appendChild",A="createElement",u="src",r=h[A]("div"),G=r[y](h[A](g)),D=h[A]("iframe"),B="document",C="domain",q;r.style.display="none";s.insertBefore(r,s.firstChild).id=g;D.frameBorder="0";D.id=g+"-loader";if(/MSIE[ ]+6/.test(navigator.userAgent)){D.src="javascript:false"} D.allowTransparency="true";G[y](D);try{D.contentWindow[B].open()}catch(F){i[C]=h[C];q="javascript:var d="+B+".open();d.domain='"+h.domain+"';";D[u]=q+"void(0);"}try{var H=D.contentWindow[B];H.write(l());H.close()}catch(E){D[u]=q+'d.write("'+l().replace(/"/g,String.fromCharCode(92)+'"')+'");d.close();'}c.P(2)})()})()})({loader:(function(a){return "static.olark.com/jsclient/loader0.js?ts="+(a?a[1]:(+new Date))})(document.cookie.match(/olarkld=([0-9]+)/)),name:"olark",methods:["configure","extend","declare","identify"]});
+ #{@option_js}
+
+/ End Olark code
@@ -0,0 +1,19 @@
+Gem::Specification.new do |s|
+ s.name = "rack-olark"
+ s.version = ::File.read(::File.expand_path('../VERSION', __FILE__)).strip
+ s.date = ::Date.today.to_s
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Dan Poggi"]
+ s.email = ["dan.poggi@gmail.com"]
+ s.homepage = "https://github.com/dpoggi/rack-olark"
+ s.summary = %q{Rack middleware for embedding Olark.}
+ s.description = %q{Rack middleware which injects the Olark JavaScript code before the end of the body of any HTML document being sent to the client. Code from rack/google-analytics has been used liberally and expanded/trimmed down where needed. Mucho thanks to Lee Hambley and other cool rack/google-analytics bros.}
+
+ s.rubyforge_project = "rack-olark"
+
+ s.add_dependency('rack', ['>= 1.2.2'])
+ s.add_dependency('haml', ['>= 3.1.1'])
+
+ s.files = `git ls-files`.split("\n")
+ s.require_paths = ["lib"]
+end

0 comments on commit 07842e1

Please sign in to comment.