mosca 0.0.7 → 0.0.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea7492dc9f79f9985762189d909fd62cbaa8f919
4
- data.tar.gz: 86e69452d47ed91e8805ea394e3410253003af29
3
+ metadata.gz: f77a3ccdac0df20783d950be8510522ef872cc30
4
+ data.tar.gz: 11e7061866fb33a2fe98f3cc1ef0e5ebf8bc4483
5
5
  SHA512:
6
- metadata.gz: 6d58b4569fd4a443362401386517a7323cd3ec3bd15c0a56f0c7c08bb804e3391096d2b2be695c71e5f97a725d0085c92e9014c9ffecbc8f5e001b81794c2fdd
7
- data.tar.gz: 30f1d3bd4da9740f4ce0009ccc6ec8049f2907e54ed181f2b94dc076307f2de0d7edcb31c7426f50ec5c674621313e9dfc0e1c8d4f10c7a345c3e71e6260474b
6
+ metadata.gz: 49c6779585f022278f302fe3d36897e01bc71a8d440368be55dbba5076445bd0585fcbd545ec132f13cbb3ca7ea216ebacf47046f9ed5f86c459636274d7145b
7
+ data.tar.gz: 4632fbd6230ec967acf90865932090ca30e6bd2578ab35200d6346f8d49fcb65305ef1aaede36319d604c4529defd7eff42cf0928bf86ab9a433a60f7e973401
data/Gemfile.lock CHANGED
@@ -1,9 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mosca (0.0.7)
4
+ mosca (0.0.8)
5
5
  json (~> 1.8.1)
6
- mqtt (~> 0.3.0)
6
+ mqtt (~> 0.3.1)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
@@ -15,7 +15,7 @@ GEM
15
15
  docile (1.1.5)
16
16
  json (1.8.1)
17
17
  method_source (0.8.2)
18
- mqtt (0.3.0)
18
+ mqtt (0.3.1)
19
19
  multi_json (1.10.1)
20
20
  pry (0.10.1)
21
21
  coderay (~> 1.1.0)
data/README.md CHANGED
@@ -3,7 +3,14 @@
3
3
 
4
4
  # Mosca
5
5
 
6
- A simple client for mqtt communication
6
+ A MQTT library wrapper for ruby. Currently uses ruby-mqtt for connection and packet handling.
7
+
8
+ ## Features
9
+
10
+ - Automatically handles connection / reconnection when trying to get or publish messages. Good for background processes that deal with a high load of messages and need reliability and single connection handling.
11
+ - Allows specifying timeout on all of the operations.
12
+ - Simplifies 2-way communication by setting an out topic and an in topic to automatically wait for responses.
13
+ - Keeps only 1 connection open with the broker.
7
14
 
8
15
  ## Installation
9
16
 
data/lib/mosca/client.rb CHANGED
@@ -1,9 +1,12 @@
1
1
  require 'mqtt'
2
2
  require 'json'
3
3
  require 'mosca/exceptions'
4
+ require 'forwardable'
4
5
 
5
6
  module Mosca
6
7
  class Client
8
+ extend Forwardable
9
+
7
10
  class << self
8
11
  attr_accessor :default_broker, :default_timeout
9
12
  end
@@ -12,6 +15,7 @@ module Mosca
12
15
  self.default_timeout = ENV["MOSCA_TIMEOUT"] || 5
13
16
 
14
17
  attr_accessor :user, :pass, :topic_in, :topic_out, :broker, :topic_base, :client
18
+ def_delegators :connection, :subscribe
15
19
 
16
20
  def initialize args = {}
17
21
  @user = args[:user] || ENV["MOSCA_USER"]
@@ -62,6 +66,10 @@ module Mosca
62
66
  connection
63
67
  end
64
68
 
69
+ def connected?
70
+ @connection and @connection.connected? and is_alive?
71
+ end
72
+
65
73
  private
66
74
 
67
75
  def client_options
@@ -69,7 +77,7 @@ module Mosca
69
77
  end
70
78
 
71
79
  def connection
72
- if @connection && @connection.connected?
80
+ if connected?
73
81
  @connection
74
82
  else
75
83
  @connection = @client.connect(client_options)
@@ -88,5 +96,9 @@ module Mosca
88
96
  yield
89
97
  end
90
98
  end
99
+
100
+ def is_alive?
101
+ ( Time.now - @connection.last_ping_response ) < 30
102
+ end
91
103
  end
92
104
  end
data/lib/mosca/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Mosca
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
3
3
  end
data/mosca.gemspec CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
20
20
  s.require_paths = ["lib"]
21
21
 
22
- s.add_dependency 'mqtt', '~> 0.3.0'
22
+ s.add_dependency 'mqtt', '~> 0.3', '>= 0.3.1'
23
23
  s.add_dependency 'json', '~> 1.8.1'
24
24
 
25
25
  s.add_development_dependency 'bundler'
@@ -1,7 +1,7 @@
1
1
  class ClientDouble
2
2
  attr_accessor :connected
3
3
  def connect *args
4
- connected = true
4
+ self.connected = true
5
5
  self
6
6
  end
7
7
 
@@ -9,6 +9,10 @@ class ClientDouble
9
9
  !! connected
10
10
  end
11
11
 
12
+ def last_ping_response
13
+ Time.now
14
+ end
15
+
12
16
  def method_missing *args
13
17
 
14
18
  end
data/spec/mosca_spec.rb CHANGED
@@ -10,7 +10,7 @@ describe Mosca::Client do
10
10
  }
11
11
 
12
12
  let (:mosca) {
13
- Mosca::Client.new topic_out: OUT, topic_in: IN, client: client
13
+ mosca = Mosca::Client.new topic_out: OUT, topic_in: IN, client: client
14
14
  }
15
15
 
16
16
  it "has a default broker" do
@@ -57,6 +57,12 @@ describe Mosca::Client do
57
57
  it "will receive the message with get" do
58
58
  expect(mosca.get).to eq("response")
59
59
  end
60
+
61
+ it "will delegate subscribe to the mqtt client" do
62
+ allow(mosca).to receive(:connection).and_return(client)
63
+ expect(client).to receive(:subscribe).with("queue")
64
+ mosca.subscribe "queue"
65
+ end
60
66
  end
61
67
 
62
68
  describe "parsing the incoming messages" do
@@ -138,6 +144,30 @@ describe Mosca::Client do
138
144
  end
139
145
  end
140
146
 
147
+ describe "#connected?" do
148
+ it "is false when not connected" do
149
+ expect( mosca.connected? ).to be_falsey
150
+ end
151
+
152
+ context "previously establishing the connection" do
153
+ before do
154
+ mosca.refresh_connection
155
+ end
156
+
157
+ it "is true" do
158
+ mosca.refresh_connection
159
+ expect( mosca.connected? ).to be_truthy
160
+ end
161
+
162
+ it "is false when not alive (no ping response)" do
163
+ expect( client ).to receive( :last_ping_response ).and_return( Time.now - 30 )
164
+ expect( mosca.connected? ).to be_falsey
165
+ end
166
+
167
+ end
168
+
169
+ end
170
+
141
171
  describe "Environment variables" do
142
172
  it "can set MOSCA_BROKER" do
143
173
  ENV["MOSCA_BROKER"] = "broker"
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.7
4
+ version: 0.0.8
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-15 00:00:00.000000000 Z
11
+ date: 2014-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mqtt
@@ -16,14 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.3.0
19
+ version: '0.3'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.3.1
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - "~>"
25
28
  - !ruby/object:Gem::Version
26
- version: 0.3.0
29
+ version: '0.3'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.3.1
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: json
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -143,3 +149,4 @@ test_files:
143
149
  - spec/client_double.rb
144
150
  - spec/mosca_spec.rb
145
151
  - spec/spec_helper.rb
152
+ has_rdoc: