mqtt_pipe 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.
- checksums.yaml +4 -4
- data/examples/receiver.rb +3 -3
- data/examples/sender.rb +4 -0
- data/lib/mqtt_pipe/pipe.rb +30 -6
- data/lib/mqtt_pipe/version.rb +1 -1
- data/lib/mqtt_pipe.rb +0 -1
- data/spec/mqtt_pipe_spec.rb +9 -0
- data/spec/pipe_spec.rb +49 -4
- metadata +4 -5
- data/lib/mqtt_pipe/config.rb +0 -31
- data/spec/config_spec.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69a8164675982ac052fefb772507fd26f96eac61
|
4
|
+
data.tar.gz: 6d001fffc5f2007191349ff736d4c3309e716813
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e530f0ca6aa01fd40858e35993b038b9daa552ef7eb510bff04c9dde3b7b9150b6201bef7d5131183e1bfe369c2fa527348c90e94af38cd0d948b57e576ce79
|
7
|
+
data.tar.gz: 908eb4630804e017663dd91d2cfc299433fcbcc9f026a6284e023b0cb95ca3f1856fc297a34b5d50470c0b57f805fb369d6a66484d51fb4d71da35ccfdc36151
|
data/examples/receiver.rb
CHANGED
data/examples/sender.rb
CHANGED
data/lib/mqtt_pipe/pipe.rb
CHANGED
@@ -10,10 +10,35 @@ module MQTTPipe
|
|
10
10
|
|
11
11
|
class ConnectionError < StandardError; end
|
12
12
|
|
13
|
+
##
|
14
|
+
# Create a new pipe and optionally yield to a block
|
15
|
+
|
16
|
+
def initialize &block
|
17
|
+
@listeners = []
|
18
|
+
instance_eval &block unless block.nil?
|
19
|
+
end
|
20
|
+
|
21
|
+
##
|
22
|
+
# Subscribe to a topic and attatch an action that will
|
23
|
+
# be called once a message with a matching topic is
|
24
|
+
# received.
|
25
|
+
|
26
|
+
def on topic, &action
|
27
|
+
raise ArgumentError, 'No block given' if action.nil?
|
28
|
+
@listeners << Listener.new(topic, &action)
|
29
|
+
end
|
30
|
+
|
31
|
+
##
|
32
|
+
# Subscribe to all topics
|
33
|
+
|
34
|
+
def on_anything &action
|
35
|
+
on '#', &action
|
36
|
+
end
|
37
|
+
|
38
|
+
alias_method :on_everything, :on_anything
|
13
39
|
|
14
|
-
def
|
15
|
-
@
|
16
|
-
@config.instance_eval &block unless block.nil?
|
40
|
+
def topics
|
41
|
+
@listeners.map{|listener| listener.topic }
|
17
42
|
end
|
18
43
|
|
19
44
|
##
|
@@ -23,16 +48,15 @@ module MQTTPipe
|
|
23
48
|
MQTT::Client.connect host: host, port: port do |client|
|
24
49
|
|
25
50
|
# Subscribe
|
26
|
-
topics = @config.listeners.map{|listener| listener.topic }
|
27
51
|
listener_thread = nil
|
28
52
|
|
29
|
-
unless
|
53
|
+
unless @listeners.empty?
|
30
54
|
listener_thread = Thread.new do
|
31
55
|
client.get do |topic, data|
|
32
56
|
begin
|
33
57
|
unpacked_data = Packer.unpack data
|
34
58
|
|
35
|
-
@
|
59
|
+
@listeners.each do |listener|
|
36
60
|
if m = listener.match(topic)
|
37
61
|
listener.call unpacked_data, *m
|
38
62
|
end
|
data/lib/mqtt_pipe/version.rb
CHANGED
data/lib/mqtt_pipe.rb
CHANGED
data/spec/pipe_spec.rb
CHANGED
@@ -1,9 +1,54 @@
|
|
1
1
|
require 'mqtt_pipe'
|
2
2
|
|
3
|
-
describe MQTTPipe do
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
describe MQTTPipe::Pipe do
|
4
|
+
let(:klass) { MQTTPipe::Pipe }
|
5
|
+
|
6
|
+
describe '#new' do
|
7
|
+
it 'accepts a block that evaluates within the context of the pipe' do
|
8
|
+
tester = self
|
9
|
+
pipe_inside = nil
|
10
|
+
pipe_outside = MQTTPipe::Pipe.new do
|
11
|
+
tester.expect(self).to tester.be_a tester.klass
|
12
|
+
pipe_inside = self
|
13
|
+
end
|
14
|
+
|
15
|
+
expect(pipe_inside).to be pipe_outside
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'using a pipe' do
|
20
|
+
before :each do
|
21
|
+
@pipe = klass.new
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#on' do
|
25
|
+
it 'requires one argument and a block' do
|
26
|
+
expect{@pipe.on}.to raise_error ArgumentError
|
27
|
+
expect{@pipe.on 'test'}.to raise_error ArgumentError
|
28
|
+
expect{@pipe.on('test') {}}.not_to raise_error
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#on_anything' do
|
33
|
+
it 'does not accept a topic' do
|
34
|
+
expect{@pipe.on_anything('test') {}}.to raise_error ArgumentError
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#topics' do
|
39
|
+
it 'stores the topics that have beed subscribed to' do
|
40
|
+
expect(@pipe.topics.empty?).to be true
|
41
|
+
|
42
|
+
@pipe.on('test/1') {}
|
43
|
+
|
44
|
+
expect(@pipe.topics.length).to be 1
|
45
|
+
expect(@pipe.topics.first).to eq 'test/1'
|
46
|
+
|
47
|
+
@pipe.on_anything {}
|
48
|
+
|
49
|
+
expect(@pipe.topics.length).to be 2
|
50
|
+
expect(@pipe.topics.last).to eq '#'
|
51
|
+
end
|
7
52
|
end
|
8
53
|
end
|
9
54
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mqtt_pipe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sebastian Lindberg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mqtt
|
@@ -82,7 +82,6 @@ files:
|
|
82
82
|
- examples/receiver.rb
|
83
83
|
- examples/sender.rb
|
84
84
|
- lib/mqtt_pipe.rb
|
85
|
-
- lib/mqtt_pipe/config.rb
|
86
85
|
- lib/mqtt_pipe/listener.rb
|
87
86
|
- lib/mqtt_pipe/packer.rb
|
88
87
|
- lib/mqtt_pipe/pipe.rb
|
@@ -100,8 +99,8 @@ files:
|
|
100
99
|
- lib/mqtt_pipe/types/type.rb
|
101
100
|
- lib/mqtt_pipe/version.rb
|
102
101
|
- mqtt_pipe.gemspec
|
103
|
-
- spec/config_spec.rb
|
104
102
|
- spec/listener_spec.rb
|
103
|
+
- spec/mqtt_pipe_spec.rb
|
105
104
|
- spec/packer_spec.rb
|
106
105
|
- spec/pipe_spec.rb
|
107
106
|
homepage: https://github.com/seblindberg/ruby-mqtt-pipe
|
@@ -129,7 +128,7 @@ signing_key:
|
|
129
128
|
specification_version: 4
|
130
129
|
summary: A gem for sending a small set of objects via MQTT.
|
131
130
|
test_files:
|
132
|
-
- spec/config_spec.rb
|
133
131
|
- spec/listener_spec.rb
|
132
|
+
- spec/mqtt_pipe_spec.rb
|
134
133
|
- spec/packer_spec.rb
|
135
134
|
- spec/pipe_spec.rb
|
data/lib/mqtt_pipe/config.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
module MQTTPipe
|
2
|
-
|
3
|
-
##
|
4
|
-
# An instance of Config is used as the context in which
|
5
|
-
# the pipe is configured.
|
6
|
-
|
7
|
-
class Config
|
8
|
-
attr_reader :listeners
|
9
|
-
|
10
|
-
def initialize
|
11
|
-
@listeners = []
|
12
|
-
end
|
13
|
-
|
14
|
-
##
|
15
|
-
# Subscribe to a topic and attatch an action that will
|
16
|
-
# be called once a message with a matching topic is
|
17
|
-
# received.
|
18
|
-
|
19
|
-
def on topic, &action
|
20
|
-
raise ArgumentError, 'No block given' if action.nil?
|
21
|
-
@listeners << Listener.new(topic, &action)
|
22
|
-
end
|
23
|
-
|
24
|
-
##
|
25
|
-
# Subscribe to all topics
|
26
|
-
|
27
|
-
def on_anything &action
|
28
|
-
on '#', &action
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
data/spec/config_spec.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'mqtt_pipe'
|
2
|
-
|
3
|
-
describe MQTTPipe::Config do
|
4
|
-
let(:klass) { MQTTPipe::Config }
|
5
|
-
|
6
|
-
before :each do
|
7
|
-
@config = klass.new
|
8
|
-
end
|
9
|
-
|
10
|
-
describe '#on' do
|
11
|
-
it 'requires one argument and a block' do
|
12
|
-
expect{@config.on}.to raise_error(ArgumentError)
|
13
|
-
expect{@config.on 'test'}.to raise_error(ArgumentError)
|
14
|
-
expect{@config.on('test') {}}.not_to raise_error
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'stores the listener' do
|
18
|
-
expect(@config.listeners.length).to eq(0)
|
19
|
-
|
20
|
-
@config.on('test') {}
|
21
|
-
|
22
|
-
expect(@config.listeners.length).to eq(1)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
|
27
|
-
end
|