mosca 0.0.2 → 0.0.3

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: 8790d6fe2852f8a4486cbc47d9ed48c43e0c2e1b
4
- data.tar.gz: e166ef25f408e8d7532eed8ee6690c705d98a5d3
3
+ metadata.gz: 2cb5540c23079a910c9ecaaae0552ca1215fce85
4
+ data.tar.gz: 2877f69002dd959ee86df6d3674f12e34349bc6d
5
5
  SHA512:
6
- metadata.gz: 92d433566337cb9cc73eb153accd127d300857ad89967b3c0bdadc8670de0299bfc18471e6a0bd2763b6f2c8ed0d720792c4f95b03f4cd0f171d719ed3c94275
7
- data.tar.gz: 530ba900c1cd6e483cbc4fd70fa27efd2cf3d3097eecaec2f351824417174a3f2f40d2652653032145cb7e7f923b34ecd484e600d6b0309a9dac716a1dd732fe
6
+ metadata.gz: 2d3ae4b8afd6a651a7b2d08b20898964b6c68fc95cbbb33e09535e25a20f80059362c638a9e52d2ed13e7644f46795717b5c19b0a8b13941e397e689ef1ef9ac
7
+ data.tar.gz: f7505d90646a7d672ca05cd0fe59b1e79052b75d8267c9059336292cff0c4042eb08cc7682357f4022ad603bd54f47784ab277db599ed003e71f586f6b108ad4
data/lib/mosca/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Mosca
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
data/lib/mosca.rb CHANGED
@@ -6,7 +6,7 @@ class Mosca
6
6
  @@default_timeout = 5
7
7
  @@debug = false
8
8
 
9
- attr_accessor :user, :pass, :topic_in, :topic_out, :broker, :topic_base
9
+ attr_accessor :user, :pass, :topic_in, :topic_out, :broker, :topic_base, :client
10
10
 
11
11
  def initialize params = {}
12
12
  @user = params[:user]
@@ -20,7 +20,7 @@ class Mosca
20
20
 
21
21
  def publish json, params = {}
22
22
  connection do |c|
23
- topic = params[:topic] || @topic_out
23
+ topic = params[:topic_out] || @topic_out
24
24
  debug "[start publish] " + timestamp
25
25
  c.subscribe(topic_base + topic_in) if params[:response]
26
26
  c.publish(topic_base + topic,json)
@@ -34,7 +34,7 @@ class Mosca
34
34
  def get params = {}
35
35
  response = {}
36
36
  connection(params) do |c|
37
- topic = params[:topic] || @topic_in
37
+ topic = params[:topic_in] || @topic_in
38
38
  timeout = params[:timeout] || @@default_timeout
39
39
  begin
40
40
  Timeout.timeout(timeout) do
@@ -51,10 +51,6 @@ class Mosca
51
51
  response
52
52
  end
53
53
 
54
- def get_with_connection
55
-
56
- end
57
-
58
54
  def self.default_broker= param
59
55
  @@default_broker = param
60
56
  end
@@ -0,0 +1,25 @@
1
+ class ClientDouble
2
+ def connect *args
3
+ yield self
4
+ end
5
+
6
+ def publish topic, payload
7
+
8
+ end
9
+
10
+ def get topic, *args
11
+
12
+ if topic == "json_in_topic"
13
+ response = '{"a":123}'
14
+
15
+ elsif topic == "json_array_in_topic"
16
+ response = '["a",1,2]'
17
+
18
+ else
19
+ response = "response"
20
+ end
21
+
22
+ yield topic, response
23
+ end
24
+
25
+ end
data/spec/mosca_spec.rb CHANGED
@@ -1,31 +1,51 @@
1
- require 'mosca'
1
+ require 'spec_helper'
2
2
 
3
3
  describe Mosca do
4
+ OUT = "out_topic"
5
+ IN = "in_topic"
6
+ Mosca.default_timeout = 200
7
+
4
8
  before do
5
- @client = Mosca.new
9
+ @client_double = ClientDouble.new
10
+ @mosca = Mosca.new topic_out: OUT, topic_in: IN, client: @client_double
11
+ @test_message = "something"
6
12
  end
7
13
 
8
14
  it "has a default broker" do
9
- @client.broker.should eq("test.mosquitto.org")
15
+ @mosca.broker.should eq("test.mosquitto.org")
10
16
  end
11
17
 
12
- it "uses topic_out to publish if it was specified" do
13
-
18
+ it "uses topic_out to publish if it was specified, and publishes the desired message" do
19
+ @client_double.should_receive(:publish).with(OUT,@test_message)
20
+ @mosca.publish @test_message
21
+ end
22
+ it "can take a topic_out as argument when publishing" do
23
+ new_out_topic = "other_out_topic"
24
+ @client_double.should_receive(:publish).with(new_out_topic,@test_message)
25
+ @mosca.publish @test_message, topic_out: new_out_topic
14
26
  end
15
27
 
16
28
  it "uses topic_in to get messages if it was specified" do
17
-
29
+ @client_double.should_receive(:get).with(IN)
30
+ @mosca.get
18
31
  end
19
32
 
20
- it "publishes messages" do
21
-
33
+ it "can take a topic_in as argument when getting" do
34
+ new_in_topic = "other_in_topic"
35
+ @client_double.should_receive(:get).with(new_in_topic)
36
+ @mosca.get topic_in: new_in_topic
22
37
  end
23
38
 
24
- it "gets messages" do
25
-
39
+ it "will receive the message with get" do
40
+ @mosca.get.should eq("response")
26
41
  end
27
42
 
28
- it "gets a hash if the message was JSON" do
43
+ it "gets a hash if the message was a JSON object" do
44
+ @mosca.get(topic_in: "json_in_topic").should be_a(Hash)
45
+ end
29
46
 
47
+ it "gets an array if the message was a JSON array" do
48
+ @mosca.get(topic_in: "json_array_in_topic").should be_a(Array)
30
49
  end
50
+
31
51
  end
data/spec/spec_helper.rb CHANGED
@@ -1 +1,4 @@
1
+ require 'pry'
2
+ require 'mosca'
1
3
  require 'json'
4
+ require 'client_double'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mosca
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Armando Andini
@@ -48,6 +48,7 @@ files:
48
48
  - lib/mosca.rb
49
49
  - lib/mosca/version.rb
50
50
  - mosca.gemspec
51
+ - spec/client_double.rb
51
52
  - spec/mosca_spec.rb
52
53
  - spec/spec_helper.rb
53
54
  homepage: http://github.com/antico5/mosca
@@ -75,5 +76,6 @@ signing_key:
75
76
  specification_version: 4
76
77
  summary: MQTT messaging made easy
77
78
  test_files:
79
+ - spec/client_double.rb
78
80
  - spec/mosca_spec.rb
79
81
  - spec/spec_helper.rb