require 'redis' require 'oj' 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 def ping publish(encode_json({})) end private def publish_event(event, message_id, timestamp) publish(encode_json("message_id" => message_id, "timestamp" => timestamp, "event" => event)) true rescue Exception return false end def publish(message) redis.publish(channel, message) end def channel "webhook_stopwatch:events" end def encode_json(payload) Oj.dump(payload) end end end