qtc-sdk 0.2.0 → 0.3.0
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 +4 -4
- data/.gitignore +18 -18
- data/Changelog.md +14 -6
- data/Gemfile +4 -4
- data/LICENSE.txt +22 -22
- data/README.md +44 -44
- data/Rakefile +1 -1
- data/bin/qtc-cli +13 -13
- data/lib/qtc/cli/commands.rb +15 -15
- data/lib/qtc/cli/common.rb +146 -91
- data/lib/qtc/cli/eds/base.rb +27 -27
- data/lib/qtc/cli/eds/commands.rb +20 -20
- data/lib/qtc/cli/eds/instances.rb +30 -30
- data/lib/qtc/cli/mar/apps.rb +116 -125
- data/lib/qtc/cli/mar/base.rb +60 -60
- data/lib/qtc/cli/mar/commands.rb +221 -199
- data/lib/qtc/cli/mar/debug.rb +87 -86
- data/lib/qtc/cli/mar/domains.rb +35 -38
- data/lib/qtc/cli/mar/env.rb +38 -40
- data/lib/qtc/cli/mar/repository.rb +24 -26
- data/lib/qtc/cli/mar/ssl_certificates.rb +40 -42
- data/lib/qtc/cli/mar/stack.rb +29 -0
- data/lib/qtc/cli/mdb/base.rb +47 -43
- data/lib/qtc/cli/mdb/commands.rb +43 -31
- data/lib/qtc/cli/mdb/instances.rb +79 -60
- data/lib/qtc/cli/platform/clouds.rb +33 -15
- data/lib/qtc/cli/platform/commands.rb +132 -65
- data/lib/qtc/cli/platform/datacenters.rb +23 -21
- data/lib/qtc/cli/platform/ssh_keys.rb +41 -41
- data/lib/qtc/cli/platform/user.rb +25 -25
- data/lib/qtc/cli/platform/vpn.rb +93 -0
- data/lib/qtc/client.rb +170 -170
- data/lib/qtc/eds/client.rb +116 -116
- data/lib/qtc/eds/collection.rb +124 -124
- data/lib/qtc/eds/user_collection.rb +13 -13
- data/lib/qtc/eds/usergroup_collection.rb +41 -41
- data/lib/qtc/errors.rb +13 -11
- data/lib/qtc/version.rb +3 -3
- data/lib/qtc-sdk.rb +1 -1
- data/qtc-sdk.gemspec +28 -28
- data/spec/unit/qtc/client_spec.rb +147 -147
- metadata +20 -19
- data/lib/qtc/mws/client.rb +0 -89
@@ -1,148 +1,148 @@
|
|
1
|
-
require 'qtc-sdk'
|
2
|
-
|
3
|
-
|
4
|
-
describe Qtc::Client do
|
5
|
-
|
6
|
-
let(:test_client) do
|
7
|
-
Qtc::Client.new('https://api.qtc.io')
|
8
|
-
end
|
9
|
-
|
10
|
-
describe '#initialize' do
|
11
|
-
it 'accepts single parameter (api_url)' do
|
12
|
-
expect {
|
13
|
-
Qtc::Client.new('https://api.qtc.io')
|
14
|
-
}.not_to raise_exception
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'accepts optional second parameter (headers)' do
|
18
|
-
expect {
|
19
|
-
client = Qtc::Client.new('https://api.qtc.io', {'Foo' => 'Bar'})
|
20
|
-
expect(client.default_headers['Foo']).to eq('Bar')
|
21
|
-
}.not_to raise_exception
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe '#get' do
|
26
|
-
it 'sends GET request with correct parameters' do
|
27
|
-
params = {test: 1}
|
28
|
-
headers = {'X-Test-Header' => 'some_value'}
|
29
|
-
test_client.http_client.should_receive(:get).with(
|
30
|
-
'https://api.qtc.io/v1/ping',
|
31
|
-
params,
|
32
|
-
hash_including(headers)
|
33
|
-
).and_return(double(:response, status: 200).as_null_object)
|
34
|
-
test_client.get('/v1/ping', params, headers)
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'raises exception when response is an error' do
|
38
|
-
data = {name: 'Test'}
|
39
|
-
test_client.http_client.should_receive(:get).and_return(double(:response, status: 400).as_null_object)
|
40
|
-
expect {
|
41
|
-
test_client.get('/v1/instances', data)
|
42
|
-
}.to raise_error(Qtc::Errors::StandardError)
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'returns parsed json responses' do
|
46
|
-
data = {'name' => 'Test'}
|
47
|
-
test_client.http_client.should_receive(:get).and_return(double(:response, status: 200, body: data.to_json).as_null_object)
|
48
|
-
resp = test_client.get('/v1/instances', data)
|
49
|
-
expect(resp).to eq(data)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
describe '#post' do
|
54
|
-
it 'sends POST request with correct parameters' do
|
55
|
-
data = {name: 'Test'}
|
56
|
-
params = {test: 1}
|
57
|
-
headers = {'Authorization' => 'Bearer token'}
|
58
|
-
test_client.http_client.should_receive(:post).with(
|
59
|
-
'https://api.qtc.io/v1/accounts',
|
60
|
-
{
|
61
|
-
header: hash_including(headers),
|
62
|
-
body: data.to_json,
|
63
|
-
query: params
|
64
|
-
}
|
65
|
-
).and_return(double(:response, status: 201).as_null_object)
|
66
|
-
test_client.post('/v1/accounts', data, params, headers)
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'raises exception when response is an error' do
|
70
|
-
data = {name: 'Test'}
|
71
|
-
test_client.http_client.should_receive(:post).and_return(double(:response, status: 400).as_null_object)
|
72
|
-
expect {
|
73
|
-
test_client.post('/v1/accounts', data)
|
74
|
-
}.to raise_error(Qtc::Errors::StandardError)
|
75
|
-
end
|
76
|
-
|
77
|
-
it 'returns parsed json responses' do
|
78
|
-
data = {'name' => 'Test'}
|
79
|
-
test_client.http_client.should_receive(:post).and_return(double(:response, status: 201, body: data.to_json).as_null_object)
|
80
|
-
resp = test_client.post('/v1/accounts', data)
|
81
|
-
expect(resp).to eq(data)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
describe '#put' do
|
86
|
-
it 'sends PUT request with correct parameters' do
|
87
|
-
data = {name: 'Test'}
|
88
|
-
params = {test: 1}
|
89
|
-
headers = {'Authorization' => 'Bearer token'}
|
90
|
-
test_client.http_client.should_receive(:put).with(
|
91
|
-
'https://api.qtc.io/v1/accounts',
|
92
|
-
{
|
93
|
-
header: hash_including(headers),
|
94
|
-
body: data.to_json,
|
95
|
-
query: params
|
96
|
-
}
|
97
|
-
).and_return(double(:response, status: 201).as_null_object)
|
98
|
-
test_client.put('/v1/accounts', data, params, headers)
|
99
|
-
end
|
100
|
-
|
101
|
-
it 'raises exception when response is an error' do
|
102
|
-
data = {name: 'Test'}
|
103
|
-
test_client.http_client.should_receive(:put).and_return(double(:response, status: 400).as_null_object)
|
104
|
-
expect {
|
105
|
-
test_client.put('/v1/accounts', data)
|
106
|
-
}.to raise_error(Qtc::Errors::StandardError)
|
107
|
-
end
|
108
|
-
|
109
|
-
it 'returns parsed json responses' do
|
110
|
-
data = {'name' => 'Test'}
|
111
|
-
test_client.http_client.should_receive(:put).and_return(double(:response, status: 201, body: data.to_json).as_null_object)
|
112
|
-
resp = test_client.put('/v1/accounts', data)
|
113
|
-
expect(resp).to eq(data)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
describe '#delete' do
|
118
|
-
it 'sends DELETE request with correct parameters' do
|
119
|
-
data = {name: 'Test'}
|
120
|
-
params = {test: 1}
|
121
|
-
headers = {'Authorization' => 'Bearer token'}
|
122
|
-
test_client.http_client.should_receive(:delete).with(
|
123
|
-
'https://api.qtc.io/v1/accounts',
|
124
|
-
{
|
125
|
-
header: hash_including(headers),
|
126
|
-
body: data.to_json,
|
127
|
-
query: params
|
128
|
-
}
|
129
|
-
).and_return(double(:response, status: 200).as_null_object)
|
130
|
-
test_client.delete('/v1/accounts', data, params, headers)
|
131
|
-
end
|
132
|
-
|
133
|
-
it 'raises exception when response is an error' do
|
134
|
-
data = {name: 'Test'}
|
135
|
-
test_client.http_client.should_receive(:delete).and_return(double(:response, status: 400).as_null_object)
|
136
|
-
expect {
|
137
|
-
test_client.delete('/v1/accounts', data)
|
138
|
-
}.to raise_error(Qtc::Errors::StandardError)
|
139
|
-
end
|
140
|
-
|
141
|
-
it 'returns parsed json responses' do
|
142
|
-
data = {'name' => 'Test'}
|
143
|
-
test_client.http_client.should_receive(:delete).and_return(double(:response, status: 200, body: data.to_json).as_null_object)
|
144
|
-
resp = test_client.delete('/v1/accounts', data)
|
145
|
-
expect(resp).to eq(data)
|
146
|
-
end
|
147
|
-
end
|
1
|
+
require 'qtc-sdk'
|
2
|
+
|
3
|
+
|
4
|
+
describe Qtc::Client do
|
5
|
+
|
6
|
+
let(:test_client) do
|
7
|
+
Qtc::Client.new('https://api.qtc.io')
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#initialize' do
|
11
|
+
it 'accepts single parameter (api_url)' do
|
12
|
+
expect {
|
13
|
+
Qtc::Client.new('https://api.qtc.io')
|
14
|
+
}.not_to raise_exception
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'accepts optional second parameter (headers)' do
|
18
|
+
expect {
|
19
|
+
client = Qtc::Client.new('https://api.qtc.io', {'Foo' => 'Bar'})
|
20
|
+
expect(client.default_headers['Foo']).to eq('Bar')
|
21
|
+
}.not_to raise_exception
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#get' do
|
26
|
+
it 'sends GET request with correct parameters' do
|
27
|
+
params = {test: 1}
|
28
|
+
headers = {'X-Test-Header' => 'some_value'}
|
29
|
+
test_client.http_client.should_receive(:get).with(
|
30
|
+
'https://api.qtc.io/v1/ping',
|
31
|
+
params,
|
32
|
+
hash_including(headers)
|
33
|
+
).and_return(double(:response, status: 200).as_null_object)
|
34
|
+
test_client.get('/v1/ping', params, headers)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'raises exception when response is an error' do
|
38
|
+
data = {name: 'Test'}
|
39
|
+
test_client.http_client.should_receive(:get).and_return(double(:response, status: 400).as_null_object)
|
40
|
+
expect {
|
41
|
+
test_client.get('/v1/instances', data)
|
42
|
+
}.to raise_error(Qtc::Errors::StandardError)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'returns parsed json responses' do
|
46
|
+
data = {'name' => 'Test'}
|
47
|
+
test_client.http_client.should_receive(:get).and_return(double(:response, status: 200, body: data.to_json).as_null_object)
|
48
|
+
resp = test_client.get('/v1/instances', data)
|
49
|
+
expect(resp).to eq(data)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#post' do
|
54
|
+
it 'sends POST request with correct parameters' do
|
55
|
+
data = {name: 'Test'}
|
56
|
+
params = {test: 1}
|
57
|
+
headers = {'Authorization' => 'Bearer token'}
|
58
|
+
test_client.http_client.should_receive(:post).with(
|
59
|
+
'https://api.qtc.io/v1/accounts',
|
60
|
+
{
|
61
|
+
header: hash_including(headers),
|
62
|
+
body: data.to_json,
|
63
|
+
query: params
|
64
|
+
}
|
65
|
+
).and_return(double(:response, status: 201).as_null_object)
|
66
|
+
test_client.post('/v1/accounts', data, params, headers)
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'raises exception when response is an error' do
|
70
|
+
data = {name: 'Test'}
|
71
|
+
test_client.http_client.should_receive(:post).and_return(double(:response, status: 400).as_null_object)
|
72
|
+
expect {
|
73
|
+
test_client.post('/v1/accounts', data)
|
74
|
+
}.to raise_error(Qtc::Errors::StandardError)
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'returns parsed json responses' do
|
78
|
+
data = {'name' => 'Test'}
|
79
|
+
test_client.http_client.should_receive(:post).and_return(double(:response, status: 201, body: data.to_json).as_null_object)
|
80
|
+
resp = test_client.post('/v1/accounts', data)
|
81
|
+
expect(resp).to eq(data)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe '#put' do
|
86
|
+
it 'sends PUT request with correct parameters' do
|
87
|
+
data = {name: 'Test'}
|
88
|
+
params = {test: 1}
|
89
|
+
headers = {'Authorization' => 'Bearer token'}
|
90
|
+
test_client.http_client.should_receive(:put).with(
|
91
|
+
'https://api.qtc.io/v1/accounts',
|
92
|
+
{
|
93
|
+
header: hash_including(headers),
|
94
|
+
body: data.to_json,
|
95
|
+
query: params
|
96
|
+
}
|
97
|
+
).and_return(double(:response, status: 201).as_null_object)
|
98
|
+
test_client.put('/v1/accounts', data, params, headers)
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'raises exception when response is an error' do
|
102
|
+
data = {name: 'Test'}
|
103
|
+
test_client.http_client.should_receive(:put).and_return(double(:response, status: 400).as_null_object)
|
104
|
+
expect {
|
105
|
+
test_client.put('/v1/accounts', data)
|
106
|
+
}.to raise_error(Qtc::Errors::StandardError)
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'returns parsed json responses' do
|
110
|
+
data = {'name' => 'Test'}
|
111
|
+
test_client.http_client.should_receive(:put).and_return(double(:response, status: 201, body: data.to_json).as_null_object)
|
112
|
+
resp = test_client.put('/v1/accounts', data)
|
113
|
+
expect(resp).to eq(data)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe '#delete' do
|
118
|
+
it 'sends DELETE request with correct parameters' do
|
119
|
+
data = {name: 'Test'}
|
120
|
+
params = {test: 1}
|
121
|
+
headers = {'Authorization' => 'Bearer token'}
|
122
|
+
test_client.http_client.should_receive(:delete).with(
|
123
|
+
'https://api.qtc.io/v1/accounts',
|
124
|
+
{
|
125
|
+
header: hash_including(headers),
|
126
|
+
body: data.to_json,
|
127
|
+
query: params
|
128
|
+
}
|
129
|
+
).and_return(double(:response, status: 200).as_null_object)
|
130
|
+
test_client.delete('/v1/accounts', data, params, headers)
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'raises exception when response is an error' do
|
134
|
+
data = {name: 'Test'}
|
135
|
+
test_client.http_client.should_receive(:delete).and_return(double(:response, status: 400).as_null_object)
|
136
|
+
expect {
|
137
|
+
test_client.delete('/v1/accounts', data)
|
138
|
+
}.to raise_error(Qtc::Errors::StandardError)
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'returns parsed json responses' do
|
142
|
+
data = {'name' => 'Test'}
|
143
|
+
test_client.http_client.should_receive(:delete).and_return(double(:response, status: 200, body: data.to_json).as_null_object)
|
144
|
+
resp = test_client.delete('/v1/accounts', data)
|
145
|
+
expect(resp).to eq(data)
|
146
|
+
end
|
147
|
+
end
|
148
148
|
end
|
metadata
CHANGED
@@ -1,97 +1,97 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: qtc-sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jari Kolehmainen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.5'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.5'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: 2.14.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 2.14.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: httpclient
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '2.3'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ~>
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '2.3'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: commander
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - '>='
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - '>='
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: inifile
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - '>='
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
type: :runtime
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - '>='
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
description: Qt Cloud Services SDK for Ruby
|
@@ -102,7 +102,7 @@ executables:
|
|
102
102
|
extensions: []
|
103
103
|
extra_rdoc_files: []
|
104
104
|
files:
|
105
|
-
-
|
105
|
+
- .gitignore
|
106
106
|
- Changelog.md
|
107
107
|
- Gemfile
|
108
108
|
- LICENSE.txt
|
@@ -123,6 +123,7 @@ files:
|
|
123
123
|
- lib/qtc/cli/mar/env.rb
|
124
124
|
- lib/qtc/cli/mar/repository.rb
|
125
125
|
- lib/qtc/cli/mar/ssl_certificates.rb
|
126
|
+
- lib/qtc/cli/mar/stack.rb
|
126
127
|
- lib/qtc/cli/mdb/base.rb
|
127
128
|
- lib/qtc/cli/mdb/commands.rb
|
128
129
|
- lib/qtc/cli/mdb/instances.rb
|
@@ -131,13 +132,13 @@ files:
|
|
131
132
|
- lib/qtc/cli/platform/datacenters.rb
|
132
133
|
- lib/qtc/cli/platform/ssh_keys.rb
|
133
134
|
- lib/qtc/cli/platform/user.rb
|
135
|
+
- lib/qtc/cli/platform/vpn.rb
|
134
136
|
- lib/qtc/client.rb
|
135
137
|
- lib/qtc/eds/client.rb
|
136
138
|
- lib/qtc/eds/collection.rb
|
137
139
|
- lib/qtc/eds/user_collection.rb
|
138
140
|
- lib/qtc/eds/usergroup_collection.rb
|
139
141
|
- lib/qtc/errors.rb
|
140
|
-
- lib/qtc/mws/client.rb
|
141
142
|
- lib/qtc/version.rb
|
142
143
|
- qtc-sdk.gemspec
|
143
144
|
- spec/spec_helper.rb
|
@@ -152,17 +153,17 @@ require_paths:
|
|
152
153
|
- lib
|
153
154
|
required_ruby_version: !ruby/object:Gem::Requirement
|
154
155
|
requirements:
|
155
|
-
- -
|
156
|
+
- - '>='
|
156
157
|
- !ruby/object:Gem::Version
|
157
158
|
version: 1.9.3
|
158
159
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
160
|
requirements:
|
160
|
-
- -
|
161
|
+
- - '>='
|
161
162
|
- !ruby/object:Gem::Version
|
162
163
|
version: '0'
|
163
164
|
requirements: []
|
164
165
|
rubyforge_project:
|
165
|
-
rubygems_version: 2.
|
166
|
+
rubygems_version: 2.4.3
|
166
167
|
signing_key:
|
167
168
|
specification_version: 4
|
168
169
|
summary: Qt Cloud Services SDK for Ruby
|
data/lib/qtc/mws/client.rb
DELETED
@@ -1,89 +0,0 @@
|
|
1
|
-
require 'faye/websocket'
|
2
|
-
|
3
|
-
module Qtc
|
4
|
-
module Mws
|
5
|
-
class Client
|
6
|
-
|
7
|
-
DEFAULT_OPTIONS = {
|
8
|
-
api_url: 'https://mws-eu-1.qtc.io/v1'
|
9
|
-
}
|
10
|
-
|
11
|
-
attr_accessor :gateway_id, :options, :http_client
|
12
|
-
|
13
|
-
##
|
14
|
-
# Initialize
|
15
|
-
#
|
16
|
-
# @param [String] gateway_id
|
17
|
-
# @param [Hash] options
|
18
|
-
def initialize(gateway_id, options = {})
|
19
|
-
self.gateway_id = gateway_id
|
20
|
-
self.options = DEFAULT_OPTIONS.merge(options)
|
21
|
-
self.http_client = Qtc::Client.new(http_client_url)
|
22
|
-
self.access_token = options[:access_token]
|
23
|
-
end
|
24
|
-
|
25
|
-
##
|
26
|
-
# Send message to websocket clients
|
27
|
-
#
|
28
|
-
# @param [String] message
|
29
|
-
# @param [Hash] receivers
|
30
|
-
# @option receivers [Array<String>] :sockets
|
31
|
-
# @option receivers [Array<String>] :tags
|
32
|
-
def send_message(message, receivers = {sockets: ['*'], tags: nil})
|
33
|
-
data = {data: message, receivers: receivers}
|
34
|
-
self.http_client.post('/messages', data)
|
35
|
-
end
|
36
|
-
|
37
|
-
##
|
38
|
-
# Create a new websocket object
|
39
|
-
#
|
40
|
-
# @param [Array<String>] tags
|
41
|
-
# @return [Hash]
|
42
|
-
def create_socket(tags = [])
|
43
|
-
data = {tags: tags}
|
44
|
-
self.http_client.post('/sockets', data)
|
45
|
-
end
|
46
|
-
|
47
|
-
##
|
48
|
-
# Get websocket uri
|
49
|
-
#
|
50
|
-
# @return [Hash]
|
51
|
-
def websocket_uri
|
52
|
-
self.http_client.get('/websocket_uri')
|
53
|
-
end
|
54
|
-
|
55
|
-
##
|
56
|
-
# Get websocket client
|
57
|
-
#
|
58
|
-
# @param [String] uri
|
59
|
-
# @return [Faye::WebSocket::Client]
|
60
|
-
def websocket_client(uri = nil)
|
61
|
-
uri = websocket_uri["uri"] if uri.nil?
|
62
|
-
|
63
|
-
Faye::WebSocket::Client.new(uri)
|
64
|
-
end
|
65
|
-
|
66
|
-
##
|
67
|
-
# Set access token
|
68
|
-
#
|
69
|
-
# @param [String] access_token
|
70
|
-
def access_token=(access_token)
|
71
|
-
if !access_token.nil?
|
72
|
-
self.http_client.default_headers['Authorization'] = "Bearer #{access_token}"
|
73
|
-
else
|
74
|
-
self.http_client.default_headers.delete('Authorization')
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
private
|
79
|
-
|
80
|
-
##
|
81
|
-
# Get url for HTTPClient
|
82
|
-
#
|
83
|
-
# @return [String]
|
84
|
-
def http_client_url
|
85
|
-
"#{self.options[:api_url]}/gateways/#{self.gateway_id}"
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|