Class: Analytics::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/api_analytics.rb

Direct Known Subclasses

Rails, Sinatra

Instance Method Summary collapse

Constructor Details

#initialize(app, api_key, config = Config.new) ⇒ Middleware

Returns a new instance of Middleware.



10
11
12
13
14
15
16
17
18
# File 'lib/api_analytics.rb', line 10

def initialize(app, api_key, config = Config.new)
  @app = app
  @api_key = api_key
  @config = config
  @framework = "Middleware"
  @requests = []
  @last_posted = Time.now
  @mutex = Mutex.new
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/api_analytics.rb', line 20

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

  request_data = {
    hostname: @config.get_hostname.call(env),
    ip_address: get_ip_address(env),
    path: @config.get_path.call(env),
    user_agent: @config.get_user_agent.call(env),
    method: env['REQUEST_METHOD'],
    status: status,
    response_time: ((Time.now - start) * 1000).round,
    user_id: @config.get_user_id.call(env),
    created_at: Time.now.utc.iso8601
  }

  log_request(request_data)

  [status, headers, response]
end