mosca 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +15 -9
- data/lib/command_builder.rb +4 -4
- data/lib/mosca/client.rb +22 -23
- data/lib/mosca/version.rb +1 -1
- data/spec/mosca_spec.rb +33 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb5e5ed870c959777ce1d362f9d20d12df283979
|
4
|
+
data.tar.gz: b9cc31952645221be2a57aff70a0668622da5d04
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2eea2347c5c2ea015ed02a99e8ecf51bb9cb685dc0565a506992bb7eff13511233213aa8b1b1b39a27a499169cf5194ab449c689e0a4185358e039773ea962d3
|
7
|
+
data.tar.gz: 4fa10abd1bc6c7cfed3f6419543ac35ceb7d7dd98c347d65d1e87e80af9f9705fdfc6a8b14f0e3fe2e92231df85faa2240cb7bbbe107fdb0f25f7553aa11ef7a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,4 @@
|
|
1
|
-
[![Version ](https://img.shields.io/gem/v/mosca.svg)](https://rubygems.org/gems/mosca)
|
2
|
-
[![Dependency Status](https://gemnasium.com/ionia-corporation/mosca.svg)](https://gemnasium.com/ionia-corporation/mosca)
|
3
|
-
[![Code Climate](https://codeclimate.com/github/ionia-corporation/mosca.png)](https://codeclimate.com/github/ionia-corporation/mosca)
|
4
|
-
[![Build Status](https://img.shields.io/travis/ionia-corporation/mosca/master.svg)](https://travis-ci.org/ionia-corporation/mosca)
|
5
|
-
[![Coverage ](https://img.shields.io/codeclimate/coverage/github/ionia-corporation/mosca.svg)](https://codeclimate.com/github/ionia-corporation/mosca)
|
1
|
+
[![Version ](https://img.shields.io/gem/v/mosca.svg)](https://rubygems.org/gems/mosca) [![Dependency Status](https://gemnasium.com/ionia-corporation/mosca.svg)](https://gemnasium.com/ionia-corporation/mosca) [![Code Climate](https://codeclimate.com/github/ionia-corporation/mosca.png)](https://codeclimate.com/github/ionia-corporation/mosca) [![Build Status](https://img.shields.io/travis/ionia-corporation/mosca/master.svg)](https://travis-ci.org/ionia-corporation/mosca) [![Coverage ](https://img.shields.io/codeclimate/coverage/github/ionia-corporation/mosca.svg)](https://codeclimate.com/github/ionia-corporation/mosca)
|
6
2
|
|
7
3
|
|
8
4
|
# Mosca
|
@@ -22,7 +18,7 @@ And then execute:
|
|
22
18
|
Or install it yourself as:
|
23
19
|
|
24
20
|
$ gem install mosca
|
25
|
-
|
21
|
+
|
26
22
|
## Usage
|
27
23
|
|
28
24
|
### Configure
|
@@ -34,6 +30,15 @@ You can configure the default timeout for response, and default mqtt broker.
|
|
34
30
|
Mosca::Client.default_broker = "test.mosquitto.org"
|
35
31
|
```
|
36
32
|
|
33
|
+
#### Environment variables
|
34
|
+
|
35
|
+
The following environment variables are used when creating clients, so you can use something like dotenv to put them on a file:
|
36
|
+
|
37
|
+
MOSCA_BROKER
|
38
|
+
MOSCA_USER
|
39
|
+
MOSCA_PASS
|
40
|
+
MOSCA_TIMEOUT
|
41
|
+
|
37
42
|
### New instance
|
38
43
|
|
39
44
|
```ruby
|
@@ -45,7 +50,8 @@ You can configure the default timeout for response, and default mqtt broker.
|
|
45
50
|
#### Single message
|
46
51
|
|
47
52
|
```ruby
|
48
|
-
client.publish "restart" # will be sent to topic /device/commands
|
53
|
+
client.publish "restart" # will be sent to topic /device/commands. Returns nil if timed out.
|
54
|
+
client.publish! "restart" # Raises Timeout::Error if timed out.
|
49
55
|
```
|
50
56
|
|
51
57
|
#### Message with response
|
@@ -58,9 +64,9 @@ You can configure the default timeout for response, and default mqtt broker.
|
|
58
64
|
### Getting messages
|
59
65
|
|
60
66
|
```ruby
|
61
|
-
puts client.get # will wait up to Mosca.default_timeout (default 5) seconds. will return if no response comes.
|
62
|
-
|
67
|
+
puts client.get # will wait up to Mosca.default_timeout (default 5) seconds. will return nil if no response comes.
|
63
68
|
puts client.get timeout: 2, topic_in: "another_topic" # will wait up to 2 seconds for a response on the another_topic topic.
|
69
|
+
client.get! # Raises exception if timed out
|
64
70
|
```
|
65
71
|
|
66
72
|
###TO DO
|
data/lib/command_builder.rb
CHANGED
@@ -4,7 +4,7 @@ module CommandBuilder
|
|
4
4
|
public
|
5
5
|
|
6
6
|
def method_missing(method, *args)
|
7
|
-
json = full_hash(method, args
|
7
|
+
json = full_hash(method, *args).to_json
|
8
8
|
client.publish json
|
9
9
|
end
|
10
10
|
|
@@ -28,9 +28,9 @@ module CommandBuilder
|
|
28
28
|
hash
|
29
29
|
end
|
30
30
|
|
31
|
-
def full_hash method, args
|
32
|
-
command_default = process_command(method, args)
|
33
|
-
command_specific = specific_hash(method, args)
|
31
|
+
def full_hash method, *args
|
32
|
+
command_default = process_command(method, *args)
|
33
|
+
command_specific = specific_hash(method, *args)
|
34
34
|
default_hash.merge(command_default).merge(command_specific)
|
35
35
|
end
|
36
36
|
|
data/lib/mosca/client.rb
CHANGED
@@ -3,31 +3,40 @@ require 'json'
|
|
3
3
|
require 'mosca/exceptions'
|
4
4
|
|
5
5
|
module Mosca
|
6
|
-
|
7
6
|
class Client
|
8
|
-
|
9
7
|
class << self
|
10
8
|
attr_accessor :default_broker, :default_timeout
|
11
9
|
end
|
12
10
|
|
13
11
|
self.default_broker = ENV["MOSCA_BROKER"] || "test.mosquitto.org"
|
14
|
-
self.default_timeout = 5
|
12
|
+
self.default_timeout = 5 || ENV["MOSCA_TIMEOUT"]
|
15
13
|
|
16
14
|
attr_accessor :user, :pass, :topic_in, :topic_out, :broker, :topic_base, :client
|
17
15
|
|
18
16
|
def initialize args = {}
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
@user = args[:user] || ENV["MOSCA_USER"]
|
18
|
+
@pass = args[:user] || ENV["MOSCA_PASS"]
|
19
|
+
@topic_in = args[:topic_in]
|
20
|
+
@topic_out = args[:topic_out]
|
21
|
+
@topic_base = args[:topic_base] || ""
|
22
|
+
@broker = args[:broker] || ENV["MOSCA_BROKER"] || self.class.default_broker
|
23
|
+
@client = args[:client] || MQTT::Client
|
24
|
+
end
|
25
|
+
|
26
|
+
def publish! message, params = {}
|
27
|
+
timeout(params) do
|
28
|
+
topic_out = params[:topic_out] || params[:topic] || @topic_out || Exceptions.raise_missing_topic
|
29
|
+
topic_in = params[:topic_in] || @topic_in
|
30
|
+
connection.subscribe full_topic(topic_in) if params[:response]
|
31
|
+
connection.publish full_topic(topic_out), message
|
32
|
+
params[:response] ? get(params) : message
|
22
33
|
end
|
23
34
|
end
|
24
35
|
|
25
|
-
def publish
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
connection.publish full_topic(topic_out), json
|
30
|
-
get(params) if params[:response]
|
36
|
+
def publish message, params = {}
|
37
|
+
publish! message, params
|
38
|
+
rescue Timeout::Error
|
39
|
+
nil
|
31
40
|
end
|
32
41
|
|
33
42
|
def get! params = {}
|
@@ -51,16 +60,6 @@ module Mosca
|
|
51
60
|
|
52
61
|
private
|
53
62
|
|
54
|
-
def default
|
55
|
-
{ topic_base: "",
|
56
|
-
broker: self.class.default_broker,
|
57
|
-
client: MQTT::Client }
|
58
|
-
end
|
59
|
-
|
60
|
-
def attributes
|
61
|
-
[:user, :pass, :topic_in, :topic_out, :topic_base, :broker, :client]
|
62
|
-
end
|
63
|
-
|
64
63
|
def client_options
|
65
64
|
{remote_host: @broker, username: @user, password: @pass}
|
66
65
|
end
|
@@ -80,7 +79,7 @@ module Mosca
|
|
80
79
|
end
|
81
80
|
|
82
81
|
def timeout params
|
83
|
-
timeout = params[:timeout] || self.class.default_timeout
|
82
|
+
timeout = params[:timeout] || ENV["MOSCA_TIMEOUT"].to_i || self.class.default_timeout
|
84
83
|
Timeout.timeout(timeout) do
|
85
84
|
yield
|
86
85
|
end
|
data/lib/mosca/version.rb
CHANGED
data/spec/mosca_spec.rb
CHANGED
@@ -25,9 +25,9 @@ describe Mosca::Client do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
describe "publishing" do
|
28
|
-
it "uses topic_out to publish if it was specified,
|
28
|
+
it "uses topic_out to publish if it was specified, publishes the desired message and returns the message when successfully published" do
|
29
29
|
expect(client).to receive(:publish).with(OUT,MESSAGE)
|
30
|
-
mosca.publish MESSAGE
|
30
|
+
expect(mosca.publish MESSAGE).to eq MESSAGE
|
31
31
|
end
|
32
32
|
|
33
33
|
it "can take a topic_out as argument when publishing" do
|
@@ -123,6 +123,37 @@ describe Mosca::Client do
|
|
123
123
|
it "get returns nil" do
|
124
124
|
expect(mosca.get).to be nil
|
125
125
|
end
|
126
|
+
|
127
|
+
it "publish returns nil" do
|
128
|
+
expect(mosca.publish).to be nil
|
129
|
+
end
|
130
|
+
|
131
|
+
it "get! raises the timed out exception" do
|
132
|
+
expect(mosca.get!).to raise_error
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe "Environment variables" do
|
138
|
+
it "can set MOSCA_BROKER" do
|
139
|
+
ENV["MOSCA_BROKER"] = "broker"
|
140
|
+
expect(described_class.new.broker).to eq "broker"
|
141
|
+
end
|
142
|
+
|
143
|
+
it "can set MOSCA_USER" do
|
144
|
+
ENV["MOSCA_USER"] = "USER"
|
145
|
+
expect(described_class.new.user).to eq "USER"
|
146
|
+
end
|
147
|
+
|
148
|
+
it "can set MOSCA_PASS" do
|
149
|
+
ENV["MOSCA_PASS"] = "PASS"
|
150
|
+
expect(described_class.new.pass).to eq "PASS"
|
151
|
+
end
|
152
|
+
|
153
|
+
it "can set MOSCA_TIMEOUT" do
|
154
|
+
ENV["MOSCA_TIMEOUT"] = "3"
|
155
|
+
expect(Timeout).to receive(:timeout).with 3
|
156
|
+
mosca.get
|
126
157
|
end
|
127
158
|
end
|
128
159
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mosca
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Armando Andini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mqtt
|