Sha256: 3f9a264178e8d24c80b08db2031711123ef272b447adf56c3f549375649e165b

Contents?: true

Size: 937 Bytes

Versions: 1

Compression:

Stored size: 937 Bytes

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
webhook_stopwatch_client-0.1.1 lib/webhook_stopwatch/client.rb