Sha256: 717afa1812cfaf0f5f8a04fd750151ffa807eb38f383fb44119090e180a5c896

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

require 'uri'
require 'net/http'
require 'json'

module Analytics
  class Middleware
    def initialize(app, api_key)
      @app = app
      @api_key = api_key
    end

    def call(env)
      start = Time.now
      status, headers, response = @app.call(env)

      data = {
        api_key: @api_key,
        hostname: env['HTTP_HOST'],
        path: env['REQUEST_PATH'],
        user_agent: env['HTTP_USER_AGENT'],
        method: env['REQUEST_METHOD'],
        status: status,
        framework: @framework,
        response_time: (Time.now - start).to_f.round,
      }

      Thread.new {
        log_request(data)
      }

      [status, headers, response]
    end

    private

    def log_request(data)
      uri = URI('https://api-analytics-server.vercel.app/api/log-request')
      res = Net::HTTP.post(uri, data.to_json)
    end
  end

  private_constant :Middleware
  
  class Rails < Middleware
    def initialize(app, api_key)
      super(app, api_key)
      @framework = "Rails"
    end
  end

  class Sinatra < Middleware
    def initialize(app, api_key)
      super(app, api_key)
      @framework = "Sinatra"
    end
  end
end



Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
api_analytics-1.0.4 lib/api_analytics.rb