Class: Rack::Analytics::RequestParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/analytics/request_parser.rb

Constant Summary collapse

DEFAULT_KEYS =
['time', 'path', 'user_agent', 'referral']

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/rack/analytics/request_parser.rb', line 4

def data
  @data
end

Instance Method Details

#<<(field) ⇒ Object



16
17
18
19
# File 'lib/rack/analytics/request_parser.rb', line 16

def <<(field)
  @custom_fields ||= []
  @custom_fields << field
end

#except=(values) ⇒ Object



8
9
10
# File 'lib/rack/analytics/request_parser.rb', line 8

def except=(values)
  @except = values.to_a
end

#only=(values) ⇒ Object



12
13
14
# File 'lib/rack/analytics/request_parser.rb', line 12

def only=(values)
  @only = values.to_a
end

#parse(request) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rack/analytics/request_parser.rb', line 21

def parse request
  @data = {}

  @data['time'] = Time.now if to_parse.include? 'time'
  @data['path'] = request['PATH_INFO'] if to_parse.include? 'path'
  @data['user_agent'] = request['HTTP_USER_AGENT'] if to_parse.include? 'user_agent'
  @data['referral'] = request['HTTP_REFERER'] if to_parse.include? 'referral'

  @custom_fields.to_a.each do |field|
    raise TypeError unless field.respond_to? :call
    field.call(request, @data)
  end

  return self
end