Class: Trafficlogger::UAParse

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

Class Method Summary collapse

Class Method Details

.extract(ua_string) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/trafficlogger.rb', line 6

def extract(ua_string)
	regexes_file = File.join(Trafficlogger::Engine.root,"lib/trafficlogger/regexes.yaml")
	ua_info = YAML.load_file(regexes_file)
	data = process ua_info
	os = extract_operating_system(data[1],ua_string)
	device = extract_device(data[2],ua_string)
	user_agent = extract_user_agent(data[0],ua_string)
	[os,device,user_agent]
end

.extract_device(platform_parsers, str) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/trafficlogger.rb', line 34

def extract_device(platform_parsers,str)
	pattern, match = match_first(platform_parsers,str)
	if !match.nil?
		match.captures.first.present? ? match.captures.first : 'unidentifed'
	else
		'unidentifed'
	end
end

.extract_operating_system(os_parsers, str) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/trafficlogger.rb', line 25

def extract_operating_system(os_parsers,str)
	pattern, match = match_first(os_parsers,str)
	if !match.nil?
		match.captures.first.present? ? match.captures.first : 'unidentifed'
	else
		'unidentifed'
	end
end

.extract_user_agent(user_agent_parsers, str) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/trafficlogger.rb', line 43

def extract_user_agent(user_agent_parsers,str)
	pattern, match = match_first(user_agent_parsers,str)
	if !match.nil?
		name = match.captures.first
		if pattern["family_replacement"]
       name = pattern["family_replacement"].sub('$1', name || '')
     end
     name.present? ? name : 'unidentifed'
	else
		'unidentifed'
	end
end

.match_first(patterns, str) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/trafficlogger.rb', line 56

def match_first(patterns,str)
  patterns.each do |p|
    if m = p["regex"].match(str)
      return [p,m]
    end
  end
  nil
end

.process(ua_info) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/trafficlogger.rb', line 16

def process(ua_info)
	ua_info.each_pair do |type, patterns|
      patterns.each do |pattern|
        pattern["regex"] = Regexp.new(pattern["regex"])
      end
    end
   	[ua_info["user_agent_parsers"],ua_info["os_parsers"],ua_info["device_parsers"]]
end