tail_from_sentinel 0.0.1

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.
@@ -0,0 +1 @@
1
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tail_from_sentinel.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,41 @@
1
+ TailFromSentinel
2
+
3
+ Parses a file, looking for a sentinel line that satisfies a block, and returns
4
+ all lines from there until the end of the file (including the sentinel itself).
5
+
6
+ This can be handy when you are looking for information in a log file only after
7
+ a certain condition (eg. a date) has been met.
8
+
9
+ You can either use it on it's own, or subclass it to build your own custom
10
+ tail-like classes.
11
+
12
+ Standalone:
13
+ File.open('/var/log/messages', 'r') do |file|
14
+ tfs=TailFromSentinel::Base.new(file) { |line| line =~ /Dec 20 11:2\d+/ }
15
+ end
16
+ tfs.data # => [ 'Dec 20 11:20:00 ...', 'Dec 20 11:21:00 ...', ... ]
17
+
18
+ Subclassed:
19
+ class TailMyFile < TailFromSentinel::Base
20
+ DATA_ROOT="/path/to/data/"
21
+
22
+ def initialize(data_file)
23
+ file=File.open(File.join(DATA_ROOT, data_file), 'r') do |file|
24
+ super(file, &select_sentinel_criteria(data_file))
25
+ end
26
+ end
27
+
28
+ def select_sentinel_criteria(data_file)
29
+ case data_file
30
+ when ''production.log'
31
+ Proc.new { |line| line =~ /pattern/ }
32
+ when 'development.log'
33
+ Proc.new { |line| line =~ /another pattern/ }
34
+ else
35
+ raise "Unhandled file: #{data_file}"
36
+ end
37
+ end
38
+ end
39
+
40
+ t=TailMyFile.new('production.log')
41
+ t.data # => [ 'Started GET "/pattern ...", " Processing ... ", ... ]
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,36 @@
1
+ module TailFromSentinel
2
+ # Parses a file, looking for a sentinel line that satisfies a block, and returns all lines from there
3
+ # until the end of the file (including the sentinel itself)
4
+ #
5
+ # This can be handy when you are looking for information in a log file only after a certain condition (eg. a date)
6
+ # has been seen.
7
+ class Base
8
+ attr_reader :file, :data
9
+
10
+ # Parameters:
11
+ # file: File IO object
12
+ # &sentinel: Block, taking one parameter. Should return true when the sentinel is found.
13
+ def initialize(file, &sentinel)
14
+ @file=file
15
+ @sentinel=sentinel
16
+ load_data
17
+ self
18
+ end
19
+
20
+ private
21
+ # Read all the lines of the file, but don't store them until we find one that satisfies sentinel
22
+ # Pushes them into @data
23
+ def load_data
24
+ @data=[]
25
+ marker_found=false
26
+ @file.rewind
27
+ @file.each_line do |line|
28
+ if !marker_found && @sentinel.call(line) then
29
+ marker_found=true
30
+ end
31
+
32
+ @data << line if marker_found
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module TailFromSentinel
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "tail_from_sentinel/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "tail_from_sentinel"
7
+ s.version = TailFromSentinel::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jason Stirk"]
10
+ s.email = ["jstirk@oobleyboo.com"]
11
+ s.homepage = ""
12
+ s.homepage = "http://github.com/jstirk/tail_from_sentinel"
13
+ s.summary = "Reads a file, but only return content once a marker (sentinel) has been found."
14
+ s.description = "Reads a file, but only return content once a marker (sentinel) has been found."
15
+
16
+ s.rubyforge_project = "tail_from_sentinel"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tail_from_sentinel
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jason Stirk
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-08 00:00:00 +11:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Reads a file, but only return content once a marker (sentinel) has been found.
23
+ email:
24
+ - jstirk@oobleyboo.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README
35
+ - Rakefile
36
+ - lib/tail_from_sentinel.rb
37
+ - lib/tail_from_sentinel/version.rb
38
+ - tail_from_sentinel.gemspec
39
+ has_rdoc: true
40
+ homepage: http://github.com/jstirk/tail_from_sentinel
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project: tail_from_sentinel
69
+ rubygems_version: 1.3.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Reads a file, but only return content once a marker (sentinel) has been found.
73
+ test_files: []
74
+