log_slice 0.3 → 0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/log_slice_date CHANGED
@@ -1,17 +1,25 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'date'
3
3
  require File.expand_path("../lib/log_slice", File.dirname(__FILE__))
4
- find_date = begin
5
- DateTime.parse(ARGV[0])
6
- rescue
7
- $stderr.puts "USAGE: #{$0} DATE FILEPATH\n\n"
8
- $stderr.puts "EXAMPLE: #{$0} '2012-08-31 12:00:00' /tmp/something.log\n\n"
4
+ if ARGV.length < 2
5
+ $stderr.puts "USAGE: #{$0} DATE [END_DATE] FILEPATH\n\n"
6
+ $stderr.puts "EXAMPLE: #{$0} '2012-08-31 12:00:00' /tmp/something.log\n"
7
+ $stderr.puts "EXAMPLE: #{$0} '2012-08-31 12:00:00' '2012-08-31 12:05:00' /tmp/something.log\n"
9
8
  $stderr.puts "Expects log lines to contain a DateTime string enclosed in [square brackets] \n\n"
10
- $stderr.puts "Could not parse date argument\n\n"
11
- raise
12
9
  end
13
- file = LogSlice.new(ARGV[1]).find do |line|
14
- date_string = line.match(/\[([^\]]+)\]/)[1]
15
- find_date <=> DateTime.parse(date_string)
10
+
11
+ start_date = ARGV[0]
12
+ (end_date, file) = begin
13
+ if ARGV.length == 2
14
+ [nil, ARGV[1]]
15
+ else
16
+ [ARGV[1], ARGV[2]]
17
+ end
18
+ end
19
+
20
+ begin
21
+ LogSlice::DateRange.new(file, start_date, end_date).each do |line|
22
+ puts line
23
+ end
24
+ rescue Errno::EPIPE
16
25
  end
17
- puts file.readline until file.eof?
@@ -0,0 +1,37 @@
1
+ require 'date'
2
+
3
+ class LogSlice
4
+ class DateRange
5
+
6
+ # @param file [File, String]
7
+ # @param start_date [DateTime, String]
8
+ # @param end_date [DateTime, String, nil]
9
+ def initialize file, start_date, end_date=nil
10
+ @file = file
11
+ @start_date = start_date.is_a?(DateTime) ? start_date : DateTime.parse(start_date)
12
+ @end_date = (end_date.is_a?(DateTime) || end_date.nil?) ? end_date : DateTime.parse(end_date)
13
+ end
14
+
15
+ def each
16
+ file = LogSlice.new(@file).find do |line|
17
+ date_compare @start_date, line
18
+ end
19
+ begin
20
+ line = file.readline
21
+ if @end_date.nil? || date_compare(@end_date, line) == 1
22
+ yield line
23
+ else
24
+ break
25
+ end
26
+ end until file.eof?
27
+ end
28
+
29
+ private
30
+
31
+ def date_compare date, line
32
+ date_string = line.match(/\[([^\]]+)\]/)[1]
33
+ date <=> DateTime.parse(date_string)
34
+ end
35
+
36
+ end
37
+ end
data/lib/log_slice.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require File.expand_path("log_slice/search_boundary", File.dirname(__FILE__))
2
+ require File.expand_path("log_slice/date_range", File.dirname(__FILE__))
2
3
 
3
4
  class LogSlice
4
5
 
data/log_slice.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'log_slice'
3
- s.version = '0.3'
3
+ s.version = '0.4'
4
4
  s.authors = ["Joel Plane"]
5
5
  s.email = ["joel.plane@gmail.com"]
6
6
  s.homepage = 'https://github.com/joelplane/log_slice'
data/spec/helper.rb CHANGED
@@ -4,7 +4,7 @@ RSpec.configure do |c|
4
4
  c.include(Module.new do
5
5
 
6
6
  def enumerable_to_file range
7
- file = Tempfile.new("test-#{range}")
7
+ file = Tempfile.new("test-enum")
8
8
  file.write(range.to_a.join("\n"))
9
9
  file.flush
10
10
  file.seek(0)
@@ -0,0 +1,48 @@
1
+ require File.expand_path('../lib/log_slice', File.dirname(__FILE__))
2
+ require 'helper'
3
+
4
+ describe LogSlice::DateRange do
5
+
6
+ let(:file_contents) {
7
+ %{
8
+ [2012-08-31 17:19:45] test 1
9
+ [2012-08-31 17:19:57] test 2
10
+ [2012-08-31 17:19:57] test 3
11
+ [2012-08-31 17:20:12] test 4
12
+ [2012-08-31 17:20:24] test 5
13
+ [2012-08-31 17:20:35] test 6
14
+ [2012-08-31 17:21:02] test 7
15
+ [2012-08-31 17:21:09] test 8
16
+ }.strip
17
+ }
18
+
19
+ it "works" do
20
+ start_date = '2012-08-31 17:20:00'
21
+ end_date = '2012-08-31 17:21:00'
22
+ lines = []
23
+ LogSlice::DateRange.new(string_to_file(file_contents), start_date, end_date).each do |line|
24
+ lines << line.strip
25
+ end
26
+ lines.should == [
27
+ "[2012-08-31 17:20:12] test 4",
28
+ "[2012-08-31 17:20:24] test 5",
29
+ "[2012-08-31 17:20:35] test 6"
30
+ ]
31
+ end
32
+
33
+ it "works without an end date" do
34
+ start_date = '2012-08-31 17:20:00'
35
+ lines = []
36
+ LogSlice::DateRange.new(string_to_file(file_contents), start_date, nil).each do |line|
37
+ lines << line.strip
38
+ end
39
+ lines.should == [
40
+ "[2012-08-31 17:20:12] test 4",
41
+ "[2012-08-31 17:20:24] test 5",
42
+ "[2012-08-31 17:20:35] test 6",
43
+ "[2012-08-31 17:21:02] test 7",
44
+ "[2012-08-31 17:21:09] test 8"
45
+ ]
46
+ end
47
+
48
+ end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log_slice
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
9
- version: "0.3"
8
+ - 4
9
+ version: "0.4"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Joel Plane
@@ -45,9 +45,11 @@ files:
45
45
  - README.md
46
46
  - bin/log_slice_date
47
47
  - lib/log_slice.rb
48
+ - lib/log_slice/date_range.rb
48
49
  - lib/log_slice/search_boundary.rb
49
50
  - log_slice.gemspec
50
51
  - spec/helper.rb
52
+ - spec/log_slice_date_range_spec.rb
51
53
  - spec/log_slice_spec.rb
52
54
  homepage: https://github.com/joelplane/log_slice
53
55
  licenses: []