nodevent 3.0.3 → 3.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/nodevent/version.rb +1 -1
- data/lib/nodevent.rb +38 -10
- data/spec/base_spec.rb +165 -0
- data/spec/spec_helper.rb +6 -0
- metadata +6 -4
- /data/spec/{nodevent_spec.rb → emitter_spec.rb} +0 -0
    
        data/lib/nodevent/version.rb
    CHANGED
    
    
    
        data/lib/nodevent.rb
    CHANGED
    
    | @@ -3,19 +3,46 @@ require 'json' | |
| 3 3 |  | 
| 4 4 | 
             
            module NoDevent
         | 
| 5 5 | 
             
              def self.included(base)
         | 
| 6 | 
            -
                 | 
| 6 | 
            +
                raise "No longer supported, Please include NoDevent::Base instead"
         | 
| 7 7 | 
             
              end
         | 
| 8 | 
            +
              module Base
         | 
| 9 | 
            +
                def self.included(base)
         | 
| 10 | 
            +
                  base.extend(NoDevent::Base)
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
                
         | 
| 13 | 
            +
                class << self
         | 
| 14 | 
            +
                  def emit(name, message)
         | 
| 15 | 
            +
                    NoDevent::Emitter.emit(self.room, name, message)
         | 
| 16 | 
            +
                  end
         | 
| 17 | 
            +
                  def room(obj)
         | 
| 18 | 
            +
                    NoDevent::Emitter.room(self)
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
                  def room_key(expires)
         | 
| 21 | 
            +
                    Emitter.room_key(self.room, expires)
         | 
| 22 | 
            +
                  end
         | 
| 23 | 
            +
                end
         | 
| 8 24 |  | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 25 | 
            +
                def emit(name, message=nil)
         | 
| 26 | 
            +
                  Emitter.emit(self.room, name, message || self)
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
                
         | 
| 29 | 
            +
                def room
         | 
| 30 | 
            +
                  Emitter.room(self)
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
                
         | 
| 33 | 
            +
                def room_key(expires)
         | 
| 34 | 
            +
                  Emitter.room_key(self.room, expires)
         | 
| 35 | 
            +
                end
         | 
| 12 36 |  | 
| 13 | 
            -
              def room
         | 
| 14 | 
            -
                Emitter.room(self)
         | 
| 15 | 
            -
              end
         | 
| 16 37 |  | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 38 | 
            +
                def nodevent_create
         | 
| 39 | 
            +
                  NoDevent::Emitter.emit(self.class.name, 'create', self)
         | 
| 40 | 
            +
                end
         | 
| 41 | 
            +
                
         | 
| 42 | 
            +
                def nodevent_update
         | 
| 43 | 
            +
                  self.emit('update')
         | 
| 44 | 
            +
                end
         | 
| 45 | 
            +
              
         | 
| 19 46 | 
             
              end
         | 
| 20 47 |  | 
| 21 48 |  | 
| @@ -39,7 +66,7 @@ module NoDevent | |
| 39 66 |  | 
| 40 67 | 
             
                  def config
         | 
| 41 68 | 
             
                    @@config ||= Hash.new({
         | 
| 42 | 
            -
                                            :host => "http:// | 
| 69 | 
            +
                                            :host => "http://loadsfcalhost:8080",
         | 
| 43 70 | 
             
                                            :namespace => "/nodevent"
         | 
| 44 71 | 
             
                                          })
         | 
| 45 72 | 
             
                    @@config
         | 
| @@ -48,6 +75,7 @@ module NoDevent | |
| 48 75 | 
             
                  def emit(room, name, message)
         | 
| 49 76 | 
             
                    room = NoDevent::Emitter.room(room)
         | 
| 50 77 |  | 
| 78 | 
            +
                    puts "NoDevent::emit: #{room}, #{name}, #{message}"
         | 
| 51 79 | 
             
                    $redis.publish("events", 
         | 
| 52 80 | 
             
                                   { :room => room,
         | 
| 53 81 | 
             
                                     :event => name, 
         | 
    
        data/spec/base_spec.rb
    ADDED
    
    | @@ -0,0 +1,165 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module ActiveRecord
         | 
| 4 | 
            +
              class Base;end
         | 
| 5 | 
            +
            end
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            class ModelMock < ActiveRecord::Base
         | 
| 8 | 
            +
              include NoDevent::Base
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              attr_accessor :to_param
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              def initialize(param)
         | 
| 13 | 
            +
                @to_param = param
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
              def to_json(options)
         | 
| 16 | 
            +
                '{"id":"#{to_param}"}'
         | 
| 17 | 
            +
              end
         | 
| 18 | 
            +
            end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            NoDevent::Emitter.config = {
         | 
| 21 | 
            +
              :host => "http://thehost",
         | 
| 22 | 
            +
              :namespace => "thenamespace", 
         | 
| 23 | 
            +
              :secret => "asdf"
         | 
| 24 | 
            +
            }
         | 
| 25 | 
            +
             | 
| 26 | 
            +
            describe NoDevent do
         | 
| 27 | 
            +
              describe "the class" do
         | 
| 28 | 
            +
                
         | 
| 29 | 
            +
                it { ModelMock.room.should == "ModelMock" }
         | 
| 30 | 
            +
                
         | 
| 31 | 
            +
                it "should emit to the right room" do
         | 
| 32 | 
            +
                  $redis.should_receive(:publish).with("events", 
         | 
| 33 | 
            +
                                                       {
         | 
| 34 | 
            +
                                                         :room => "ModelMock", 
         | 
| 35 | 
            +
                                                         :event => 'theevent', 
         | 
| 36 | 
            +
                                                         :message => 'themessage'}.to_json)
         | 
| 37 | 
            +
                  
         | 
| 38 | 
            +
                  ModelMock.emit('theevent', 'themessage')
         | 
| 39 | 
            +
                end
         | 
| 40 | 
            +
                
         | 
| 41 | 
            +
                it "should create a key from the right room" do
         | 
| 42 | 
            +
                  t = Time.now
         | 
| 43 | 
            +
                  ts = (t.to_f*1000).to_i
         | 
| 44 | 
            +
                  ModelMock.room_key(t).should == 
         | 
| 45 | 
            +
                    (Digest::SHA2.new << "ModelMock" << ts.to_s << NoDevent::Emitter.config[:secret]).to_s
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
                
         | 
| 48 | 
            +
                describe "with a custom room name" do
         | 
| 49 | 
            +
                  before  do
         | 
| 50 | 
            +
                    ModelMock.stub(:room => "otherRoom")
         | 
| 51 | 
            +
                  end
         | 
| 52 | 
            +
                  
         | 
| 53 | 
            +
                  it { ModelMock.room.should == "otherRoom" }
         | 
| 54 | 
            +
                  
         | 
| 55 | 
            +
                  it "should emit to the right room" do
         | 
| 56 | 
            +
                    $redis.should_receive(:publish).with("events", 
         | 
| 57 | 
            +
                                                         {
         | 
| 58 | 
            +
                                                           :room => "otherRoom", 
         | 
| 59 | 
            +
                                                           :event => 'theevent', 
         | 
| 60 | 
            +
                                                           :message => 'themessage'}.to_json)
         | 
| 61 | 
            +
                    
         | 
| 62 | 
            +
                    ModelMock.emit('theevent', 'themessage')
         | 
| 63 | 
            +
                  end
         | 
| 64 | 
            +
                  
         | 
| 65 | 
            +
                  it "should create a key from the right room" do
         | 
| 66 | 
            +
                    t = Time.now
         | 
| 67 | 
            +
                    ts = (t.to_f*1000).to_i
         | 
| 68 | 
            +
                    ModelMock.room_key(t).should == 
         | 
| 69 | 
            +
                      (Digest::SHA2.new << ModelMock.room << ts.to_s << NoDevent::Emitter.config[:secret]).to_s
         | 
| 70 | 
            +
                  end
         | 
| 71 | 
            +
                end  
         | 
| 72 | 
            +
              end
         | 
| 73 | 
            +
              
         | 
| 74 | 
            +
              describe "an instance of the class" do
         | 
| 75 | 
            +
                let(:instance) {ModelMock.new( 'theparam') }
         | 
| 76 | 
            +
             | 
| 77 | 
            +
                it { instance.room.should == "ModelMock_theparam" }
         | 
| 78 | 
            +
                it "should emit to the right room" do
         | 
| 79 | 
            +
                  $redis.should_receive(:publish).with("events", 
         | 
| 80 | 
            +
                                                       {
         | 
| 81 | 
            +
                                                         :room => "ModelMock_theparam", 
         | 
| 82 | 
            +
                                                         :event => 'theevent', 
         | 
| 83 | 
            +
                                                         :message => 'themessage'}.to_json)
         | 
| 84 | 
            +
                  
         | 
| 85 | 
            +
                  instance.emit('theevent', 'themessage')
         | 
| 86 | 
            +
                end
         | 
| 87 | 
            +
                
         | 
| 88 | 
            +
                describe "#nodevent_create" do
         | 
| 89 | 
            +
                  it "should emit to the right room" do
         | 
| 90 | 
            +
                  $redis.should_receive(:publish).with("events", 
         | 
| 91 | 
            +
                                                       {
         | 
| 92 | 
            +
                                                         :room => "ModelMock", 
         | 
| 93 | 
            +
                                                         :event => 'create', 
         | 
| 94 | 
            +
                                                         :message => instance}.to_json)
         | 
| 95 | 
            +
                    instance.nodevent_create
         | 
| 96 | 
            +
                    
         | 
| 97 | 
            +
                  end
         | 
| 98 | 
            +
                end
         | 
| 99 | 
            +
                describe "#nodevent_update" do
         | 
| 100 | 
            +
                  it "should emit to the right room" do
         | 
| 101 | 
            +
                  $redis.should_receive(:publish).with("events", 
         | 
| 102 | 
            +
                                                       {
         | 
| 103 | 
            +
                                                         :room => "ModelMock_theparam", 
         | 
| 104 | 
            +
                                                         :event => 'update', 
         | 
| 105 | 
            +
                                                         :message => instance}.to_json)
         | 
| 106 | 
            +
                    instance.nodevent_update
         | 
| 107 | 
            +
                    
         | 
| 108 | 
            +
                  end
         | 
| 109 | 
            +
                end
         | 
| 110 | 
            +
             | 
| 111 | 
            +
                it "should create a key from the right room" do
         | 
| 112 | 
            +
                  t = Time.now
         | 
| 113 | 
            +
                  ts = (t.to_f*1000).to_i
         | 
| 114 | 
            +
                  instance.room_key(t).should == 
         | 
| 115 | 
            +
                    (Digest::SHA2.new << instance.room << ts.to_s << NoDevent::Emitter.config[:secret]).to_s
         | 
| 116 | 
            +
                end
         | 
| 117 | 
            +
                
         | 
| 118 | 
            +
                describe "with a custom room name" do
         | 
| 119 | 
            +
                  before  do
         | 
| 120 | 
            +
                    instance.stub(:room => "otherRoom")
         | 
| 121 | 
            +
                  end
         | 
| 122 | 
            +
                  
         | 
| 123 | 
            +
                  it { instance.room.should == "otherRoom" }
         | 
| 124 | 
            +
                  
         | 
| 125 | 
            +
                  it "should emit to the right room" do
         | 
| 126 | 
            +
                    $redis.should_receive(:publish).with("events", 
         | 
| 127 | 
            +
                                                         {
         | 
| 128 | 
            +
                                                           :room => "otherRoom", 
         | 
| 129 | 
            +
                                                           :event => 'theevent', 
         | 
| 130 | 
            +
                                                           :message => 'themessage'}.to_json)
         | 
| 131 | 
            +
                    
         | 
| 132 | 
            +
                    instance.emit('theevent', 'themessage')
         | 
| 133 | 
            +
                  end
         | 
| 134 | 
            +
                  
         | 
| 135 | 
            +
                  it "should create a key from the right room" do
         | 
| 136 | 
            +
                    t = Time.now
         | 
| 137 | 
            +
                    ts = (t.to_f*1000).to_i
         | 
| 138 | 
            +
                    instance.room_key(t).should == 
         | 
| 139 | 
            +
                      (Digest::SHA2.new << "otherRoom" << ts.to_s << NoDevent::Emitter.config[:secret]).to_s
         | 
| 140 | 
            +
                  end
         | 
| 141 | 
            +
                  describe "#nodevent_create" do
         | 
| 142 | 
            +
                    it "should emit to the right room" do
         | 
| 143 | 
            +
                      $redis.should_receive(:publish).with("events", 
         | 
| 144 | 
            +
                                                           {
         | 
| 145 | 
            +
                                                             :room => instance.class.room, 
         | 
| 146 | 
            +
                                                             :event => 'create', 
         | 
| 147 | 
            +
                                                             :message => instance}.to_json)
         | 
| 148 | 
            +
                      instance.nodevent_create
         | 
| 149 | 
            +
                      
         | 
| 150 | 
            +
                    end
         | 
| 151 | 
            +
                  end
         | 
| 152 | 
            +
                  describe "#nodevent_update" do
         | 
| 153 | 
            +
                    it "should emit to the right room" do
         | 
| 154 | 
            +
                      $redis.should_receive(:publish).with("events", 
         | 
| 155 | 
            +
                                                           {
         | 
| 156 | 
            +
                                                             :room => instance.room,
         | 
| 157 | 
            +
                                                             :event => 'update', 
         | 
| 158 | 
            +
                                                             :message => instance}.to_json)
         | 
| 159 | 
            +
                      instance.nodevent_update
         | 
| 160 | 
            +
                      
         | 
| 161 | 
            +
                    end
         | 
| 162 | 
            +
                  end  
         | 
| 163 | 
            +
                end
         | 
| 164 | 
            +
              end
         | 
| 165 | 
            +
             end
         | 
    
        data/spec/spec_helper.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: nodevent
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 3.0. | 
| 4 | 
            +
              version: 3.0.4
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2012-07- | 
| 12 | 
            +
            date: 2012-07-24 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: redis
         | 
| @@ -44,7 +44,8 @@ files: | |
| 44 44 | 
             
            - lib/nodevent.rb
         | 
| 45 45 | 
             
            - lib/nodevent/version.rb
         | 
| 46 46 | 
             
            - nodevent.gemspec
         | 
| 47 | 
            -
            - spec/ | 
| 47 | 
            +
            - spec/base_spec.rb
         | 
| 48 | 
            +
            - spec/emitter_spec.rb
         | 
| 48 49 | 
             
            - spec/spec_helper.rb
         | 
| 49 50 | 
             
            homepage: https://github.com/cconstantine/NoDevent
         | 
| 50 51 | 
             
            licenses: []
         | 
| @@ -71,5 +72,6 @@ signing_key: | |
| 71 72 | 
             
            specification_version: 3
         | 
| 72 73 | 
             
            summary: Write a gem summary
         | 
| 73 74 | 
             
            test_files:
         | 
| 74 | 
            -
            - spec/ | 
| 75 | 
            +
            - spec/base_spec.rb
         | 
| 76 | 
            +
            - spec/emitter_spec.rb
         | 
| 75 77 | 
             
            - spec/spec_helper.rb
         | 
| 
            File without changes
         |