From 9776c1cfefef4db2d9c67ec359dd29eced70da05 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sat, 24 Aug 2013 17:25:19 -0700 Subject: [PATCH] support config file in Ruby client --- ruby/Gemfile | 2 ++ ruby/Gemfile.lock | 8 ++++++++ ruby/kwikemon.rb | 19 ++++++++++++++++++- ruby/monitor.rb | 2 +- ruby/version.rb | 2 +- 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/ruby/Gemfile b/ruby/Gemfile index d2c7f0b..32e3c87 100644 --- a/ruby/Gemfile +++ b/ruby/Gemfile @@ -1,3 +1,5 @@ source 'https://rubygems.org' +gem 'hashie', '~> 2.0.5' gem 'redis', '~> 3.0.4' +gem 'toml', '~> 0.0.4' diff --git a/ruby/Gemfile.lock b/ruby/Gemfile.lock index ce773d3..48cbebd 100644 --- a/ruby/Gemfile.lock +++ b/ruby/Gemfile.lock @@ -1,10 +1,18 @@ GEM remote: https://rubygems.org/ specs: + blankslate (2.1.2.4) + hashie (2.0.5) + parslet (1.5.0) + blankslate (~> 2.0) redis (3.0.4) + toml (0.0.4) + parslet PLATFORMS ruby DEPENDENCIES + hashie (~> 2.0.5) redis (~> 3.0.4) + toml (~> 0.0.4) diff --git a/ruby/kwikemon.rb b/ruby/kwikemon.rb index 4c1609f..ab2c92e 100644 --- a/ruby/kwikemon.rb +++ b/ruby/kwikemon.rb @@ -3,7 +3,9 @@ # MIT License # http://sjs.mit-license.org +require 'hashie' require 'redis' +require 'toml' require File.expand_path('../monitor.rb', __FILE__) module Kwikemon @@ -13,7 +15,7 @@ module Kwikemon include Enumerable def redis - @redis ||= Redis.new + @redis ||= Redis.new(config.redis) end def redis=(redis) @@ -110,4 +112,19 @@ module Kwikemon end end +private + + def config + @config ||= Hashie::Mash.new(load_config) + end + + def load_config + path = File.join(ENV['HOME'], '.kwikemon.toml') + if File.exists?(path) + TOML.load_file(path) + else + {} + end + end + end diff --git a/ruby/monitor.rb b/ruby/monitor.rb index f93bbd7..ba7e339 100644 --- a/ruby/monitor.rb +++ b/ruby/monitor.rb @@ -70,7 +70,7 @@ module Kwikemon end def text - @text ||= exists? ? redis.hget(key, 'name') + @text ||= exists? ? redis.hget(key, 'name') : nil end diff --git a/ruby/version.rb b/ruby/version.rb index dedb86a..f4da415 100644 --- a/ruby/version.rb +++ b/ruby/version.rb @@ -1,3 +1,3 @@ module Kwikemon - VERSION = '0.0.1' + VERSION = '0.0.2' end