libmagellan 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/libmagellan/core.rb +16 -0
- data/lib/libmagellan/http.rb +12 -0
- data/lib/libmagellan/mqtt.rb +20 -4
- data/lib/libmagellan/version.rb +1 -1
- data/spec/libmagellan/http_spec.rb +18 -9
- 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: 3871e63211b7396a0ba438cf859071efd30a810c
|
4
|
+
data.tar.gz: e93b0f9b640106a7b305fad091123e61994c25f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99775a4dc3340ef07c1b0a80584c84719e01e59b5926152e74410a372532b6e75362bc992cc85b59e26af44fef9ff3243eb152c8e295c827460eb339de439a3c
|
7
|
+
data.tar.gz: f146f7fee6374a3d34221344999a555ccd67a4a48c793e902df4279b769c87f590227339ed668509596d6d0512f8feac47edfb83623bb13829be47e4678eae5f
|
data/lib/libmagellan/core.rb
CHANGED
@@ -78,6 +78,10 @@ module Libmagellan
|
|
78
78
|
end
|
79
79
|
alias :sub :subscribe
|
80
80
|
|
81
|
+
def get_packet(topic=nil, &block)
|
82
|
+
@mqtt.get_packet(topic, &block)
|
83
|
+
end
|
84
|
+
|
81
85
|
def disconnect
|
82
86
|
@mqtt.disconnect
|
83
87
|
end
|
@@ -109,6 +113,18 @@ module Libmagellan
|
|
109
113
|
logger.level = level
|
110
114
|
end
|
111
115
|
|
116
|
+
def inspect
|
117
|
+
h = @http
|
118
|
+
m = @mqtt
|
119
|
+
arr = ["\#<#{self.class}: @base_uri=\"#{h.base_uri.to_s}\"",
|
120
|
+
"@consumer_key=\"#{h.consumer_key}\"",
|
121
|
+
"@client_version=\"#{h.client_version}\"",]
|
122
|
+
arr += ["@mqtt_host=\"#{m.host}\"",
|
123
|
+
"@mqtt_port=#{m.port}"] if @mqtt
|
124
|
+
arr << ">"
|
125
|
+
arr.join(", ")
|
126
|
+
end
|
127
|
+
|
112
128
|
end
|
113
129
|
|
114
130
|
end
|
data/lib/libmagellan/http.rb
CHANGED
@@ -8,6 +8,9 @@ require 'active_support/core_ext/object/blank'
|
|
8
8
|
module Libmagellan
|
9
9
|
class HTTP
|
10
10
|
|
11
|
+
attr_accessor :base_uri, :project, :consumer_key, :client_version
|
12
|
+
attr_accessor :verbose
|
13
|
+
|
11
14
|
DEFAULT_OPTIONS = {
|
12
15
|
# consumer_key: "groovenauts-test",
|
13
16
|
# consumer_secret: "test",
|
@@ -26,6 +29,7 @@ module Libmagellan
|
|
26
29
|
@consumer_key = options.delete :consumer_key || @project
|
27
30
|
@consumer_secret = options.delete :consumer_secret
|
28
31
|
@client_version = options.delete :client_version
|
32
|
+
@verbose = options.delete :verbose
|
29
33
|
@skip_verify = options[:skip_verify] || false
|
30
34
|
arg = args.first
|
31
35
|
@base_uri =
|
@@ -51,6 +55,14 @@ module Libmagellan
|
|
51
55
|
path_query = "#{uri.path}"
|
52
56
|
path_query << "?#{uri.query}" unless uri.query.blank?
|
53
57
|
|
58
|
+
if verbose
|
59
|
+
verbose_body =
|
60
|
+
case method.downcase.to_sym
|
61
|
+
when :patch, :put, :post then "\nBody:\n#{body}"
|
62
|
+
else nil
|
63
|
+
end
|
64
|
+
$stderr.puts("\e[34m#{uri.to_s}\nHeaders:\n#{headers.inspect}#{verbose_body}\e[0m")
|
65
|
+
end
|
54
66
|
response = case method.downcase.to_sym
|
55
67
|
when :patch then http.patch(path_query, body, headers)
|
56
68
|
when :delete then http.delete(path_query, headers)
|
data/lib/libmagellan/mqtt.rb
CHANGED
@@ -75,10 +75,10 @@ module Libmagellan
|
|
75
75
|
# Publish message to MQTT server.
|
76
76
|
# @param [String] topic Topic name
|
77
77
|
# @param [String] payload Payload
|
78
|
-
def publish(topic, payload)
|
78
|
+
def publish(topic, payload, retain=false, qos=0)
|
79
79
|
payload = payload.to_json unless payload.is_a?(::String)
|
80
80
|
with_connection do |c|
|
81
|
-
c.publish(topic, payload)
|
81
|
+
c.publish(topic, payload, retain, qos)
|
82
82
|
log("[PUBLISH] #{topic}: #{payload}", Logger::DEBUG)
|
83
83
|
end
|
84
84
|
end
|
@@ -105,8 +105,8 @@ module Libmagellan
|
|
105
105
|
def get(topic=nil)
|
106
106
|
with_connection do |c|
|
107
107
|
if block_given?
|
108
|
-
c.get(topic) do |topic_,
|
109
|
-
yield(topic_,
|
108
|
+
c.get(topic) do |topic_, payload_|
|
109
|
+
yield(topic_, payload_)
|
110
110
|
end
|
111
111
|
else
|
112
112
|
# return topic, payload
|
@@ -115,6 +115,22 @@ module Libmagellan
|
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
|
+
# Get message to me
|
119
|
+
# @param [String] topic Topic name
|
120
|
+
# @return [Mqtt::Packet] MQTT packet object
|
121
|
+
def get_packet(topic=nil)
|
122
|
+
with_connection do |c|
|
123
|
+
if block_given?
|
124
|
+
c.get_packet(topic) do |packet|
|
125
|
+
yield(packet)
|
126
|
+
end
|
127
|
+
else
|
128
|
+
# return topic, payload
|
129
|
+
c.get_packet(topic)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
118
134
|
|
119
135
|
# MQTT Disconnect
|
120
136
|
def disconnect
|
data/lib/libmagellan/version.rb
CHANGED
@@ -16,6 +16,15 @@ describe Libmagellan::HTTP do
|
|
16
16
|
|
17
17
|
describe :get do
|
18
18
|
it :success do
|
19
|
+
allow(http).to receive(:get).\
|
20
|
+
with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store")).\
|
21
|
+
and_return(res)
|
22
|
+
r = lm.request("/ping")
|
23
|
+
expect(r).to eq res
|
24
|
+
end
|
25
|
+
|
26
|
+
it "success with client_version" do
|
27
|
+
lm.client_version = "0.0.1"
|
19
28
|
allow(http).to receive(:get).\
|
20
29
|
with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store", "Client-Version" => "0.0.1")).\
|
21
30
|
and_return(res)
|
@@ -27,14 +36,14 @@ describe Libmagellan::HTTP do
|
|
27
36
|
describe :post do
|
28
37
|
it :success do
|
29
38
|
allow(http).to receive(:post).\
|
30
|
-
with("/ping", "param1=value1", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store"
|
39
|
+
with("/ping", "param1=value1", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store")).\
|
31
40
|
and_return(res)
|
32
41
|
r = lm.request("/ping", :post, "param1=value1")
|
33
42
|
expect(r).to eq res
|
34
43
|
end
|
35
44
|
it "accept Hash as body parameter" do
|
36
45
|
allow(http).to receive(:post).\
|
37
|
-
with("/ping", "param1=value1¶m2=value2", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store"
|
46
|
+
with("/ping", "param1=value1¶m2=value2", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store")).\
|
38
47
|
and_return(res)
|
39
48
|
r = lm.request("/ping", :post, {param1: "value1", param2: "value2"})
|
40
49
|
expect(r).to eq res
|
@@ -44,7 +53,7 @@ describe Libmagellan::HTTP do
|
|
44
53
|
describe :put do
|
45
54
|
it :success do
|
46
55
|
allow(http).to receive(:put).\
|
47
|
-
with("/ping", "param1=value1¶m2=value2", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store"
|
56
|
+
with("/ping", "param1=value1¶m2=value2", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store")).\
|
48
57
|
and_return(res)
|
49
58
|
r = lm.request("/ping", :put, {param1: "value1", param2: "value2"})
|
50
59
|
expect(r).to eq res
|
@@ -54,7 +63,7 @@ describe Libmagellan::HTTP do
|
|
54
63
|
describe :delete do
|
55
64
|
it :success do
|
56
65
|
allow(http).to receive(:delete).\
|
57
|
-
with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store"
|
66
|
+
with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store")).\
|
58
67
|
and_return(res)
|
59
68
|
r = lm.request("/ping", :delete)
|
60
69
|
expect(r).to eq res
|
@@ -76,7 +85,7 @@ describe Libmagellan::HTTP do
|
|
76
85
|
allow(res).to receive(:code).and_return("200")
|
77
86
|
allow(res).to receive(:body).and_return("pong\n")
|
78
87
|
allow(http).to receive(:get).\
|
79
|
-
with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store"
|
88
|
+
with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store")).\
|
80
89
|
and_return(res)
|
81
90
|
r = lm.test_ping
|
82
91
|
expect(r).to eq lm
|
@@ -84,13 +93,13 @@ describe Libmagellan::HTTP do
|
|
84
93
|
|
85
94
|
it :connection_failure_1_time do
|
86
95
|
allow(http).to receive(:get).\
|
87
|
-
with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store"
|
96
|
+
with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store")).\
|
88
97
|
once.\
|
89
98
|
and_raise(Errno::ECONNREFUSED)
|
90
99
|
allow(res).to receive(:code).and_return("200")
|
91
100
|
allow(res).to receive(:body).and_return("pong\n")
|
92
101
|
allow(http).to receive(:get).\
|
93
|
-
with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store"
|
102
|
+
with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store")).\
|
94
103
|
and_return(res)
|
95
104
|
r = lm.test_ping
|
96
105
|
expect(r).to eq lm
|
@@ -98,7 +107,7 @@ describe Libmagellan::HTTP do
|
|
98
107
|
|
99
108
|
it :connection_failure do
|
100
109
|
allow(http).to receive(:get).\
|
101
|
-
with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store"
|
110
|
+
with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store")).\
|
102
111
|
exactly(3).times.\
|
103
112
|
and_raise(Errno::ECONNREFUSED)
|
104
113
|
expect{ lm.test_ping(max_retry_count: 2, retry_interval: 0) }.to raise_error(Errno::ECONNREFUSED)
|
@@ -108,7 +117,7 @@ describe Libmagellan::HTTP do
|
|
108
117
|
allow(res).to receive(:code).and_return("500")
|
109
118
|
allow(res).to receive(:body).and_return("Something wrong!")
|
110
119
|
allow(http).to receive(:get).\
|
111
|
-
with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store"
|
120
|
+
with("/ping", hash_including("Authorization" => an_instance_of(String), "Cache-Control" => "no-store")).\
|
112
121
|
and_return(res)
|
113
122
|
expect{ lm.test_ping }.to raise_error(Libmagellan::InvalidResponse)
|
114
123
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: libmagellan
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- akima
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: signet
|