Sha256: 6ee48533072c3ef399135241802c95c091299c73a6eaf6ab0b9d453ce904fdc9

Contents?: true

Size: 998 Bytes

Versions: 1

Compression:

Stored size: 998 Bytes

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

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