apache_log-parser 3.1.1 → 3.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 059d6f42a9d78d8cfdb3b4245dc5bb7038b52e6b
4
- data.tar.gz: 9843f3196f886414b7c7ee784669b0b26b367c6e
3
+ metadata.gz: 79f05f655abf68de378abfbc46727b3ffb63c814
4
+ data.tar.gz: 881391956529b0d466b71b2ead3118a9586da5b2
5
5
  SHA512:
6
- metadata.gz: 0e54be8c7eee30b4d7abdc5fea023f9c88cd8917d2ff472ea74d6214bf8919f8c68a3915d8eb561de3e1d09da4300f1dd63fdd90e7a6883024f02308e42223d0
7
- data.tar.gz: 577b92735e5c908b20953bfc2729777489b5f58e72517054d39674498926bf630f37559d1c595f126b4b3eba9ce467515de0f3a17c2dfe9ff3399de039873786
6
+ metadata.gz: 02cc9b7d20474c3b0d3049e36c7f1e8e4f6e72374457e002132cc52156bf22f3a062c18d780131aa021ab877c2779f433e119ffa269d89fcc9b0a4019b2a35df
7
+ data.tar.gz: e5b3c7f417621295ef82346f919d900f5ef56b54e49faede00373bb21bea80462790145b53b040627e43ce1b9abb1cfa23266a01775c583b63473395732b2978
@@ -3,25 +3,24 @@ require 'date'
3
3
 
4
4
  module ApacheLog
5
5
  class Parser
6
- def initialize(format, additional_fields=[])
7
- common_fields = %w(remote_host identity_check user datetime request status size)
8
- combined_fields = common_fields + %w(referer user_agent)
6
+ COMMON_FIELDS = %w(remote_host identity_check user datetime request status size)
7
+ COMBINED_FIELDS = COMMON_FIELDS + %w(referer user_agent)
9
8
 
10
- common_pattern = '(?:^|\s)((?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(?:[\w:]+?))\s+(\S+)\s+(\S+)\s+\[(\d{2}\/.*\d{4}:\d{2}:\d{2}:\d{2}\s.*)\]\s+"(.*?)"\s+(\S+)\s+(\S+)'
11
- combined_pattern = common_pattern + '\s+"(.*?[^\\\\])"\s+"(.*?[^\\\\])"'
12
- additional_pattern = ''
9
+ COMMON_PATTERN = '(?:^|\s)((?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(?:[\w:]+?))\s+(\S+)\s+(\S+)\s+\[(\d{2}\/.*\d{4}:\d{2}:\d{2}:\d{2}\s.*)\]\s+"(.*?)"\s+(\S+)\s+(\S+)'
10
+ COMBINED_PATTERN = COMMON_PATTERN + '\s+"(.*?[^\\\\])"\s+"(.*?[^\\\\])"'
11
+ ADDITIONAL_PATTERN = '\s+"?([^"]*)"?'
13
12
 
14
- additional_fields.each do
15
- additional_pattern += '\s+"?([^"]*)"?'
16
- end
13
+ def initialize(format, additional_fields=[])
14
+ additional_pattern = ''
15
+ additional_pattern << ADDITIONAL_PATTERN * additional_fields.size
17
16
 
18
17
  case format
19
18
  when 'common'
20
- @fields = common_fields + additional_fields
21
- @pattern = /#{common_pattern}#{additional_pattern}/
19
+ @fields = COMMON_FIELDS + additional_fields
20
+ @pattern = /#{COMMON_PATTERN}#{additional_pattern}/
22
21
  when 'combined'
23
- @fields = combined_fields + additional_fields
24
- @pattern = /#{combined_pattern}#{additional_pattern}/
22
+ @fields = COMBINED_FIELDS + additional_fields
23
+ @pattern = /#{COMBINED_PATTERN}#{additional_pattern}/
25
24
  else
26
25
  raise "format error\n no such format: <#{format}> \n"
27
26
  end
@@ -29,28 +28,27 @@ module ApacheLog
29
28
 
30
29
  def parse(line)
31
30
  matched = @pattern.match(line)
31
+
32
32
  raise "parse error\n at line: <#{line}> \n" if matched.nil?
33
+
33
34
  generate_hash(@fields, matched.to_a)
34
35
  end
35
36
 
36
37
  private
37
38
 
38
39
  def generate_hash(keys, values)
39
- hash = {}
40
-
41
- keys.each.with_index(1) do |key, idx|
40
+ keys.each.with_index(1).each_with_object({}) do |(key, i), hash|
42
41
  key = key.to_sym
42
+
43
43
  case key
44
44
  when :datetime
45
- hash[key] = to_datetime(values[idx])
45
+ hash[key] = to_datetime(values[i])
46
46
  when :request
47
- hash[key] = parse_request(values[idx])
47
+ hash[key] = parse_request(values[i])
48
48
  else
49
- hash[key] = values[idx]
49
+ hash[key] = values[i]
50
50
  end
51
51
  end
52
-
53
- hash
54
52
  end
55
53
 
56
54
  def to_datetime(str)
@@ -59,11 +57,8 @@ module ApacheLog
59
57
 
60
58
  def parse_request(str)
61
59
  method, path, protocol = str.split
62
- {
63
- method: method,
64
- path: path,
65
- protocol: protocol,
66
- }
60
+
61
+ { method: method, path: path, protocol: protocol }
67
62
  end
68
63
  end
69
64
  end
@@ -1,5 +1,5 @@
1
1
  module ApacheLog
2
2
  class Parser
3
- VERSION = "3.1.1"
3
+ VERSION = "3.1.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apache_log-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.1
4
+ version: 3.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuichi Takada
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-18 00:00:00.000000000 Z
11
+ date: 2015-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler