summaryrefslogtreecommitdiff
path: root/lognotify.rb
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2010-09-28 23:29:15 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2010-09-28 23:29:15 -0700
commit933f562cdd0562398654e5c1a0352e38ba1ac459 (patch)
treeceadeeb7aff15c703f1b15188caf3baa4c5d2747 /lognotify.rb
parent7f6a33b2c73e13c3239b2d46cce49fd2977a9082 (diff)
downloadlognotify-933f562cdd0562398654e5c1a0352e38ba1ac459.tar.gz
lognotify-933f562cdd0562398654e5c1a0352e38ba1ac459.tar.xz
Less redundancy with file paths.
Diffstat (limited to 'lognotify.rb')
-rwxr-xr-xlognotify.rb26
1 files changed, 17 insertions, 9 deletions
diff --git a/lognotify.rb b/lognotify.rb
index f789d08..dc99119 100755
--- a/lognotify.rb
+++ b/lognotify.rb
@@ -25,15 +25,25 @@
require 'ftools'
+# Global settings.
CACHE_DIR="~/.cache/lognotify"
-CONFIG_DIR="~/.config/lognotify"
+CONF_DIR="~/.config/lognotify"
+
+# Methods for quickly getting file paths.
+class String
+ def to_cache_path
+ return File.expand_path(CACHE_DIR + '/' + self + '.log')
+ end
+
+ def to_conf_path
+ return File.expand_path(CONF_DIR + '/' + self + '.conf')
+ end
+end
# Configuration file parser.
def parse identifier
conf = Hash.new
- path = File.expand_path(CONFIG_DIR + '/' + identifier + '.conf')
-
- File.open(path) do |file|
+ File.open(identifier.to_conf_path) do |file|
file.each_line do |line|
# Remove whitespace from beginning of line, allowing for indentation.
line.lstrip!
@@ -55,8 +65,7 @@ end
# Count lines in cached log.
def count_lines identifier
- path = File.expand_path(CACHE_DIR + '/' + identifier + '.log')
- return File.open(path).readlines.length
+ return File.open(identifier.to_cache_path).readlines.length
end
# Retrieve new lines via SSH.
@@ -69,8 +78,7 @@ end
# Append new lines to cached log.
def append_lines identifier, lines
- path = File.expand_path(CACHE_DIR + '/' + identifier + '.log')
- file = File.open(path, 'a')
+ file = File.open(identifier.to_cache_path, 'a')
file.print lines
file.close
@@ -88,7 +96,7 @@ ARGV.each do |identifier|
conf = parse(identifier)
# Create cache file, if nonexistent.
- path = File.expand_path(CACHE_DIR + '/' + identifier + '.log')
+ path = identifier.to_cache_path
File.open(path, 'w').close unless File.exist?(path)
print '* Counting lines in cached log... '