eventd 0.0.0 → 1.0.0

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/LICENSE.md ADDED
File without changes
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ Introduction
2
+ ------------
3
+
4
+ Eventd is a web socket client and server wrapper around **em-websockets**. Eventd strives to provide an event-based
5
+ structure, which provides the basis for web-socket enabled applications. Eventd implements a Server and a Client, which
6
+ both implement a base ``EventdObject``.
7
+
8
+
9
+ Installation
10
+ ------------
11
+
12
+ ```
13
+ gem install eventd
14
+ ```
15
+
16
+
17
+ EventdObject Example
18
+ --------------------
19
+
20
+ ```ruby
21
+ require 'eventd'
22
+
23
+ class Dog < EventdObject
24
+ def initialize(name)
25
+ super()
26
+ @name = name
27
+ end
28
+
29
+ def bark
30
+ self.emit 'bark'
31
+ end
32
+ end
33
+
34
+ vader = Dog.new 'Vader'
35
+
36
+ vader.on 'bark' do
37
+ puts 'Vader barked! OMGWTFBBQ!'
38
+ end
39
+
40
+ vader.bark
41
+ ```
42
+
43
+
44
+ EventdServer Example
45
+ ---------------------
46
+
47
+ ```ruby
48
+ require 'eventd'
49
+
50
+ server = EventdServer.new :host => '127.0.0.1', :port => 8080
51
+
52
+ server.run do
53
+ server.on 'connection' do |client|
54
+ client.on 'hello' do
55
+ client.emit 'hello!'
56
+ end
57
+ end
58
+ end
59
+
60
+ server.start
61
+ ```
@@ -0,0 +1,52 @@
1
+ # EventdClient
2
+ #
3
+ # Communicates with an em-websocket web socket instance, wrapping it in an evented layer. Implements the
4
+ # Eventd Websocket Communication Protocol described at:
5
+ #
6
+ # https://github.com/hydrais/shot/wiki/Eventd-Websocket-Communication-Protocol
7
+ #
8
+ # Shot Framework - Copyright (c) Jesse Aaron Dunlap <me@jessedunlap.me>
9
+ # Licensed under the MIT License. For full licensing information, please
10
+ # see LICENSE.md. http://github.com/JesseDunlap/shot/
11
+
12
+ require 'json'
13
+
14
+ require_relative './eventd_object'
15
+
16
+ class EventdClient < EventdObject
17
+
18
+ # em-websocket socket object
19
+ attr_accessor :socket
20
+
21
+ def initialize(socket)
22
+ super()
23
+ @socket = socket
24
+ setup_socket unless @socket == nil
25
+ end
26
+
27
+ def setup_socket
28
+ @socket.onopen do |handshake|
29
+ self.emit('connect', handshake, true)
30
+ end
31
+
32
+ @socket.onclose do
33
+ self.emit('disconnect', nil, true)
34
+ end
35
+
36
+ @socket.onmessage do |message|
37
+ self.emit('message', message, true)
38
+
39
+ emission = JSON.parse message
40
+ self.emit emission['channel'], emission['data']
41
+ end
42
+ end
43
+
44
+ def emit(channel, data = nil, local = false)
45
+ super(channel, data)
46
+
47
+ if not local
48
+ emission = { 'channel' => channel, 'priority' => 'normal', 'data' => data }
49
+ @socket.send emission.to_json
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,57 @@
1
+ # EventdObject
2
+ #
3
+ # Implements an event-based architecture centered around the concept of *channels* and *listeners*. Each channel can
4
+ # contain multiple listeners. An "emission" can be sent on a channel, which will be then sent to all corresponding
5
+ # listeners on that channel. Emissions can have optional data sent with them to the listeners.
6
+ #
7
+ # Shot Framework - Copyright (c) Jesse Aaron Dunlap <me@jessedunlap.me>
8
+ # Licensed under the MIT License. For full licensing information, please
9
+ # see LICENSE.md. http://github.com/JesseDunlap/shot/
10
+
11
+
12
+ class EventdObject
13
+ attr_accessor :events
14
+
15
+ # Initialize the EventdObject and set up the events hash.
16
+ # Note:: It is important that you always call +super()+ so that the +events+ hash can be initialized
17
+
18
+ def initialize
19
+ @events = {}
20
+ end
21
+
22
+ # Add an event listener to a specified channel
23
+ #
24
+ # == Attributes
25
+ # * +channel+ - Channel to subscribe to
26
+ # * +callback+ - Callback block, which is triggered when an emission occurs on the channel
27
+
28
+ def on(channel, &callback)
29
+ @events[channel] = (@events[channel] or [])
30
+ @events[channel].push callback
31
+ end
32
+
33
+ # Remove all event listeners from a specified channel
34
+ #
35
+ # == Attributes
36
+ # * +channel+ - Channel to unsubscribe from
37
+
38
+ def off(channel)
39
+ @events[channel] = []
40
+ end
41
+
42
+ # Send an emission on a channel
43
+ #
44
+ # == Attributes
45
+ # * +channel+ - Channel to emit on
46
+ # * +data+ - Optional. Data to send along with the emission.
47
+
48
+ def emit(channel, data = nil)
49
+ emit 'emission', { :channel => channel, :data => data } unless channel == 'emission'
50
+
51
+ @events[channel] = (@events[channel] or [])
52
+
53
+ @events[channel].each do |callback|
54
+ if data == nil then callback.call else callback.call data end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,61 @@
1
+ # EventdServer
2
+ #
3
+ # Wraps the em-websocket library in an event-based system. All incoming clients are also converted
4
+ # to EventdClients, which implement the Eventd Websocket Communication Protocol described at:
5
+ #
6
+ # https://github.com/hydrais/shot/wiki/Eventd-Websocket-Communication-Protocol
7
+ #
8
+ # Shot Framework - Copyright (c) Jesse Aaron Dunlap <me@jessedunlap.me>
9
+ # Licensed under the MIT License. For full licensing information, please
10
+ # see LICENSE.md. http://github.com/JesseDunlap/shot/
11
+
12
+
13
+ require 'em-websocket'
14
+ require_relative './eventd_object'
15
+ require_relative './eventd_client'
16
+
17
+ class EventdServer < EventdObject
18
+ # Configuration options for em-websocket and the EventdServer
19
+ attr_accessor :options
20
+
21
+ # Callback block which is called when the server starts for threading purposed
22
+ attr_accessor :run_callback
23
+
24
+ # Initialize the EventdServer with configuration options
25
+ #
26
+ # == Attributes
27
+ # * +options+ - Configuration hash which will be used as the initial EventdServer configuration
28
+
29
+ def initialize(options)
30
+ super()
31
+ @options = options
32
+ end
33
+
34
+ # Add to the EventdServer configuration
35
+ #
36
+ # == Attributes
37
+ # * +options+ - Configuration hash to merge with the existing configuration
38
+
39
+ def configure(options)
40
+ @options.merge! options
41
+ end
42
+
43
+ # Start the server
44
+
45
+ def start
46
+ EM.run do
47
+ @run_callback.call
48
+
49
+ EM::WebSocket.run @options do |socket|
50
+ client = EventdClient.new socket
51
+ self.emit 'connection', client
52
+ end
53
+ end
54
+ end
55
+
56
+ # Special event listener which is triggered when the server runs
57
+
58
+ def run(&callback)
59
+ @run_callback = callback
60
+ end
61
+ end
data/lib/eventd.rb CHANGED
@@ -1,17 +1,3 @@
1
- class Eventd
2
- def initialize
3
- @emitters = {}
4
- end
5
-
6
- def on(emitter_name, &callback)
7
- (@emitters[emitter_name] = (@emitters[emitter_name] || [])).push callback
8
- end
9
-
10
- def off(emitter_name)
11
- @emitters[emitter_name] = []
12
- end
13
-
14
- def emit(emitter_name, parameters = nil)
15
- (@emitters[emitter_name] || []).each do |callback| callback.call parameters end
16
- end
17
- end
1
+ require_relative './eventd/eventd_server'
2
+ require_relative './eventd/eventd_client'
3
+ require_relative './eventd/eventd_object'
metadata CHANGED
@@ -1,44 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eventd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 1.0.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
- - Jesse Dunlap
8
+ - Jesse A. Dunlap
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-05-18 00:00:00.000000000 Z
12
- dependencies: []
13
- description: A quick architecture that can be used to add eventing support to any
14
- object
12
+ date: 2013-06-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: em-websocket
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: flexmock
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Eventd provides a clear and comprehensive structure to add evented functionality
47
+ to any object. Eventd also implements an event-based Web Socket server, which is
48
+ a wrapper around em-websockets.
15
49
  email: me@jessedunlap.me
16
50
  executables: []
17
51
  extensions: []
18
52
  extra_rdoc_files: []
19
53
  files:
54
+ - lib/eventd/eventd_client.rb
55
+ - lib/eventd/eventd_object.rb
56
+ - lib/eventd/eventd_server.rb
20
57
  - lib/eventd.rb
21
- homepage: ''
22
- licenses: []
23
- metadata: {}
58
+ - LICENSE.md
59
+ - README.md
60
+ homepage: http://github.com/shot/
61
+ licenses:
62
+ - MIT
24
63
  post_install_message:
25
64
  rdoc_options: []
26
65
  require_paths:
27
66
  - lib
28
67
  required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
29
69
  requirements:
30
- - - '>='
70
+ - - ! '>='
31
71
  - !ruby/object:Gem::Version
32
72
  version: '0'
33
73
  required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
34
75
  requirements:
35
- - - '>='
76
+ - - ! '>='
36
77
  - !ruby/object:Gem::Version
37
78
  version: '0'
38
79
  requirements: []
39
80
  rubyforge_project:
40
- rubygems_version: 2.0.3
81
+ rubygems_version: 1.8.24
41
82
  signing_key:
42
- specification_version: 4
43
- summary: Add eventing support to any object!
83
+ specification_version: 3
84
+ summary: A simple, clever way of adding event-based functionality to Ruby objects.
44
85
  test_files: []
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 73d3d294cf56822b928463281bb0ad466abe0de5
4
- data.tar.gz: 127b2e6476bd229a1d788b282bbef8655e6c2789
5
- SHA512:
6
- metadata.gz: a533058737df7dd30638fb25e55ec0fc7da00bfec54cfc28a2cdd270ad23175f639c9597ffd6eaf9be1a62df7d460806de379d2b1d10b00985c686aac10bcfdd
7
- data.tar.gz: 78a14581e774dbee1c3c0349dbae8c623dc1dab490f2f735bfd7cd57fcc76fa6bc377d87b4a988901dc08b8ebd278c6068069ed9a4a977b796473685f11897f7