Sha256: 24d219ac62d52dd4815972d6dc627c2f7fb742ae1ad339e439ae08c1ab519479

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'staccato/rack/version'
require 'staccato'
require 'rack/request'

module Staccato
  module Rack
    # middleware
    class Middleware
      attr_accessor :last_hit

      def initialize(app, tracking_id)
        @app = app
        @tracker = Staccato.tracker(tracking_id)
      end

      def call(env)
        # First, call `@app`
        load_staccato_into_env(env)

        # @last_hit = nil
        status, headers, body  = @app.call(env)

        env['staccato.tracker'].track(env['staccato.pageview'].params) if (200..299).include?(status.to_i)

        # return result
        [status, headers, body]
      end

      private

      def load_staccato_into_env(env)
        request = ::Rack::Request.new(env)
        env['staccato.tracker'] = @tracker
        env['staccato.pageview'] = Staccato::Pageview.new(@tracker,
                                                          path: request.fullpath,
                                                          hostname: request.host,
                                                          user_agent: request.env['HTTP_USER_AGENT'],
                                                          user_ip: request.ip)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
staccato-rack-0.1.1 lib/staccato/rack.rb