class Rack::Olark
Constants
- TEMPLATE_PATH
- VERSION
Public Class Methods
new(app, options = {})
click to toggle source
# File lib/rack/olark.rb, line 13 def initialize(app, options = {}) @app = app # Validity check on Site-ID site_id = (options.delete(:id) || '').gsub(/[^0-9-]/, '') unless site_id.length == 16 raise ArgumentError, 'rack-olark requires a valid Olark Site-ID!' end # Deprecation warnings deprecation_warning('format') if options.delete(:format) deprecation_warning('tag') if options.delete(:tag) # Is it a Regexp? No? Then escape it, and make it a Regexp. @paths = (options.delete(:paths) || []).map do |path| path.is_a?(Regexp) ? path : /^#{Regexp.escape(path.to_s)}$/ end # Let's please not call Array#empty? on every request. @inject_all = @paths.empty? js = options.map { |k, v| olarkify(k, v) }.join @html = ERB.new(::File.read(TEMPLATE_PATH)).result(binding) end
Public Instance Methods
call(env)
click to toggle source
# File lib/rack/olark.rb, line 37 def call(env) status, headers, body = @app.call(env) request = Rack::Request.new(env) if html?(headers) && (@inject_all || should_inject?(request)) response = Rack::Response.new([], status, headers) body.each do |fragment| response.write(fragment.gsub('</body>', @html)) end response.finish else [status, headers, body] end end
Private Instance Methods
deprecation_warning(option)
click to toggle source
# File lib/rack/olark.rb, line 57 def deprecation_warning(option) STDOUT.puts("Rack::Olark: The '#{option}' option is deprecated and no longer functions! See README.md for details.") end
html?(headers)
click to toggle source
# File lib/rack/olark.rb, line 61 def html?(headers) (Rack::Utils::HeaderHash.new(headers)['Content-Type'] || '').include?('html') end
olarkify(key, val)
click to toggle source
# File lib/rack/olark.rb, line 53 def olarkify(key, val) "olark.configure(#{key.to_s.to_json}, #{val.to_json});" end
should_inject?(request)
click to toggle source
# File lib/rack/olark.rb, line 65 def should_inject?(request) @paths.map { |p| request.path_info =~ p }.include?(true) end