mirror of
https://github.com/samsonjs/kwikemon.git
synced 2026-04-27 15:07:45 +00:00
add ruby client library
This commit is contained in:
parent
672e1d1d0f
commit
6cf290baf9
6 changed files with 244 additions and 0 deletions
3
ruby/Gemfile
Normal file
3
ruby/Gemfile
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
source 'https://rubygems.org'
|
||||||
|
|
||||||
|
gem 'redis', '~> 3.0.4'
|
||||||
10
ruby/Gemfile.lock
Normal file
10
ruby/Gemfile.lock
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
GEM
|
||||||
|
remote: https://rubygems.org/
|
||||||
|
specs:
|
||||||
|
redis (3.0.4)
|
||||||
|
|
||||||
|
PLATFORMS
|
||||||
|
ruby
|
||||||
|
|
||||||
|
DEPENDENCIES
|
||||||
|
redis (~> 3.0.4)
|
||||||
17
ruby/kwikemon.gemspec
Normal file
17
ruby/kwikemon.gemspec
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
$LOAD_PATH << File.expand_path("../", __FILE__)
|
||||||
|
require 'version'
|
||||||
|
|
||||||
|
Gem::Specification.new do |s|
|
||||||
|
s.name = 'kwikemon'
|
||||||
|
s.version = Kwikemon::VERSION
|
||||||
|
s.license = 'MIT'
|
||||||
|
s.summary = 'Ruby client for kwikemon.'
|
||||||
|
s.description = 'Read & write simple monitors using Redis.'
|
||||||
|
s.author = 'Sami Samhuri'
|
||||||
|
s.email = 'sami@samhuri.net'
|
||||||
|
s.homepage = 'https://github.com/samsonjs/kwikemon'
|
||||||
|
s.require_path = '.'
|
||||||
|
s.files = ['kwikemon.rb', 'monitor.rb']
|
||||||
|
s.add_dependency 'redis', '~> 3.0.4'
|
||||||
|
s.required_ruby_version = '>= 1.9.1'
|
||||||
|
end
|
||||||
113
ruby/kwikemon.rb
Normal file
113
ruby/kwikemon.rb
Normal file
|
|
@ -0,0 +1,113 @@
|
||||||
|
# Copyright 2013 Sami Samhuri <sami@samhuri.net>
|
||||||
|
#
|
||||||
|
# MIT License
|
||||||
|
# http://sjs.mit-license.org
|
||||||
|
|
||||||
|
require 'redis'
|
||||||
|
require File.expand_path('../monitor.rb', __FILE__)
|
||||||
|
|
||||||
|
module Kwikemon
|
||||||
|
|
||||||
|
extend self
|
||||||
|
|
||||||
|
include Enumerable
|
||||||
|
|
||||||
|
def redis
|
||||||
|
@redis ||= Redis.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def redis=(redis)
|
||||||
|
@redis = redis
|
||||||
|
end
|
||||||
|
|
||||||
|
def key_prefix
|
||||||
|
@key_prefix ||= "kwikemon"
|
||||||
|
end
|
||||||
|
|
||||||
|
def key_prefix=(key_prefix)
|
||||||
|
@key_prefix = key_prefix
|
||||||
|
end
|
||||||
|
|
||||||
|
def key(x)
|
||||||
|
"#{key_prefix}:#{x}"
|
||||||
|
end
|
||||||
|
|
||||||
|
Monitor.on(:create) do |name|
|
||||||
|
redis.sadd(key('monitors'), name)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
# Set `name` to `value`.
|
||||||
|
#
|
||||||
|
# @param name [#to_s] name of the monitor
|
||||||
|
# @param text [#to_s] status text
|
||||||
|
def set(name, text)
|
||||||
|
Monitor.new(name, text).save
|
||||||
|
end
|
||||||
|
|
||||||
|
# Check if `name` exists3
|
||||||
|
#
|
||||||
|
# @param name [#to_s] name of the monitor
|
||||||
|
# @return [true, false] true if monitor exists, otherwise false
|
||||||
|
def exists?(name)
|
||||||
|
Monitor.new(name).exists?
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get the value of `name`. Returns `nil` if it doesn't exist.
|
||||||
|
#
|
||||||
|
# @param name [#_tos] name of the monitor
|
||||||
|
# @return [String, nil] status text, or `nil` if it doesn't exist
|
||||||
|
def get(name)
|
||||||
|
Monitor.new(name).text
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get the TTL in seconds of `name`. Returns `nil` if it doesn't exit.
|
||||||
|
#
|
||||||
|
# @param name [#_tos] name of the monitor
|
||||||
|
# @return [String, nil] TTL, or `nil` if it doesn't exist
|
||||||
|
def ttl(name)
|
||||||
|
Monitor.new(name).ttl
|
||||||
|
end
|
||||||
|
|
||||||
|
# Count all monitors.
|
||||||
|
def count
|
||||||
|
redis.scard(key('monitors'))
|
||||||
|
end
|
||||||
|
|
||||||
|
# List all monitor names.
|
||||||
|
def list
|
||||||
|
redis.smembers(key('monitors'))
|
||||||
|
end
|
||||||
|
|
||||||
|
def each
|
||||||
|
list.each { |m| yield(m) }
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get a `Hash` of all monitors.
|
||||||
|
def get_all
|
||||||
|
list.inject({}) do |ms, name|
|
||||||
|
ms[name] = Monitor.new(name).text
|
||||||
|
ms
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Remove the monitor named `name`.
|
||||||
|
def remove(name)
|
||||||
|
Monitor.new(name).delete
|
||||||
|
end
|
||||||
|
|
||||||
|
# Clear all monitors.
|
||||||
|
def clear
|
||||||
|
list.each do |name|
|
||||||
|
remove(name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Clean up expired monitors.
|
||||||
|
def sweep
|
||||||
|
list.each do |name|
|
||||||
|
remove(name) unless exists?(name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
98
ruby/monitor.rb
Normal file
98
ruby/monitor.rb
Normal file
|
|
@ -0,0 +1,98 @@
|
||||||
|
# Copyright 2013 Sami Samhuri <sami@samhuri.net>
|
||||||
|
#
|
||||||
|
# MIT License
|
||||||
|
# http://sjs.mit-license.org
|
||||||
|
|
||||||
|
module Kwikemon
|
||||||
|
|
||||||
|
class Monitor
|
||||||
|
|
||||||
|
DefaultTTL = 86400 # 1 day
|
||||||
|
|
||||||
|
attr_accessor :redis
|
||||||
|
attr_reader :name, :text, :ttl, :created, :modified
|
||||||
|
|
||||||
|
@listeners = Hash.new { |h, k| h[k] = [] }
|
||||||
|
|
||||||
|
def Monitor.on(event, &block)
|
||||||
|
@listeners[event] << block
|
||||||
|
end
|
||||||
|
|
||||||
|
def Monitor.emit(event, *args)
|
||||||
|
@listeners[event].each { |handler| handler.call(*args) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(name, text = nil)
|
||||||
|
@name = name
|
||||||
|
@text = text
|
||||||
|
end
|
||||||
|
|
||||||
|
def save
|
||||||
|
if exists?
|
||||||
|
update
|
||||||
|
else
|
||||||
|
create
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def exists?
|
||||||
|
redis.exists(key)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
raise MonitorError.new('name cannot be blank') if name.to_s.strip.length == 0
|
||||||
|
redis.hmset(key, *to_a)
|
||||||
|
emit(:create, name)
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def update(text, ttl = nil)
|
||||||
|
raise MonitorError.new('name cannot be blank') if name.to_s.strip.length == 0
|
||||||
|
redis.hmset(key, 'text', text, 'modified', Time.now.to_i)
|
||||||
|
redis.ttl(key, ttl) if ttl
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def key
|
||||||
|
Kwikemon.key("monitor:#{name}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def ttl
|
||||||
|
@ttl ||= exists? ? redis.ttl(key) : nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def created
|
||||||
|
@created ||= exists? ? redis.hget(key, 'created').to_i : nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def modified
|
||||||
|
@modified ||= exists? ? redis.hget(key, 'modified').to_i : nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def text
|
||||||
|
@text ||= exists? ? redis.hget(key, 'name')
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def redis
|
||||||
|
Kwikemon.redis
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_hash
|
||||||
|
{ name: name,
|
||||||
|
text: text,
|
||||||
|
ttl: ttl || DefaultTTL,
|
||||||
|
created: created || Time.now.to_i,
|
||||||
|
modified: modified || Time.now_to_i
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_a
|
||||||
|
to_hash.to_a
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
3
ruby/version.rb
Normal file
3
ruby/version.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
module Kwikemon
|
||||||
|
VERSION = '0.0.1'
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue