Skip to content

Commit

Permalink
Allow to subscribe to specific resource. #16
Browse files Browse the repository at this point in the history
  • Loading branch information
josecp committed May 22, 2014
1 parent 041b9cf commit add7722
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rest_redis_pub_sub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "rest_redis_pub_sub/client"
require "rest_redis_pub_sub/event_handler"
require "rest_redis_pub_sub/helper"
require "rest_redis_pub_sub/resource_subscriber"
require "rest_redis_pub_sub/version"

module RestRedisPubSub
Expand Down
22 changes: 22 additions & 0 deletions lib/rest_redis_pub_sub/resource_subscriber.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module RestRedisPubSub
class ResourceSubscriber
SUBSCRIPTION_KEY = 'subscriptions'

def initialize(attrs)
@app = attrs.fetch(:app)
@resource = attrs.fetch(:resource)
@id = attrs.fetch(:id)
end

attr_reader :app, :resource, :id

def subscribe
RestRedisPubSub.redis_instance.sadd(resource_key, RestRedisPubSub.publisher)
end

def resource_key
[SUBSCRIPTION_KEY, app, resource, id].join(':')
end

end
end
40 changes: 40 additions & 0 deletions spec/rest_redis_pub_sub/resource_subscriber_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'spec_helper'

describe RestRedisPubSub::ResourceSubscriber do

describe "#initialize" do
it "should succeed if all required attrs are present" do
expect{
described_class.new(app: 'crushpath', resource: 'spot', id: 1234)
}.not_to raise_error
end

it "should fail if required attrs are missing" do
expect{
described_class.new(resource: 'spot', id: 1234)
}.to raise_error(KeyError)

expect{
described_class.new(app: 'crushpath', id: 1234)
}.to raise_error(KeyError)

expect{
described_class.new(app: 'crushpath', resource: 'spot')
}.to raise_error(KeyError)
end
end

describe "#resource_key" do
it "should return the correct key" do
subscriber = described_class.new(app: 'crushpath', resource: 'spot', id: 1234)
expect(subscriber.resource_key).to eq("subscriptions:crushpath:spot:1234")
end
end

describe "#subscribe" do
it "should add current app to the resource subscribers list" do

end
end

end

0 comments on commit add7722

Please sign in to comment.