Skip to content

Loading…

Fixed commits; Added 2 tests #5

Open
wants to merge 4 commits into from

1 participant

@dcolebatch

Hey Simone,

Sorry about the last commits. Take a look at these if you have time.

Also, note: "rake manifest" deletes your test fixtures... I hope that's desired? I ran it mainly to remove all your NetBeans files that were kickin' around in there.

Rgds,
David

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 7, 2011
  1. @dcolebatch
Commits on Jan 9, 2011
  1. @dcolebatch

    Added ApacheLogRegex.foreach(), works like FasterCSV.foreach()

    dcolebatch committed
    Made format optional in initialize(), default to NCSA Extended format
  2. @dcolebatch
  3. @dcolebatch
Showing with 37 additions and 13 deletions.
  1. +6 −10 Manifest
  2. +21 −3 lib/apache_log_regex.rb
  3. +10 −0 test/apache_log_regex_test.rb
View
16 Manifest
@@ -1,16 +1,12 @@
CHANGELOG.rdoc
-lib/apache_log_regex/version.rb
-lib/apache_log_regex.rb
LICENSE.rdoc
-nbproject/private/config.properties
-nbproject/private/private.properties
-nbproject/private/private.xml
-nbproject/project.properties
-nbproject/project.xml
-Rakefile
README.rdoc
+Rakefile
+lib/apache_log_regex.rb
+lib/apache_log_regex/version.rb
+lib/apachelogregex.rb
setup.rb
-test/helper.rb
+test/apache_log_regex_test.rb
test/test_all.rb
-test/unit/apache_log_regex_test.rb
+test/test_helper.rb
Manifest
View
24 lib/apache_log_regex.rb
@@ -66,7 +66,11 @@ class ApacheLogRegex
GEM = 'apachelogregex'
AUTHOR = 'Simone Carletti <weppos@weppos.net>'
-
+ NCSA_EXTENDED = '%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"'
+ COMBINED = NCSA_EXTENDED
+ CLF = '%h %l %u %t \"%r\" %>s %b'
+ CLF_VH = '%v %h %l %u %t \"%r\" %>s %b'
+
#
# = ParseError
#
@@ -95,12 +99,26 @@ class ParseError < RuntimeError; end
# The list of field names that extracted from log format.
attr_reader :names
+ class << self
+ def foreach(file_name, format = nil)
+ parser = ApacheLogRegex.new(format)
+ File.open(file_name, 'r') do |file|
+ while line = file.gets
+ yield parser.parse! line
+ end
+ end
+ end
+ end
# Initializes a new parser instance with given log <tt>format</tt>.
- def initialize(format)
+ def initialize(format = nil)
@regexp = nil
@names = []
- @format = parse_format(format)
+ if format.nil?
+ @format = parse_format(ApacheLogRegex::NCSA_EXTENDED)
+ else
+ @format = parse_format(format)
+ end
end
# Parses <tt>line</tt> according to current log <tt>format</tt>
View
10 test/apache_log_regex_test.rb
@@ -94,7 +94,17 @@ def test_stricparse_line_should_raise_on_invalid_format
assert_match(/Invalid format/, error.message)
end
+ def test_foreach_without_format
+ results = []
+ ApacheLogRegex.foreach("#{TESTCASES_PATH}/log.log"){ |r| results << r['%h'] }
+ assert_equal(%w(87.18.183.252 79.28.16.191 79.28.16.191 69.150.40.169 217.220.110.75), results)
+ end
+ def test_foreach_with_format
+ results = []
+ ApacheLogRegex.foreach("#{TESTCASES_PATH}/log.log", @format){ |r| results << r['%h'] }
+ assert_equal(%w(87.18.183.252 79.28.16.191 79.28.16.191 69.150.40.169 217.220.110.75), results)
+ end
protected
def read_testcase(filename)
Something went wrong with that request. Please try again.