class Rack::TdJsSdk

Constants

VERSION

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/rack/td_js_sdk.rb, line 8
def initialize(app, options = {})
  @@database  ||= options[:database]
  @@write_key ||= options[:write_key]

  raise ArgumentError, "`database` must be set!"  if database.blank?
  raise ArgumentError, "`write_key` must be set!" if write_key.blank?
  @app, @options = app, options
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/td_js_sdk.rb, line 17
def call(env)
  @status, @headers, @body = @app.call(env)
  if !html?
    return [@status, @headers, @body]
  end
  response = Rack::Response.new([], @status, @headers)
  @body.each { |fragment| response.write inject(fragment) }
  @body.close if @body.respond_to?(:close)

  response.finish
end

Private Instance Methods

html?() click to toggle source
# File lib/rack/td_js_sdk.rb, line 30
def html?; @headers['Content-Type'] =~ /html/; end
inject(fragment) click to toggle source
# File lib/rack/td_js_sdk.rb, line 32
def inject(fragment)
  @sdk_template ||= ::ERB.new ::File.read ::File.expand_path("../templates/sdk.erb", __FILE__)
  fragment.gsub!(%r{</head>}, @sdk_template.result(binding) + "</head>")

  @pageview_template ||= ::ERB.new ::File.read ::File.expand_path("../templates/pageview.erb", __FILE__)
  fragment.gsub!(%r{</body>}, @pageview_template.result(binding) + "</body>")
end