Sha256: ac12c9b0263d332b8e4cfaf2c99a2d7a158dee8aab7d7576cba600288ba0cda6

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

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

module Staccato
  module Rack
    # middleware
    class Middleware
      attr_reader :tracker
      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.pageview'].track! 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.0 lib/staccato/rack.rb