Sha256: 174555b5275f19caa29d493236266c2532182a3bc0b6d7075d6950316c9fe40f

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

require 'rack'
require 'rack/request'
require 'haml'

module Rack
  class Olark
    Defaults = {:format => :html5, :paths => []}

    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, @paths = [@options.delete(:id), @options.delete(:format), @options.delete(:paths)]
      @paths = [] unless @paths.class.eql? Array
      @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? and (@paths.empty? or @paths.include? Rack::Request.new(env).path_info)
      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-olark-0.0.3 lib/rack/olark.rb