require 'redis' require 'multi_json' module WebhookStopwatch class Client attr_reader :redis def initialize(redis_or_url) if redis_or_url.is_a?(String) @redis = Redis.new(:url => redis_or_url) else @redis = redis_or_url end end def start(message_id, timestamp) publish_event("start", message_id, timestamp) end def stop(message_id, timestamp) publish_event("stop", message_id, timestamp) end private def publish_event(event, message_id, timestamp) redis.publish(channel, encode_json({"message_id" => message_id, "timestamp" => timestamp, "event" => event})) true rescue Exception return false end def channel "webhook_stopwatch:events" end def encode_json(payload) MultiJson.dump(payload) end end end