mosca 0.0.9 → 0.1.0

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: 15f35eb8f69221fc5b332d47f9a7d16d2bc89298
4
- data.tar.gz: 0e69fc6153e88456cdb09a62e2e367fb2f9be624
3
+ metadata.gz: d27a9cc149fcd2c3204b832ece96c66329c7448d
4
+ data.tar.gz: 6219f4bb5be919658a32364dc5b755b93d7b6b9e
5
5
  SHA512:
6
- metadata.gz: a71f4cc939b000d437113a0c51b53e19fca2f8921e02ffaff04aa2f1459ac95225992b1f781c1fa9be0e597ef904dbf9e3f95ef23886899604ca9dc6785c6e1b
7
- data.tar.gz: 8132b4d0bb719cb28ef9e28d2a39cc4afa38031361a1761532940842c34c7a1c2e9693665fda5137da3d06c060e97f4cc63ed0c63ee60cbfe12d2d2db1801693
6
+ metadata.gz: 6463fdf1b45ac1fdfdb5a3e323ec7ec205ff773ae97c72b0e2ef5c31782da9d742fbc144a95306b9b06d8309dece9000df4b89b98ed2747fa99d1d167a837b60
7
+ data.tar.gz: 18696c8ff48bf8232fe1874524670e49d0ba2fda9729ea5fe364ba5d79efe8ebf7fa8c76492366a04f9a0334d90b6d7488c2e9ee5c93393b9f10e7a32e13d8a2
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mosca (0.0.8)
4
+ mosca (0.1.0)
5
5
  json (~> 1.8.1)
6
6
  mqtt (~> 0.3, >= 0.3.1)
7
7
 
@@ -13,7 +13,7 @@ GEM
13
13
  coderay (1.1.0)
14
14
  diff-lcs (1.2.5)
15
15
  docile (1.1.5)
16
- json (1.8.1)
16
+ json (1.8.2)
17
17
  method_source (0.8.2)
18
18
  mqtt (0.3.1)
19
19
  multi_json (1.10.1)
data/lib/mosca/client.rb CHANGED
@@ -7,28 +7,33 @@ module Mosca
7
7
  class Client
8
8
  extend Forwardable
9
9
 
10
- DEFAULT_KEEP_ALIVE = 10
11
10
  KEEP_ALIVE_MARGIN = 5
12
11
 
12
+ # class attributes
13
13
  class << self
14
14
  attr_accessor :default_broker, :default_timeout
15
15
  end
16
16
 
17
- self.default_broker = ENV["MOSCA_BROKER"] || "test.mosquitto.org"
18
- self.default_timeout = ENV["MOSCA_TIMEOUT"] || 5
17
+ attr_accessor :user, :pass, :topic_in, :topic_out, :broker, :topic_base,
18
+ :client, :keep_alive, :port, :time_out
19
19
 
20
- attr_accessor :user, :pass, :topic_in, :topic_out, :broker, :topic_base, :client
21
20
  def_delegators :connection, :subscribe
22
21
 
23
22
  def initialize args = {}
24
- @user = args[:user] || ENV["MOSCA_USER"]
25
- @pass = args[:user] || ENV["MOSCA_PASS"]
26
- @topic_in = args[:topic_in]
27
- @topic_out = args[:topic_out]
28
- @topic_base = args[:topic_base] || ""
29
- @broker = args[:broker] || ENV["MOSCA_BROKER"] || self.class.default_broker
30
- @client = args[:client] || MQTT::Client
31
- @keep_alive = args[:keep_alive] || DEFAULT_KEEP_ALIVE
23
+ default_attributes.merge(args).each do |k,v|
24
+ self.send("#{k}=", v)
25
+ end
26
+ end
27
+
28
+ def default_attributes
29
+ { user: ENV["MOSCA_USER"],
30
+ pass: ENV["MOSCA_PASS"],
31
+ topic_base: "",
32
+ broker: ENV["MOSCA_BROKER"] || self.class.default_broker || "test.mosquitto.org",
33
+ client: MQTT::Client,
34
+ keep_alive: 10,
35
+ time_out: (ENV["MOSCA_TIMEOUT"] || self.class.default_timeout || 5).to_i
36
+ }
32
37
  end
33
38
 
34
39
  def publish! message, params = {}
@@ -77,7 +82,7 @@ module Mosca
77
82
  private
78
83
 
79
84
  def client_options
80
- {remote_host: @broker, username: @user, password: @pass}
85
+ {remote_host: @broker, remote_port: @port, username: @user, password: @pass}
81
86
  end
82
87
 
83
88
  def connection
@@ -96,9 +101,9 @@ module Mosca
96
101
  response
97
102
  end
98
103
 
99
- def timeout params
100
- timeout = params[:timeout] || ENV["MOSCA_TIMEOUT"].to_i || self.class.default_timeout
101
- Timeout.timeout(timeout) do
104
+ def timeout params, &b
105
+ seconds = params[:timeout] || time_out
106
+ Timeout.timeout(seconds) do
102
107
  yield
103
108
  end
104
109
  end
data/lib/mosca/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Mosca
2
- VERSION = '0.0.9'
2
+ VERSION = '0.1.0'
3
3
  end
data/spec/mosca_spec.rb CHANGED
@@ -11,7 +11,7 @@ describe Mosca::Client do
11
11
  }
12
12
 
13
13
  let (:mosca) {
14
- mosca = Mosca::Client.new topic_out: OUT, topic_in: IN, client: client, keep_alive: KEEP_ALIVE
14
+ mosca = Mosca::Client.new topic_out: OUT, topic_in: IN, client: client, keep_alive: KEEP_ALIVE, port: "port"
15
15
  }
16
16
 
17
17
  it "has a default broker" do
@@ -38,7 +38,8 @@ describe Mosca::Client do
38
38
  end
39
39
 
40
40
  it "should wait for a response on topic_in if it's specified" do
41
- expect(client).to receive(:connect).once.and_call_original
41
+ expect(client).to receive(:connect).with(hash_including(remote_port: "port"))
42
+ .once.and_call_original
42
43
  expect(mosca.publish(MESSAGE, response: true)).to eq("response")
43
44
  end
44
45
  end
@@ -109,12 +110,12 @@ describe Mosca::Client do
109
110
 
110
111
  describe "Timeout" do
111
112
  it "has a default timeout of 5 seconds" do
112
- expect(Mosca::Client.default_timeout).to eq(5)
113
+ expect(described_class.new.time_out).to eq(5)
113
114
  end
114
115
 
115
116
  it "can set a default timeout" do
116
117
  described_class.default_timeout = 1
117
- expect(described_class.default_timeout).to eq 1
118
+ expect(described_class.new.time_out).to eq 1
118
119
  end
119
120
 
120
121
  it "calls timeout when getting a message" do
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.9
4
+ version: 0.1.0
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-12-11 00:00:00.000000000 Z
11
+ date: 2015-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mqtt