libmagellan 0.2.2 → 0.2.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: 2f77e34c045fcbf4c3dd80ccd3dc1cb324995bc3
4
- data.tar.gz: d13e05fe27f5bd98b3738e7304d7cac361855e40
3
+ metadata.gz: 13b45a69c0c9d2f8b9f43d46982bbc6eaf996c1b
4
+ data.tar.gz: 4a66787949d38ea6c7b59165525d9caa68d616d1
5
5
  SHA512:
6
- metadata.gz: c0fcf59afd0f0ed96e9e99f36beece45f98600bc0c6b8ac40fc34ddfd71e6cc44b7f492fca2cc2e1b9e0185363e77c789a491309340df5902462e8efa4a27f1d
7
- data.tar.gz: 7c512f6d7e66f0135e2340c8d869575e4e348f416c4d88a47ab72dcdebee4d0fa53985d69c4f804000fc32bd9ff1a16753d6c262a2567cf0c0ba2d5f4a4d1b9b
6
+ metadata.gz: 71440d7cfc32a062e72794ef68fd639f65529b7eaa31e3eeff452d10083694e3bedfbcd4754dd20bacc758579bbe343a4161eeda302441fd0a12d7543976cc75
7
+ data.tar.gz: 81787a14f39dfd840cd5c97be79d8b6d0c80703cf01ab3b73e35ef6d629c273ada6a6d773ce3d93c74b93e3f91c2e7e6c8024d0240e864e97b1db1ea9c0e5c84
data/Rakefile CHANGED
@@ -9,3 +9,12 @@ require "rspec/core/rake_task"
9
9
  RSpec::Core::RakeTask.new(:spec)
10
10
 
11
11
  task :default => :spec
12
+
13
+ module Bundler
14
+ class GemHelper
15
+ def version_tag
16
+ d = File.basename(File.dirname(__FILE__))
17
+ "#{d}/#{version}"
18
+ end
19
+ end
20
+ end
@@ -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 disconnet
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
@@ -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
- @consumer_key = options.delete :consumer_key
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
- http_client.use_ssl = true if uri.scheme == 'https'
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
- init_opt = {client_credential_key: @consumer_key, client_credential_secret: @consumer_secret}
84
- options[:uri] = uri if options[:uri].blank?
85
- client = ::Signet::OAuth1::Client.new(init_opt)
86
- client, options = yield(client, options) if block_given?
87
- req = client.generate_authenticated_request(options)
88
- header = { "Authorization" => req["Authorization"], "Cache-Control" => "no-store" }
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
 
@@ -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
- @consumer_key = args.delete(:consumer_key)
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(@consumer_key, @token)
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}; Client-Version: #{@client_version}"
166
+ headers = "Authorization: #{auth}"
167
+ headers << ";Client-Version: #{@client_version}" if @client_version.present?
152
168
  password = Base64.urlsafe_encode64(headers)
153
- connect(@consumer_key, password)
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
@@ -1,3 +1,3 @@
1
1
  module Libmagellan
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  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.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: 2014-12-22 00:00:00.000000000 Z
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.2.2
143
+ rubygems_version: 2.4.5
144
144
  signing_key:
145
145
  specification_version: 4
146
146
  summary: ruby client for magellanic cloud