Sha256: 5df8d3383a5fabe31f8a17b7870a74b6126f890146f3742a2763103daf239eab

Contents?: true

Size: 1006 Bytes

Versions: 2

Compression:

Stored size: 1006 Bytes

Contents

require 'logger'
require 'rack/request'
require 'staccato'

require 'staccato/rack/version'
require 'staccato/rack/faraday_http_adapter'
require 'staccato/rack/page_view'

module Staccato
  module Rack
    # middleware
    class Middleware
      # page view wrapper

      attr_accessor :last_hit

      def initialize(app, tracking_id, options = {})
        @app = app
        @tracking_id = tracking_id
        @logger = options[:logger]
        @default_tracker = Staccato.tracker(tracking_id) do |c|
          c.adapter = FaradayHttpAdaper.new(@logger)
        end
      end

      def call(env)
        env['staccato.pageview'] = PageView.new.tap { |p| p.logger = @logger }

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

        if (200..299).include?(status.to_i)
          @last_hit = env['staccato.pageview'].track!(@default_tracker, @tracking_id, ::Rack::Request.new(env))
        end

        # return result
        [status, headers, body]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
staccato-rack-0.4.1 lib/staccato/rack.rb
staccato-rack-0.4.0 lib/staccato/rack.rb