ruby-ddp-client 0.0.1
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/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/ruby-ddp-client.rb +98 -0
- data/lib/ruby-ddp-client/version.rb +3 -0
- data/ruby-ddp-client.gemspec +26 -0
- metadata +99 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require "ruby-ddp-client/version"
|
2
|
+
require 'faye/websocket'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
|
6
|
+
class RubyDdp::Client < Faye::WebSocket::Client
|
7
|
+
attr_accessor :onconnect
|
8
|
+
attr_accessor :collections
|
9
|
+
|
10
|
+
def initialize(host, port = 3000, path = 'websocket')
|
11
|
+
super("http://#{host}:#{port}/#{path}")
|
12
|
+
self.init_event_handlers()
|
13
|
+
|
14
|
+
@_callbacks = {}
|
15
|
+
@_next_id = 0
|
16
|
+
@collections = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def connect
|
20
|
+
dosend(:msg => :connect)
|
21
|
+
end
|
22
|
+
|
23
|
+
def call(method, params = [], &blk)
|
24
|
+
id = self.next_id()
|
25
|
+
self.dosend(:msg => 'method', :id => id, :method => method, :params => params)
|
26
|
+
@_callbacks[id] = blk
|
27
|
+
end
|
28
|
+
|
29
|
+
def subscribe(name, params, &blk)
|
30
|
+
id = self.next_id()
|
31
|
+
self.dosend(:msg => 'sub', :id => id, :name => name, :params => params)
|
32
|
+
@_callbacks[id] = blk
|
33
|
+
end
|
34
|
+
protected
|
35
|
+
def next_id
|
36
|
+
(@_next_id += 1).to_s
|
37
|
+
end
|
38
|
+
|
39
|
+
def dosend(data)
|
40
|
+
self.send(data.to_json)
|
41
|
+
end
|
42
|
+
|
43
|
+
def init_event_handlers
|
44
|
+
# event handlers
|
45
|
+
self.onopen = lambda do |event|
|
46
|
+
self.connect()
|
47
|
+
end
|
48
|
+
|
49
|
+
self.onmessage = lambda do |event|
|
50
|
+
data = JSON.parse(event.data)
|
51
|
+
if data.has_key?('msg')
|
52
|
+
|
53
|
+
# TODO: 'error', 'nosub'
|
54
|
+
# TODO -- method acks <- not sure exactly what the point is here
|
55
|
+
|
56
|
+
case(data['msg'])
|
57
|
+
when 'connected'
|
58
|
+
self.onconnect.call(event)
|
59
|
+
|
60
|
+
# collections
|
61
|
+
when 'data'
|
62
|
+
if data.has_key?('collection')
|
63
|
+
name = data['collection']
|
64
|
+
id = data['id']
|
65
|
+
@collections[name] ||= {}
|
66
|
+
@collections[name][id] ||= {}
|
67
|
+
|
68
|
+
if data.has_key?('set')
|
69
|
+
data['set'].each do |key, value|
|
70
|
+
@collections[name][id][key] = value
|
71
|
+
end
|
72
|
+
end
|
73
|
+
if data.has_key?('unset')
|
74
|
+
data['unset'].each do |key|
|
75
|
+
@collections[name][id].delete(key)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# subscription ready
|
80
|
+
elsif data.has_key?('subs')
|
81
|
+
data['subs'].each do |id|
|
82
|
+
cb = @_callbacks[id]
|
83
|
+
cb.call() if cb
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# method callbacks
|
88
|
+
when 'result'
|
89
|
+
cb = @_callbacks[data['id']]
|
90
|
+
cb.call(data['error'], data['result']) if cb
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
self.onclose = lambda do |event|
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "ruby-ddp-client/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "ruby-ddp-client"
|
7
|
+
s.version = RubyDdp::VERSION
|
8
|
+
s.authors = ["Tom Coleman"]
|
9
|
+
s.email = ["tom@thesnail.org"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{A DDP client for ruby, to connect to meteor servers.}
|
12
|
+
s.description = %q{A DDP client for ruby, to connect to meteor servers.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "ruby-ddp-client"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# s.add_development_dependency "rspec"
|
22
|
+
# s.add_development_dependency "eventmachine"
|
23
|
+
|
24
|
+
s.add_runtime_dependency "faye-websocket"
|
25
|
+
s.add_runtime_dependency "json"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-ddp-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Tom Coleman
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-07-19 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: faye-websocket
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: json
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
description: A DDP client for ruby, to connect to meteor servers.
|
49
|
+
email:
|
50
|
+
- tom@thesnail.org
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files: []
|
56
|
+
|
57
|
+
files:
|
58
|
+
- .gitignore
|
59
|
+
- Gemfile
|
60
|
+
- Rakefile
|
61
|
+
- lib/ruby-ddp-client.rb
|
62
|
+
- lib/ruby-ddp-client/version.rb
|
63
|
+
- ruby-ddp-client.gemspec
|
64
|
+
homepage: ""
|
65
|
+
licenses: []
|
66
|
+
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project: ruby-ddp-client
|
93
|
+
rubygems_version: 1.8.10
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: A DDP client for ruby, to connect to meteor servers.
|
97
|
+
test_files: []
|
98
|
+
|
99
|
+
has_rdoc:
|