libmagellan 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +9 -0
- data/lib/libmagellan/core.rb +6 -1
- data/lib/libmagellan/http.rb +24 -12
- data/lib/libmagellan/mqtt.rb +28 -5
- data/lib/libmagellan/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13b45a69c0c9d2f8b9f43d46982bbc6eaf996c1b
|
4
|
+
data.tar.gz: 4a66787949d38ea6c7b59165525d9caa68d616d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71440d7cfc32a062e72794ef68fd639f65529b7eaa31e3eeff452d10083694e3bedfbcd4754dd20bacc758579bbe343a4161eeda302441fd0a12d7543976cc75
|
7
|
+
data.tar.gz: 81787a14f39dfd840cd5c97be79d8b6d0c80703cf01ab3b73e35ef6d629c273ada6a6d773ce3d93c74b93e3f91c2e7e6c8024d0240e864e97b1db1ea9c0e5c84
|
data/Rakefile
CHANGED
data/lib/libmagellan/core.rb
CHANGED
@@ -19,6 +19,7 @@ module Libmagellan
|
|
19
19
|
if mqtt_options[:mqtt_host].present? and mqtt_options[:mqtt_port].present?
|
20
20
|
mqtt_options[:host] = mqtt_options.delete(:mqtt_host)
|
21
21
|
mqtt_options[:port] = mqtt_options.delete(:mqtt_port)
|
22
|
+
mqtt_options[:secure] = mqtt_options.delete(:mqtt_secure) || false
|
22
23
|
@mqtt = MQTT.new(mqtt_options)
|
23
24
|
@mqtt.logger = logger
|
24
25
|
end
|
@@ -77,10 +78,14 @@ module Libmagellan
|
|
77
78
|
end
|
78
79
|
alias :sub :subscribe
|
79
80
|
|
80
|
-
def
|
81
|
+
def disconnect
|
81
82
|
@mqtt.disconnect
|
82
83
|
end
|
83
84
|
|
85
|
+
def set_will(topic, payload, retain=false, qos=0)
|
86
|
+
@mqtt.set_will(topic, payload, retain, qos)
|
87
|
+
end
|
88
|
+
|
84
89
|
def get_message(*args, &block)
|
85
90
|
@mqtt.get(*args, &block)
|
86
91
|
end
|
data/lib/libmagellan/http.rb
CHANGED
@@ -9,9 +9,9 @@ module Libmagellan
|
|
9
9
|
class HTTP
|
10
10
|
|
11
11
|
DEFAULT_OPTIONS = {
|
12
|
-
consumer_key: "groovenauts-test",
|
13
|
-
consumer_secret: "test",
|
14
|
-
client_version: "0.0.1",
|
12
|
+
# consumer_key: "groovenauts-test",
|
13
|
+
# consumer_secret: "test",
|
14
|
+
# client_version: "0.0.1",
|
15
15
|
}.freeze
|
16
16
|
|
17
17
|
# LibMagellan.new("localhost")
|
@@ -22,9 +22,11 @@ module Libmagellan
|
|
22
22
|
# LibMagellan.new(host: "localhost", port: 3000, consumer_key: "foo", consumer_secret: "bar")
|
23
23
|
def initialize(*args)
|
24
24
|
options = DEFAULT_OPTIONS.dup.update((args.last.is_a?(Hash) ? args.pop : {}))
|
25
|
-
@
|
25
|
+
@project = options.delete :project
|
26
|
+
@consumer_key = options.delete :consumer_key || @project
|
26
27
|
@consumer_secret = options.delete :consumer_secret
|
27
28
|
@client_version = options.delete :client_version
|
29
|
+
@skip_verify = options[:skip_verify] || false
|
28
30
|
arg = args.first
|
29
31
|
@base_uri =
|
30
32
|
case arg
|
@@ -41,7 +43,10 @@ module Libmagellan
|
|
41
43
|
end
|
42
44
|
uri = URI.join(@base_uri.to_s, path)
|
43
45
|
http_client = Net::HTTP.new(uri.host, uri.port)
|
44
|
-
|
46
|
+
if uri.scheme == 'https'
|
47
|
+
http_client.use_ssl = true
|
48
|
+
http_client.verify_mode = OpenSSL::SSL::VERIFY_NONE if @skip_verify
|
49
|
+
end
|
45
50
|
response = http_client.start do |http|
|
46
51
|
path_query = "#{uri.path}"
|
47
52
|
path_query << "?#{uri.query}" unless uri.query.blank?
|
@@ -62,7 +67,7 @@ module Libmagellan
|
|
62
67
|
uri = URI.join(@base_uri.to_s, path)
|
63
68
|
options = {method: method}
|
64
69
|
auth_headers = generate_oauth1_2legged_request(uri, options)
|
65
|
-
auth_headers["Client-Version"] = @client_version
|
70
|
+
auth_headers["Client-Version"] = @client_version if @client_version.present?
|
66
71
|
headers = headers.update(auth_headers)
|
67
72
|
request_without_oauth(path, method, body, headers) do |r|
|
68
73
|
yield(r) if block_given?
|
@@ -80,12 +85,19 @@ module Libmagellan
|
|
80
85
|
end
|
81
86
|
|
82
87
|
def generate_oauth_request(uri, options={})
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
88
|
+
header = { "Cache-Control" => "no-store" }
|
89
|
+
if @consumer_key.blank? and @consumer_secret.blank? and @project.blank?
|
90
|
+
return header
|
91
|
+
elsif (@consumer_key.blank? or @consumer_secret.blank?) and @project.present?
|
92
|
+
header["Project"] = @project
|
93
|
+
else
|
94
|
+
init_opt = {client_credential_key: @consumer_key, client_credential_secret: @consumer_secret}
|
95
|
+
options[:uri] = uri if options[:uri].blank?
|
96
|
+
client = ::Signet::OAuth1::Client.new(init_opt)
|
97
|
+
client, options = yield(client, options) if block_given?
|
98
|
+
req = client.generate_authenticated_request(options)
|
99
|
+
header["Authorization"] = req["Authorization"]
|
100
|
+
end
|
89
101
|
return header
|
90
102
|
end
|
91
103
|
|
data/lib/libmagellan/mqtt.rb
CHANGED
@@ -40,6 +40,7 @@ module Libmagellan
|
|
40
40
|
consumer_key: {required: true},
|
41
41
|
consumer_secret: {required: true},
|
42
42
|
client_version: {required: true},
|
43
|
+
secure: {default: false},
|
43
44
|
}.freeze
|
44
45
|
|
45
46
|
attr_accessor :host, :port, :consumer_key, :consumer_secret, :client_version
|
@@ -50,6 +51,7 @@ module Libmagellan
|
|
50
51
|
# @param [Hash] args MQTT Arguments
|
51
52
|
# @option args [String] :host MQTT server host address
|
52
53
|
# @option args [Integer] :port MQTT server port
|
54
|
+
# @option args [String] :project Project name
|
53
55
|
# @option args [String] :consumer_key MQTT authorization consumer-key
|
54
56
|
# @option args [String] :consumer_key MQTT authorization consumer-secret
|
55
57
|
def initialize(args={})
|
@@ -58,9 +60,12 @@ module Libmagellan
|
|
58
60
|
|
59
61
|
@host = args.delete(:host)
|
60
62
|
@port = args.delete(:port)
|
61
|
-
@
|
63
|
+
@project = args.delete(:project)
|
64
|
+
@consumer_key = args.delete(:consumer_key) || @project
|
62
65
|
@consumer_secret = args.delete(:consumer_secret)
|
63
66
|
@client_version = args.delete(:client_version)
|
67
|
+
@secure = args.delete(:secure) || false
|
68
|
+
@skip_verify = args[:skip_verify] || false
|
64
69
|
|
65
70
|
@token = nil
|
66
71
|
@client = nil
|
@@ -116,6 +121,15 @@ module Libmagellan
|
|
116
121
|
connection.disconnect if connected?
|
117
122
|
end
|
118
123
|
|
124
|
+
# MQTT Will
|
125
|
+
def set_will(topic, payload, retain=false, qos=0)
|
126
|
+
@will_topic = topic
|
127
|
+
@will_payload = payload
|
128
|
+
@will_qos = qos
|
129
|
+
@will_retain = retain
|
130
|
+
@with_will = true
|
131
|
+
end
|
132
|
+
|
119
133
|
# @return [Logger]
|
120
134
|
def logger
|
121
135
|
@logger_
|
@@ -139,18 +153,20 @@ module Libmagellan
|
|
139
153
|
# Connect MQTT server
|
140
154
|
# @return [MQTT::Client] MQTT Client
|
141
155
|
def connection
|
156
|
+
username = @project || @consumer_key
|
142
157
|
if connected?
|
143
158
|
return @client
|
144
159
|
else
|
145
160
|
if @token.present?
|
146
|
-
|
147
|
-
connect(
|
161
|
+
# トークンによる認証
|
162
|
+
connect(username, @token)
|
148
163
|
else
|
149
164
|
# OAuthによる認証
|
150
165
|
auth = generate_oauth_header(AUTH_BASE_URL, @consumer_key, @consumer_secret)
|
151
|
-
headers = "Authorization: #{auth}
|
166
|
+
headers = "Authorization: #{auth}"
|
167
|
+
headers << ";Client-Version: #{@client_version}" if @client_version.present?
|
152
168
|
password = Base64.urlsafe_encode64(headers)
|
153
|
-
connect(
|
169
|
+
connect(username, password)
|
154
170
|
end
|
155
171
|
end
|
156
172
|
end
|
@@ -163,6 +179,13 @@ module Libmagellan
|
|
163
179
|
mqtt_host = "#{@host}:#{@port.to_s}"
|
164
180
|
uri = "#{PROTOCOL}://#{username}:#{password}@#{mqtt_host}"
|
165
181
|
@client = ::MQTT::Client.new(uri)
|
182
|
+
if @with_will
|
183
|
+
@client.set_will(@will_topic, @will_payload, @will_retain, @will_qos)
|
184
|
+
end
|
185
|
+
if @secure
|
186
|
+
@client.ssl = true
|
187
|
+
@client.ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE if @skip_verify
|
188
|
+
end
|
166
189
|
@client.connect
|
167
190
|
log("[CONNECTED] #{host}:#{port}", Logger::DEBUG)
|
168
191
|
return @client
|
data/lib/libmagellan/version.rb
CHANGED
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- akima
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: signet
|
@@ -140,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
140
|
version: '0'
|
141
141
|
requirements: []
|
142
142
|
rubyforge_project:
|
143
|
-
rubygems_version: 2.
|
143
|
+
rubygems_version: 2.4.5
|
144
144
|
signing_key:
|
145
145
|
specification_version: 4
|
146
146
|
summary: ruby client for magellanic cloud
|