mqtt_pipe 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93caadb951ff60325a712d56794e4431adbcf621
4
- data.tar.gz: b9a8f6b3b8a31d2925601444bad95bb054e00c5e
3
+ metadata.gz: 69a8164675982ac052fefb772507fd26f96eac61
4
+ data.tar.gz: 6d001fffc5f2007191349ff736d4c3309e716813
5
5
  SHA512:
6
- metadata.gz: bf62b6c097b52d0770b64b5bf63dc299a399120a525eb4682b03a92f74d74ce3734fe6221c6008c9620774d4c3c28e009b4196d1a0492f822886fbe83b53e645
7
- data.tar.gz: 9e8cf7c06b4e8a625bff2944337bbf90bb8b180f16e898d1127417a47fc34641f030baec5fb0f0e860023eed030f268bfde664a318ed4de36695843efc30e292
6
+ metadata.gz: 7e530f0ca6aa01fd40858e35993b038b9daa552ef7eb510bff04c9dde3b7b9150b6201bef7d5131183e1bfe369c2fa527348c90e94af38cd0d948b57e576ce79
7
+ data.tar.gz: 908eb4630804e017663dd91d2cfc299433fcbcc9f026a6284e023b0cb95ca3f1856fc297a34b5d50470c0b57f805fb369d6a66484d51fb4d71da35ccfdc36151
data/examples/receiver.rb CHANGED
@@ -4,6 +4,6 @@ pipe = MQTTPipe.create do
4
4
  on 'hello/world/#' do |message, id|
5
5
  p message, id
6
6
  end
7
- end
8
-
9
- pipe.open 'test.mosquitto.org', port: 1883
7
+
8
+ open 'test.mosquitto.org', port: 1883
9
+ end
data/examples/sender.rb CHANGED
@@ -2,6 +2,10 @@ require 'mqtt_pipe'
2
2
 
3
3
  pipe = MQTTPipe.create
4
4
 
5
+ pipe.on 'hello/world/5' do
6
+ puts 'received 5'
7
+ end
8
+
5
9
  pipe.open 'test.mosquitto.org', port: 1883 do
6
10
  counter = 0
7
11
  loop do
@@ -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 initialize &block
15
- @config = Config.new
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 topics.empty?
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
- @config.listeners.each do |listener|
59
+ @listeners.each do |listener|
36
60
  if m = listener.match(topic)
37
61
  listener.call unpacked_data, *m
38
62
  end
@@ -1,3 +1,3 @@
1
1
  module MQTTPipe
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/mqtt_pipe.rb CHANGED
@@ -3,7 +3,6 @@ require 'mqtt'
3
3
  require 'mqtt_pipe/version'
4
4
  require 'mqtt_pipe/types'
5
5
  require 'mqtt_pipe/packer'
6
- require 'mqtt_pipe/config'
7
6
  require 'mqtt_pipe/listener'
8
7
  require 'mqtt_pipe/pipe'
9
8
 
@@ -0,0 +1,9 @@
1
+ require 'mqtt_pipe'
2
+
3
+ describe MQTTPipe do
4
+ describe '#create' do
5
+ it 'returns a pipe' do
6
+ expect(MQTTPipe.create).to be_a MQTTPipe::Pipe
7
+ end
8
+ end
9
+ end
data/spec/pipe_spec.rb CHANGED
@@ -1,9 +1,54 @@
1
1
  require 'mqtt_pipe'
2
2
 
3
- describe MQTTPipe do
4
- describe '#create' do
5
- it 'returns a pipe' do
6
- expect(MQTTPipe.create).to be_a MQTTPipe::Pipe
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.1
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-26 00:00:00.000000000 Z
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
@@ -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