synth 0.0.1 → 0.0.2

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.
@@ -1,4 +1,46 @@
1
- require "synth/version"
1
+ require 'synth/version'
2
+ require 'synth/rails/engine'
3
+ require 'redis/namespace'
2
4
 
3
5
  module Synth
6
+ extend self
7
+
8
+ autoload :Publish, 'synth/publish'
9
+ autoload :Helpers, 'synth/helpers'
10
+
11
+ ##
12
+ # Accepts:
13
+ # 1. A 'hostname:port' String
14
+ # 2. A 'hostname:port:db' String (to select the Redis db)
15
+ # 3. A 'hostname:port/namespace' String (to set the Redis namespace)
16
+ # 4. A Redis URL String 'redis://host:port'
17
+ # 5. An instance of `Redis`, `Redis::Client`, `Redis::DistRedis`,
18
+ # or `Redis::Namespace`.
19
+ #
20
+ def redis=(server)
21
+ case server
22
+ when String
23
+ if server =~ /redis\:\/\//
24
+ redis = Redis.connect(:url => server, :thread_safe => true)
25
+ else
26
+ server, namespace = server.split('/', 2)
27
+ host, port, db = server.split(':')
28
+ redis = Redis.new(:host => host, :port => port,
29
+ :thread_safe => true, :db => db)
30
+ end
31
+ namespace ||= :synth
32
+
33
+ @redis = Redis::Namespace.new(namespace, :redis => redis)
34
+ when Redis::Namespace
35
+ @redis = server
36
+ else
37
+ @redis = Redis::Namespace.new(:synth, :redis => server)
38
+ end
39
+ end
40
+
41
+ ##
42
+ # Returns the Redis connection. If none exists, try creating one.
43
+ def redis
44
+ @redis ||= self.redis = Redis.respond_to?(:connect) ? Redis.connect : 'localhost:6379'
45
+ end
4
46
  end
@@ -0,0 +1,5 @@
1
+ module Synth
2
+ module Helpers
3
+ extend self
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ module Synth
2
+ module Publish
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ after_touch :publish_to_synth
7
+ after_update :publish_to_synth
8
+ after_destroy :publish_to_synth
9
+ end
10
+
11
+ private
12
+ def publish_to_synth
13
+ Synth.redis.publish [ self.class.to_s.downcase, id ].join('-'), persisted? ? updated_at.to_i : nil
14
+ end
15
+
16
+ module ClassMethods
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,6 @@
1
+ module Synth
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -1,3 +1,3 @@
1
1
  module Synth
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -2,10 +2,14 @@
2
2
  require File.expand_path('../lib/synth/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
- gem.authors = ["Stephen Ausman"]
6
- gem.email = ["sausman@stackd.com"]
5
+ gem.authors = ["Stephen Ausman", "Fredrick Galoso"]
6
+ gem.email = ["synth@stackd.com"]
7
7
  gem.summary = "Real-time for Rails"
8
- gem.homepage = "https://github.com/stackd/synth"
8
+ gem.description = "Real-time for Rails"
9
+ gem.homepage = "https://github.com/stackd/synth-rb"
10
+
11
+ gem.add_dependency 'redis'
12
+ gem.add_dependency 'redis-namespace'
9
13
 
10
14
  gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
11
15
  gem.files = `git ls-files`.split("\n")
@@ -0,0 +1,45 @@
1
+ ##
2
+ # Synth class
3
+
4
+ class Synth
5
+
6
+ @establishConnection = ->
7
+ @connection = new WebSocket 'ws://localhost:1337', 'synth'
8
+
9
+ @subscribe = ->
10
+ @subscriptions = ([ $(e).attr('class'), $(e).attr('data-id') ].join('-') for e in $('[class][data-id]')).uniq()
11
+ @connection.send JSON.stringify(@subscriptions)
12
+
13
+ @log = (msg) ->
14
+ console.log '[synth] ' + msg
15
+
16
+ ##
17
+ # Events
18
+
19
+ $(document).on 'synth:subscribe', ->
20
+ Synth.subscribe()
21
+
22
+ ##
23
+ # On document ready
24
+
25
+ $ ->
26
+
27
+ Synth.establishConnection()
28
+
29
+ Synth.connection.onopen = ->
30
+ $(document).trigger 'synth:subscribe'
31
+ Synth.log 'Connected'
32
+
33
+ Synth.connection.onmessage = (msg) ->
34
+ Synth.log 'Message received: ' + msg.data
35
+
36
+ Synth.connection.onclose = ->
37
+ Synth.log 'Disconnected'
38
+
39
+ ##
40
+ # Utils
41
+
42
+ Array::uniq = ->
43
+ output = {}
44
+ output[@[key]] = @[key] for key in [0...@length]
45
+ value for key, value of output
metadata CHANGED
@@ -1,19 +1,42 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Stephen Ausman
9
+ - Fredrick Galoso
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2012-04-15 00:00:00.000000000 Z
13
- dependencies: []
14
- description:
13
+ date: 2012-04-21 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: redis
17
+ requirement: &70349182991980 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70349182991980
26
+ - !ruby/object:Gem::Dependency
27
+ name: redis-namespace
28
+ requirement: &70349182986760 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *70349182986760
37
+ description: Real-time for Rails
15
38
  email:
16
- - sausman@stackd.com
39
+ - synth@stackd.com
17
40
  executables: []
18
41
  extensions: []
19
42
  extra_rdoc_files: []
@@ -24,9 +47,13 @@ files:
24
47
  - README.md
25
48
  - Rakefile
26
49
  - lib/synth.rb
50
+ - lib/synth/helpers.rb
51
+ - lib/synth/publish.rb
52
+ - lib/synth/rails/engine.rb
27
53
  - lib/synth/version.rb
28
54
  - synth.gemspec
29
- homepage: https://github.com/stackd/synth
55
+ - vendor/assets/javascripts/synth.js.coffee.erb
56
+ homepage: https://github.com/stackd/synth-rb
30
57
  licenses: []
31
58
  post_install_message:
32
59
  rdoc_options: []