metybur 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.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +137 -0
- data/Rakefile +8 -0
- data/lib/metybur/client.rb +45 -0
- data/lib/metybur/collection.rb +14 -0
- data/lib/metybur/version.rb +3 -0
- data/lib/metybur.rb +77 -0
- data/metybur.gemspec +33 -0
- data/spec/metybur_spec.rb +222 -0
- data/spec/mocks/websocket_mock.rb +31 -0
- data/spec/spec_helper.rb +91 -0
- metadata +163 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2043abcd1c4c248fdf3e2a7981a4b68aed1c248d
|
4
|
+
data.tar.gz: a44a7514d74dcdbdb571d80804db938deba29bf8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bfded496cb0f109c29daac6a401001499ba7f698c47e9358a2d2e8116fdacf9f88cc807b4608786e6c079fb4a2099335f431babfc87c8825c492173dceca0d6b
|
7
|
+
data.tar.gz: 1bc3b6fc5afb48f64b552a7006ec84680bd0c6efa8b2df70ea4580db64fdb96e2db830dfb6de82851729392589c5bcf253e85890c06c9e5f66327d4c949fc46d
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Clemens Helm
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
# Metybur
|
2
|
+
[](https://travis-ci.org/clemenshelm/metybur)
|
3
|
+
|
4
|
+
A DDP client for Ruby to connect to Meteor apps.
|
5
|
+
|
6
|
+
Metybur lets your Ruby application connect to a Meteor app. It allows you
|
7
|
+
to subscribe to collections and to receive updates on them.
|
8
|
+
You can also call Meteor methods from Ruby.
|
9
|
+
|
10
|
+
> ## Caution!
|
11
|
+
>
|
12
|
+
> This gem is just a few days old so far and doesn't include basic features like proper error handling. Also only a subset of the DDP protocol is implemented so far. Everything described in this README should work, though.
|
13
|
+
>
|
14
|
+
> I'll keep working on it constantly, so I suppose there will be a complete and stable version soon. Stay tuned!
|
15
|
+
>
|
16
|
+
> Clemens
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
gem 'metybur'
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install metybur
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
### Connecting to a Meteor app
|
35
|
+
|
36
|
+
Metybur runs in an [EventMachine](http://eventmachine.rubyforge.org/) loop.
|
37
|
+
Therefore all our code must be wrapped in an `EM.run` block.
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
require 'eventmachine'
|
41
|
+
|
42
|
+
EM.run do
|
43
|
+
meteor = Metybur.connect('http://my-meteor-app.org:80/websocket')
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
will connect to your Meteor app. If you want to log in at your app, pass the credentials:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
require 'eventmachine'
|
51
|
+
|
52
|
+
EM.run do
|
53
|
+
meteor = Metybur.connect(
|
54
|
+
'http://my-meteor-app.org:80/websocket',
|
55
|
+
email: 'rubyist@meteor.com',
|
56
|
+
password: 'twinkle twinkle'
|
57
|
+
)
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
You can also pass a `:username` or `:id` instead of an `:email`. These arguments correspond to those described in [the Meteor docs](http://docs.meteor.com/#/full/meteor_loginwithpassword).
|
62
|
+
|
63
|
+
Alternatively you can log in at a later point by calling the `login` method explicitly:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
require 'eventmachine'
|
67
|
+
|
68
|
+
EM.run do
|
69
|
+
meteor = Metybur.connect('http://my-meteor-app.org:80/websocket')
|
70
|
+
|
71
|
+
# Do something unauthenticated...
|
72
|
+
|
73
|
+
meteor.login(user: {email: 'rubyist@meteor.com'}, password: 'twinkle twinkle'
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
From now on I'll skip the `EM.run` block in code examples, but don't forget about it. Otherwise it won't work! Promise!
|
78
|
+
|
79
|
+
### Subscribing to a Meteor record set
|
80
|
+
|
81
|
+
After connecting to your Meteor app, you can subscribe to one of the published record sets:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
meteor = Metybur.connect('http://my-meteor-app.org:80/websocket')
|
85
|
+
meteor.subscribe('my-chat-messages')
|
86
|
+
```
|
87
|
+
|
88
|
+
### Collections
|
89
|
+
|
90
|
+
Once you've subscribed, you will receive all records that are already in the record set. The record set contains records from one or more collections. You can process these records as they arrive:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
@chat_messages = {}
|
94
|
+
meteor.collection('chat-messages')
|
95
|
+
.on(:added) { |id, attributes| @chat_messages[id] = attributes }
|
96
|
+
```
|
97
|
+
|
98
|
+
### Remote Procedure Calls
|
99
|
+
|
100
|
+
Call meteor methods to write back data to Meteor or to trigger actions in your Meteor app.
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
meteor.post_chat_message('Hey there!', in_room: 'General')
|
104
|
+
```
|
105
|
+
|
106
|
+
This corresponds to the following method call in Meteor:
|
107
|
+
|
108
|
+
```javascript
|
109
|
+
// Javascript
|
110
|
+
Meteor.call('postChatMessage', { inRoom: 'General' });
|
111
|
+
```
|
112
|
+
|
113
|
+
If you prefer the Meteor syntax, you can also call the method like this:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
meteor.call('postChatMessage', inRoom: 'General')
|
117
|
+
```
|
118
|
+
|
119
|
+
Note that you have to choose this syntax, if your Meteor method name collides with a Metybur method (like `collection` or `subscribe`).
|
120
|
+
|
121
|
+
### Logging
|
122
|
+
|
123
|
+
To debug your application, you can lower the log level to see all incoming websocket messages.
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
Metybur.log_level = :debug
|
127
|
+
```
|
128
|
+
|
129
|
+
Make sure to set the log level before calling `Metybur.connect`, as it won't have any effect afterwards.
|
130
|
+
|
131
|
+
## Contributing
|
132
|
+
|
133
|
+
1. Fork it ( https://github.com/clemenshelm/metybur/fork )
|
134
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
135
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
136
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
137
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
require_relative 'collection'
|
3
|
+
|
4
|
+
class Metybur::Client
|
5
|
+
def initialize(websocket)
|
6
|
+
@websocket = websocket
|
7
|
+
end
|
8
|
+
|
9
|
+
def subscribe(record_set_name)
|
10
|
+
message = {
|
11
|
+
msg: 'sub',
|
12
|
+
id: 'cde',
|
13
|
+
name: record_set_name
|
14
|
+
}.to_json
|
15
|
+
@websocket.send message
|
16
|
+
end
|
17
|
+
|
18
|
+
def collection(name)
|
19
|
+
Metybur::Collection.new(name, @websocket)
|
20
|
+
end
|
21
|
+
|
22
|
+
def call(method, params)
|
23
|
+
message = {
|
24
|
+
msg: 'method',
|
25
|
+
id: 'efg',
|
26
|
+
method: method,
|
27
|
+
params: params
|
28
|
+
}.to_json
|
29
|
+
@websocket.send message
|
30
|
+
end
|
31
|
+
|
32
|
+
def method_missing(method, *params)
|
33
|
+
method = method.to_s.camelize(:lower)
|
34
|
+
params.map! do |param|
|
35
|
+
case param
|
36
|
+
when Hash
|
37
|
+
param_array = param.map { |k, v| [k.to_s.camelize(:lower), v] }
|
38
|
+
Hash[param_array]
|
39
|
+
else
|
40
|
+
param
|
41
|
+
end
|
42
|
+
end
|
43
|
+
call(method, params)
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class Metybur::Collection
|
2
|
+
def initialize(collection_name, websocket)
|
3
|
+
websocket.on(:message) do |event|
|
4
|
+
attributes = JSON.parse(event.data, symbolize_names: true)
|
5
|
+
if attributes[:msg] == 'added' && attributes[:collection] == collection_name
|
6
|
+
@added_callback.call(attributes[:id], attributes[:fields])
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def on(event, &block)
|
12
|
+
@added_callback = block
|
13
|
+
end
|
14
|
+
end
|
data/lib/metybur.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require "metybur/version"
|
2
|
+
require 'faye/websocket'
|
3
|
+
require 'json'
|
4
|
+
require 'logger'
|
5
|
+
require_relative 'metybur/client'
|
6
|
+
|
7
|
+
module Metybur
|
8
|
+
CONFIG = {
|
9
|
+
websocket_client_class: Faye::WebSocket::Client,
|
10
|
+
log_level: Logger::INFO,
|
11
|
+
log_stream: STDOUT
|
12
|
+
}
|
13
|
+
|
14
|
+
def self.connect(url, credentials = {})
|
15
|
+
websocket = CONFIG[:websocket_client_class].new(url)
|
16
|
+
client = Metybur::Client.new(websocket)
|
17
|
+
|
18
|
+
logger = Logger.new(CONFIG[:log_stream])
|
19
|
+
logger.level = CONFIG[:log_level]
|
20
|
+
websocket.on(:open) do |event|
|
21
|
+
logger.debug 'connection open'
|
22
|
+
end
|
23
|
+
websocket.on(:message) do |message|
|
24
|
+
logger.debug "received message #{message.data}"
|
25
|
+
end
|
26
|
+
websocket.on(:close) do |event|
|
27
|
+
logger.debug "connection closed (code #{event.code}). #{event.reason}"
|
28
|
+
EM.stop_event_loop
|
29
|
+
end
|
30
|
+
|
31
|
+
websocket.on(:message) do |event|
|
32
|
+
message = JSON.parse(event.data, symbolize_names: true)
|
33
|
+
if message[:msg] == 'ping'
|
34
|
+
websocket.send({msg: 'pong'}.to_json)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
connect_message = {
|
39
|
+
msg: 'connect',
|
40
|
+
version: '1',
|
41
|
+
support: ['1']
|
42
|
+
}
|
43
|
+
websocket.send(connect_message.to_json)
|
44
|
+
|
45
|
+
password = credentials.delete(:password)
|
46
|
+
return client unless password
|
47
|
+
|
48
|
+
login_message = {
|
49
|
+
msg: 'method',
|
50
|
+
id: 'abc',
|
51
|
+
method: 'login',
|
52
|
+
params: [
|
53
|
+
{
|
54
|
+
user: credentials,
|
55
|
+
password: password
|
56
|
+
}
|
57
|
+
]
|
58
|
+
}
|
59
|
+
|
60
|
+
websocket.send(login_message.to_json)
|
61
|
+
|
62
|
+
client
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.websocket_client_class=(klass)
|
66
|
+
CONFIG[:websocket_client_class] = klass
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.log_level=(level_symbol)
|
70
|
+
upcase_symbol = level_symbol.to_s.upcase.to_sym
|
71
|
+
CONFIG[:log_level] = Logger.const_get(upcase_symbol)
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.log_stream=(io)
|
75
|
+
CONFIG[:log_stream] = io
|
76
|
+
end
|
77
|
+
end
|
data/metybur.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'metybur/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "metybur"
|
8
|
+
spec.version = Metybur::VERSION
|
9
|
+
spec.authors = ["Clemens Helm"]
|
10
|
+
spec.email = ["clemens.helm@gmail.com"]
|
11
|
+
spec.summary = 'DDP client for Ruby to connect to Meteor apps.'
|
12
|
+
spec.description = <<-description
|
13
|
+
Metybur lets your Ruby application connect to a Meteor app. It allows you
|
14
|
+
to subscribe to collections and to receive updates on them.
|
15
|
+
You can also call Meteor methods from Ruby.
|
16
|
+
description
|
17
|
+
spec.homepage = ""
|
18
|
+
spec.license = "MIT"
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0")
|
21
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
22
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_runtime_dependency "faye-websocket", "~> 0.9"
|
26
|
+
spec.add_runtime_dependency "activesupport", ">= 2.2.1"
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
29
|
+
spec.add_development_dependency "rake"
|
30
|
+
spec.add_development_dependency "rspec"
|
31
|
+
spec.add_development_dependency "ffaker"
|
32
|
+
spec.add_development_dependency "pry"
|
33
|
+
end
|
@@ -0,0 +1,222 @@
|
|
1
|
+
require_relative '../lib/metybur'
|
2
|
+
require_relative 'mocks/websocket_mock'
|
3
|
+
require 'ffaker'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
describe Metybur do
|
7
|
+
before :all do
|
8
|
+
Metybur.websocket_client_class = WebsocketMock
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:url) { FFaker::Internet.http_url }
|
12
|
+
let(:websocket) { WebsocketMock.instance }
|
13
|
+
|
14
|
+
def parse(string_data)
|
15
|
+
JSON.parse(string_data, symbolize_names: true)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'connects to a Meteor URL' do
|
19
|
+
Metybur.connect url
|
20
|
+
|
21
|
+
expect(websocket.url).to eq url
|
22
|
+
connect_message = parse(websocket.sent.first)
|
23
|
+
expect(connect_message)
|
24
|
+
.to eq msg: 'connect',
|
25
|
+
version: '1',
|
26
|
+
support: ['1']
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'logging in' do
|
30
|
+
it 'calls the login method with email and password' do
|
31
|
+
email = FFaker::Internet.email
|
32
|
+
password = FFaker::Internet.password
|
33
|
+
|
34
|
+
Metybur.connect url, email: email, password: password
|
35
|
+
|
36
|
+
login_message = parse(websocket.sent.last)
|
37
|
+
expect(login_message[:msg]).to eq 'method'
|
38
|
+
expect(login_message).to have_key :id # we don't care about the value here
|
39
|
+
expect(login_message[:method]).to eq 'login'
|
40
|
+
expect(login_message[:params][0])
|
41
|
+
.to eq user: { email: email },
|
42
|
+
password: password
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'calls the login method with username and password' do
|
46
|
+
username = FFaker::Internet.user_name
|
47
|
+
password = FFaker::Internet.password
|
48
|
+
|
49
|
+
Metybur.connect url, username: username, password: password
|
50
|
+
|
51
|
+
login_message = parse(websocket.sent.last)
|
52
|
+
expect(login_message[:msg]).to eq 'method'
|
53
|
+
expect(login_message).to have_key :id # we don't care about the value here
|
54
|
+
expect(login_message[:method]).to eq 'login'
|
55
|
+
expect(login_message[:params][0])
|
56
|
+
.to eq user: { username: username },
|
57
|
+
password: password
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'calls the login method with user id and password' do
|
61
|
+
userid = FFaker::Guid.guid
|
62
|
+
password = FFaker::Internet.password
|
63
|
+
|
64
|
+
Metybur.connect url, id: userid, password: password
|
65
|
+
|
66
|
+
login_message = parse(websocket.sent.last)
|
67
|
+
expect(login_message[:msg]).to eq 'method'
|
68
|
+
expect(login_message).to have_key :id # we don't care about the value here
|
69
|
+
expect(login_message[:method]).to eq 'login'
|
70
|
+
expect(login_message[:params][0])
|
71
|
+
.to eq user: { id: userid },
|
72
|
+
password: password
|
73
|
+
end
|
74
|
+
|
75
|
+
it "doesn't log in without credentials" do
|
76
|
+
Metybur.connect url
|
77
|
+
|
78
|
+
last_message = parse(websocket.sent.last)
|
79
|
+
expect(last_message[:msg]).to eq 'connect'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'ping pong' do
|
84
|
+
it 'responds with pong to a ping' do
|
85
|
+
Metybur.connect url
|
86
|
+
|
87
|
+
websocket.receive({msg: 'ping'}.to_json)
|
88
|
+
|
89
|
+
last_message = parse(websocket.sent.last)
|
90
|
+
expect(last_message[:msg]).to eq 'pong'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
context 'logging' do
|
96
|
+
it "doesn't log any messages by default" do
|
97
|
+
output = StringIO.new
|
98
|
+
Metybur.log_stream = output
|
99
|
+
Metybur.connect url
|
100
|
+
|
101
|
+
websocket.receive({msg: 'logged_message'}.to_json)
|
102
|
+
|
103
|
+
expect(output.string).to be_empty
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'logs a message when the log level is set to debug' do
|
107
|
+
output = StringIO.new
|
108
|
+
Metybur.log_level = :debug
|
109
|
+
Metybur.log_stream = output
|
110
|
+
Metybur.connect url
|
111
|
+
|
112
|
+
websocket.receive({msg: 'logged_message'}.to_json)
|
113
|
+
|
114
|
+
expect(output.string).not_to be_empty
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
context 'subscription' do
|
119
|
+
it 'subscribes to a published record set' do
|
120
|
+
record_set = FFaker::Internet.user_name
|
121
|
+
|
122
|
+
meteor = Metybur.connect url
|
123
|
+
meteor.subscribe(record_set)
|
124
|
+
|
125
|
+
last_message = parse(websocket.sent.last)
|
126
|
+
expect(last_message[:msg]).to eq 'sub'
|
127
|
+
expect(last_message).to have_key :id # we don't care about the value here
|
128
|
+
expect(last_message[:name]).to eq record_set
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context 'collections' do
|
133
|
+
it 'gets notified when a record is added' do
|
134
|
+
collection = FFaker::Internet.user_name
|
135
|
+
callback_called = false
|
136
|
+
|
137
|
+
id = FFaker::Guid.guid
|
138
|
+
fields = {city: FFaker::Address.city}
|
139
|
+
|
140
|
+
meteor = Metybur.connect url
|
141
|
+
meteor.collection(collection)
|
142
|
+
.on(:added) do |added_id, added_fields|
|
143
|
+
callback_called = true
|
144
|
+
expect(added_id).to eq id
|
145
|
+
expect(added_fields).to eq fields
|
146
|
+
end
|
147
|
+
|
148
|
+
message = {
|
149
|
+
msg: 'added',
|
150
|
+
collection: collection,
|
151
|
+
id: id,
|
152
|
+
fields: fields
|
153
|
+
}.to_json
|
154
|
+
websocket.receive message
|
155
|
+
|
156
|
+
fail("Callback didn't get called.") unless callback_called
|
157
|
+
end
|
158
|
+
|
159
|
+
it "doesn't get notified of a ping message" do
|
160
|
+
meteor = Metybur.connect(url)
|
161
|
+
meteor.collection('my-collection')
|
162
|
+
.on(:added) { fail('Callback got called') }
|
163
|
+
|
164
|
+
websocket.receive({msg: 'ping'}.to_json)
|
165
|
+
end
|
166
|
+
|
167
|
+
it "doesn't get notified of a record from another collection" do
|
168
|
+
meteor = Metybur.connect(url)
|
169
|
+
meteor.collection('my-collection')
|
170
|
+
.on(:added) { fail('Callback got called') }
|
171
|
+
|
172
|
+
message = {
|
173
|
+
msg: 'added',
|
174
|
+
collection: 'another-collection',
|
175
|
+
id: 'xyz',
|
176
|
+
fields: {country: 'Belarus'}
|
177
|
+
}.to_json
|
178
|
+
websocket.receive message
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
context 'methods' do
|
183
|
+
it 'calls a method through the call method' do
|
184
|
+
method = %w(postChatMessage sendEmail submitOrder).sample
|
185
|
+
params = %w(35 Vienna true).sample(2)
|
186
|
+
hashParams = {emailAddress: 'myemail@example.com', myMessage: 'Alright!', userId: 'rtnilctrniae'}
|
187
|
+
.to_a.sample(2)
|
188
|
+
params << Hash[hashParams]
|
189
|
+
|
190
|
+
meteor = Metybur.connect(url)
|
191
|
+
meteor.call(method, params)
|
192
|
+
|
193
|
+
last_message = parse(websocket.sent.last)
|
194
|
+
expect(last_message[:msg]).to eq 'method'
|
195
|
+
expect(last_message[:method]).to eq method
|
196
|
+
expect(last_message).to have_key :id # we don't care about the value here
|
197
|
+
expect(last_message[:params]).to eq params
|
198
|
+
end
|
199
|
+
|
200
|
+
it 'calls a method called on the client directly' do
|
201
|
+
meteor = Metybur.connect(url)
|
202
|
+
meteor.activate('user', id: 'utrtrvlc')
|
203
|
+
|
204
|
+
last_message = parse(websocket.sent.last)
|
205
|
+
expect(last_message[:msg]).to eq 'method'
|
206
|
+
expect(last_message[:method]).to eq 'activate'
|
207
|
+
expect(last_message).to have_key :id # we don't care about the value here
|
208
|
+
expect(last_message[:params]).to eq ['user', {id: 'utrtrvlc'}]
|
209
|
+
end
|
210
|
+
|
211
|
+
it 'camel-cases methods and parameters called on the client directly' do
|
212
|
+
meteor = Metybur.connect(url)
|
213
|
+
meteor.activate_user('Hans', user_id: 'utrtrvlc', is_admin: false)
|
214
|
+
|
215
|
+
last_message = parse(websocket.sent.last)
|
216
|
+
expect(last_message[:msg]).to eq 'method'
|
217
|
+
expect(last_message[:method]).to eq 'activateUser'
|
218
|
+
expect(last_message).to have_key :id # we don't care about the value here
|
219
|
+
expect(last_message[:params]).to eq ['Hans', {userId: 'utrtrvlc', isAdmin: false}]
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class WebsocketMock
|
2
|
+
class << self
|
3
|
+
attr_accessor :instance
|
4
|
+
end
|
5
|
+
|
6
|
+
MessageEvent = Struct.new(:data)
|
7
|
+
|
8
|
+
attr_reader :url, :sent
|
9
|
+
|
10
|
+
def initialize(url)
|
11
|
+
WebsocketMock.instance = self
|
12
|
+
@url = url
|
13
|
+
@sent = []
|
14
|
+
@message_handlers = []
|
15
|
+
end
|
16
|
+
|
17
|
+
def on(event, &handler)
|
18
|
+
if event == :message
|
19
|
+
@message_handlers << handler
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def send(string_data)
|
24
|
+
@sent << string_data
|
25
|
+
end
|
26
|
+
|
27
|
+
def receive(string_data)
|
28
|
+
event = MessageEvent.new(string_data)
|
29
|
+
@message_handlers.each { |handler| handler.call(event) }
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
RSpec.configure do |config|
|
20
|
+
# rspec-expectations config goes here. You can use an alternate
|
21
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
22
|
+
# assertions if you prefer.
|
23
|
+
config.expect_with :rspec do |expectations|
|
24
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
25
|
+
# and `failure_message` of custom matchers include text for helper methods
|
26
|
+
# defined using `chain`, e.g.:
|
27
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
28
|
+
# # => "be bigger than 2 and smaller than 4"
|
29
|
+
# ...rather than:
|
30
|
+
# # => "be bigger than 2"
|
31
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
35
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
36
|
+
config.mock_with :rspec do |mocks|
|
37
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
38
|
+
# a real object. This is generally recommended, and will default to
|
39
|
+
# `true` in RSpec 4.
|
40
|
+
mocks.verify_partial_doubles = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# The settings below are suggested to provide a good initial experience
|
44
|
+
# with RSpec, but feel free to customize to your heart's content.
|
45
|
+
=begin
|
46
|
+
# These two settings work together to allow you to limit a spec run
|
47
|
+
# to individual examples or groups you care about by tagging them with
|
48
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
49
|
+
# get run.
|
50
|
+
config.filter_run :focus
|
51
|
+
config.run_all_when_everything_filtered = true
|
52
|
+
|
53
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
54
|
+
# recommended. For more details, see:
|
55
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
56
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
57
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
58
|
+
config.disable_monkey_patching!
|
59
|
+
|
60
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
61
|
+
# be too noisy due to issues in dependencies.
|
62
|
+
config.warnings = true
|
63
|
+
|
64
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
65
|
+
# file, and it's useful to allow more verbose output when running an
|
66
|
+
# individual spec file.
|
67
|
+
if config.files_to_run.one?
|
68
|
+
# Use the documentation formatter for detailed output,
|
69
|
+
# unless a formatter has already been configured
|
70
|
+
# (e.g. via a command-line flag).
|
71
|
+
config.default_formatter = 'doc'
|
72
|
+
end
|
73
|
+
|
74
|
+
# Print the 10 slowest examples and example groups at the
|
75
|
+
# end of the spec run, to help surface which specs are running
|
76
|
+
# particularly slow.
|
77
|
+
config.profile_examples = 10
|
78
|
+
|
79
|
+
# Run specs in random order to surface order dependencies. If you find an
|
80
|
+
# order dependency and want to debug it, you can fix the order by providing
|
81
|
+
# the seed, which is printed after each run.
|
82
|
+
# --seed 1234
|
83
|
+
config.order = :random
|
84
|
+
|
85
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
86
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
87
|
+
# test failures related to randomization by passing the same `--seed` value
|
88
|
+
# as the one that triggered the failure.
|
89
|
+
Kernel.srand config.seed
|
90
|
+
=end
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: metybur
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Clemens Helm
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faye-websocket
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.9'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.2.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.2.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.6'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.6'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: ffaker
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: |2
|
112
|
+
Metybur lets your Ruby application connect to a Meteor app. It allows you
|
113
|
+
to subscribe to collections and to receive updates on them.
|
114
|
+
You can also call Meteor methods from Ruby.
|
115
|
+
email:
|
116
|
+
- clemens.helm@gmail.com
|
117
|
+
executables: []
|
118
|
+
extensions: []
|
119
|
+
extra_rdoc_files: []
|
120
|
+
files:
|
121
|
+
- ".gitignore"
|
122
|
+
- ".rspec"
|
123
|
+
- ".travis.yml"
|
124
|
+
- Gemfile
|
125
|
+
- LICENSE.txt
|
126
|
+
- README.md
|
127
|
+
- Rakefile
|
128
|
+
- lib/metybur.rb
|
129
|
+
- lib/metybur/client.rb
|
130
|
+
- lib/metybur/collection.rb
|
131
|
+
- lib/metybur/version.rb
|
132
|
+
- metybur.gemspec
|
133
|
+
- spec/metybur_spec.rb
|
134
|
+
- spec/mocks/websocket_mock.rb
|
135
|
+
- spec/spec_helper.rb
|
136
|
+
homepage: ''
|
137
|
+
licenses:
|
138
|
+
- MIT
|
139
|
+
metadata: {}
|
140
|
+
post_install_message:
|
141
|
+
rdoc_options: []
|
142
|
+
require_paths:
|
143
|
+
- lib
|
144
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - ">="
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
requirements: []
|
155
|
+
rubyforge_project:
|
156
|
+
rubygems_version: 2.4.5
|
157
|
+
signing_key:
|
158
|
+
specification_version: 4
|
159
|
+
summary: DDP client for Ruby to connect to Meteor apps.
|
160
|
+
test_files:
|
161
|
+
- spec/metybur_spec.rb
|
162
|
+
- spec/mocks/websocket_mock.rb
|
163
|
+
- spec/spec_helper.rb
|