reign-client 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d7ef2a4f1499e567a2d11ea36a34b5e748193fb5
4
- data.tar.gz: 0c07a8647b4921d711808ac610d7daf579a92cc3
3
+ metadata.gz: dea8b1909cfb4ccaed04143d9e072ff208d0e766
4
+ data.tar.gz: 3dafa685d1295e9a61836c3ca7c83d696b5e2902
5
5
  SHA512:
6
- metadata.gz: feaba5efe9fa2d672df6105d1a591040c5228cfb6cc78a7eed36073eeeac68904679b654dab1097485a17f3470e68b502c6862f8c88cac3e2110c9dbed669294
7
- data.tar.gz: 15e56f2e5df98abd192588a527e88eb8db061fe0f85fa13c6ce676ee9931c872645efd0ea48a7b21ca3ff5f9af6505950547598317c769b165cfd4bbae79ca19
6
+ metadata.gz: a3e45de40719a90c267dbcb5023486dae58a26834c71baf3a718d09d813a5bc69525b21234d2353008dd943599f673725f0c52c65927c831b216d8809338f8d7
7
+ data.tar.gz: 359b9a37ae6f58ec2a42324a446aa952061be5998d3d7cd7e6b2a2d234c62475931efe61ae4e16717d7a005dd05ad3a230a126b575547950f01d4cf775ae2cb1
data/.gitignore ADDED
@@ -0,0 +1,36 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ ## Specific to RubyMotion:
14
+ .dat*
15
+ .repl_history
16
+ build/
17
+
18
+ ## Documentation cache and generated files:
19
+ /.yardoc/
20
+ /_yardoc/
21
+ /doc/
22
+ /rdoc/
23
+
24
+ ## Environment normalization:
25
+ /.bundle/
26
+ /vendor/bundle
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ # Gemfile.lock
32
+ # .ruby-version
33
+ # .ruby-gemset
34
+
35
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # A sample Gemfile
2
+ source 'https://rubygems.org'
3
+
4
+ gem "rest-client"
5
+ gem "json"
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ domain_name (0.5.20160310)
5
+ unf (>= 0.0.5, < 1.0.0)
6
+ http-cookie (1.0.2)
7
+ domain_name (~> 0.5)
8
+ json (1.8.3)
9
+ mime-types (2.99.1)
10
+ netrc (0.11.0)
11
+ rest-client (1.8.0)
12
+ http-cookie (>= 1.0.2, < 2.0)
13
+ mime-types (>= 1.16, < 3.0)
14
+ netrc (~> 0.7)
15
+ unf (0.1.4)
16
+ unf_ext
17
+ unf_ext (0.0.7.2)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ json
24
+ rest-client
25
+
26
+ BUNDLED WITH
27
+ 1.11.2
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # reign-client-ruby
data/lib/common.rb ADDED
@@ -0,0 +1,260 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+ module Reign
5
+ #Constants for API resources
6
+ GET_REGIONS_URI = '/api/v1/regions'
7
+ GET_PRESENCE_URI = '/api/v1/presence/'
8
+ HEARTBEAT_URI = '/api/v1/heartbeat/'
9
+ KEY_VALUE_URI = '/api/v1/kv/'
10
+ LEASE_URI = '/api/v1/lease/'
11
+ WATCH_URI = '/api/v1/watch/'
12
+
13
+ def self.generate_categories_uri(namespace)
14
+ if namespace == nil
15
+ raise ArgumentError.new("must pass namespace")
16
+ end
17
+ return GET_PRESENCE_URI + namespace
18
+ end
19
+
20
+ def self.generate_nodes_in_category_uri args
21
+ category = args[:category]
22
+ namespace = args[:namespace]
23
+ status = args[:status]
24
+ if category == nil
25
+ raise ArgumentError.new("must pass category")
26
+ end
27
+ if status != nil and not ["OK", "NOK"].include?(status)
28
+ raise ArgumentError.new("status must be OK or NOK if passed")
29
+ end
30
+ if namespace == nil
31
+ raise ArgumentError.new("must pass namespace")
32
+ end
33
+
34
+ uri = Reign::GET_PRESENCE_URI + namespace + "/" + category
35
+ if status != nil
36
+ uri = uri + Reign.generate_query_string({"status" => status})
37
+ end
38
+ return uri
39
+ end
40
+
41
+ def self.generate_upsert_kv_uri args
42
+ uri = Reign.generate_kv_uri(args[:namespace], args[:kv_path])
43
+ query_params = {}
44
+ if args[:owner_id]
45
+ query_params["owner_id"] = args[:owner_id]
46
+ end
47
+ if args[:sig]
48
+ query_params["sig"] = args[:sig]
49
+ end
50
+ uri = uri + Reign.generate_query_string(query_params)
51
+ return uri
52
+ end
53
+
54
+ def self.generate_delete_kv_uri args
55
+ if args[:sig] == nil
56
+ raise ArgumentError.new("must pass sig parameter in order to delete entry")
57
+ end
58
+ uri = Reign.generate_kv_uri(args[:namespace], args[:kv_path])
59
+ uri = uri + Reign.generate_query_string({"sig" => args[:sig]})
60
+ return uri
61
+ end
62
+
63
+ def self.generate_get_kv_uri args
64
+ return Reign.generate_kv_uri(args[:namespace], args[:kv_path])
65
+ end
66
+
67
+ def self.generate_kv_uri(namespace, kv_path)
68
+ if namespace == nil
69
+ raise ArgumentError.new("must pass namespace")
70
+ end
71
+ if kv_path == nil
72
+ raise ArgumentError.new("must pass kv_path")
73
+ end
74
+ kv_path.slice!(0) if kv_path[0] == "/"
75
+ kv_path.chomp!("/")
76
+ return KEY_VALUE_URI + namespace + "/" + kv_path
77
+ end
78
+
79
+ def self.generate_acquire_lease_uri args
80
+ term_s = args[:term_s]
81
+ owner_id = args[:owner_id]
82
+ if term_s == nil
83
+ raise ArgumentError.new("must pass term_s")
84
+ end
85
+ if owner_id == nil
86
+ raise ArgumentError.new("must pass owner_id")
87
+ end
88
+ term_s = term_s.to_s
89
+ query_params = {"op" => "acquire", "term_s" => term_s, "owner_id" => owner_id}
90
+
91
+ uri = Reign.generate_lease_uri(args[:namespace], args[:lease_path])
92
+ return uri + Reign.generate_query_string(query_params)
93
+ end
94
+
95
+ def self.generate_release_lease_uri args
96
+ sig = args[:sig]
97
+ if sig == nil
98
+ raise ArgumentError.new("must pass sig. This value is returned in the response body when the lease is acquired")
99
+ end
100
+
101
+ query_params = {"op" => "release", "sig" => sig}
102
+ uri = Reign.generate_lease_uri(args[:namespace], args[:lease_path])
103
+ return uri + Reign.generate_query_string(query_params)
104
+ end
105
+
106
+ def self.generate_revoke_lease_uri args
107
+ sig = args[:sig]
108
+ if sig == nil
109
+ raise ArgumentError.new("must pass sig. This value is returned in the response body when the lease is acquired")
110
+ end
111
+
112
+ query_params = {"op" => "revoke", "sig" => sig}
113
+ uri = Reign.generate_lease_uri(args[:namespace], args[:lease_path])
114
+ return uri + Reign.generate_query_string(query_params)
115
+ end
116
+
117
+ def self.generate_get_lease_uri args
118
+ return Reign.generate_lease_uri(args[:namespace], args[:lease_path])
119
+ end
120
+
121
+ def self.generate_lease_uri(namespace, lease_path)
122
+ if namespace == nil
123
+ raise ArgumentError.new("must pass namespace")
124
+ end
125
+ if lease_path == nil or lease_path.length == 0
126
+ raise ArgumentError.new("must pass non-trivial lease_path")
127
+ end
128
+ lease_path.slice!(0) if lease_path[0] == "/"
129
+ lease_path.chomp!("/")
130
+ return LEASE_URI + namespace + "/" + lease_path
131
+ end
132
+
133
+ def self.generate_watch_uri(namespace, watch_entity_path)
134
+ if namespace == nil
135
+ raise ArgumentError.new("must pass namespace")
136
+ end
137
+ if watch_entity_path == nil
138
+ raise ArgumentError.new("must pass watch_entity_path")
139
+ end
140
+ watch_entity_path.slice!(0) if watch_entity_path[0] == "/"
141
+ watch_entity_path.chomp!("/")
142
+ return WATCH_URI + namespace + "/" + watch_entity_path
143
+ end
144
+
145
+ def self.generate_heartbeat_uri(namespace)
146
+ if namespace == nil
147
+ raise ArgumentError.new("must pass namespace")
148
+ end
149
+ return Reign::HEARTBEAT_URI + namespace
150
+ end
151
+
152
+ def self.generate_heartbeat_body args
153
+ node_id = args[:node_id]
154
+ host = args[:host]
155
+ port = args[:port]
156
+ status = args[:status]
157
+ hb_to_s = args[:hb_to_s]
158
+ category = args[:category]
159
+ namespace = args[:namespace]
160
+ if node_id == nil
161
+ raise ArgumentError.new("must pass node_id")
162
+ end
163
+ if not (status == "OK" or status == "NOK")
164
+ raise ArgumentError.new("must pass status = OK / NOK")
165
+ end
166
+ if hb_to_s == nil
167
+ raise ArgumentError.new("must pass heartbeat timeout secs")
168
+ end
169
+ if namespace == nil
170
+ raise ArgumentError.new("must pass namespace")
171
+ end
172
+ if category == nil
173
+ raise ArgumentError.new("must pass category")
174
+ end
175
+
176
+ request_body = {}
177
+ request_body['presence'] = {}
178
+ request_body['presence']['category'] = category
179
+
180
+ node = {}
181
+ node['id'] = node_id
182
+ node['host'] = host
183
+ node['port'] = port
184
+ node['status'] = status
185
+ node['hb_to_s'] = hb_to_s
186
+
187
+ request_body['presence']['node'] = node
188
+ return request_body
189
+ end
190
+
191
+ def self.generate_query_string(map)
192
+ query = "?"
193
+ map.each {
194
+ |k, v|
195
+ query.concat(k)
196
+ query.concat("=")
197
+ query.concat(v)
198
+ query.concat("&")
199
+ }
200
+ return query.chomp("&")
201
+ end
202
+
203
+ def self.generate_watch_body_template(owner_id, email_contacts, webhook_contacts)
204
+ if owner_id == nil
205
+ raise ArgumentError.new("must pass owner_id")
206
+ end
207
+
208
+ email_contacts = email_contacts ? email_contacts : []
209
+ webhook_contacts = webhook_contacts ? webhook_contacts : []
210
+
211
+ body = {}
212
+ body["watch"] = {}
213
+ body["watch"]["webhooks"] = []
214
+ body["watch"]["emails"] = []
215
+ body["watch"]["owner_id"] = owner_id
216
+
217
+ email_contacts.each {|email| body["watch"]["emails"].push(email)}
218
+ webhook_contacts.each {|webhook| body["watch"]["webhooks"].push(webhook)}
219
+
220
+ return body
221
+ end
222
+
223
+ def self.generate_header_for_request(args)
224
+ #parse arguments
225
+ request_method = args[:method] or raise ArgumentError, 'must pass :method'
226
+ request_uri = args[:request_uri] or raise ArgumentError, 'must pass :request_uri'
227
+ api_key = args[:api_key] or raise ArgumentError, 'must pass :api_key'
228
+ secret_key = args[:secret_key] or raise ArgumentError, 'must pass :secret_key'
229
+ region = args[:region] or raise ArgumentError, 'must pass :region'
230
+ payload = args[:payload]
231
+
232
+ now = Time.new
233
+ date_header_value = now.strftime("%a, %d %b %Y %X %z")
234
+
235
+ case request_method
236
+ when :get
237
+ message = date_header_value + "\n" + "GET" + "\n" + request_uri
238
+ when :post
239
+ if payload != nil
240
+ message = date_header_value + "\n" + "POST" + "\n" + request_uri + "\n" + Base64.strict_encode64(payload.strip)
241
+ else
242
+ message = date_header_value + "\n" + "POST" + "\n" + request_uri
243
+ end
244
+ when :put
245
+ if payload != nil
246
+ message = date_header_value + "\n" + "PUT" + "\n" + request_uri + "\n" + Base64.strict_encode64(payload.strip)
247
+ else
248
+ message = date_header_value + "\n" + "PUT" + "\n" + request_uri
249
+ end
250
+ when :delete
251
+ message = date_header_value + "\n" + "DELETE" + "\n" + request_uri
252
+ end
253
+
254
+ hash = Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', secret_key, message)).strip
255
+ auth = 'REIGN-HMAC-SHA256 ' + api_key + ":" + hash
256
+ headers = {:Date => date_header_value, :Authorization => auth, :'Reign-Region' => region}
257
+
258
+ return headers
259
+ end
260
+ end
@@ -0,0 +1,216 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+ require 'base64'
4
+ require File.dirname(__FILE__) + '/common.rb'
5
+
6
+ module Reign
7
+ class Client
8
+ def initialize args
9
+ #example endpoint: https://int-usw2.reign.io
10
+ @endpoint = args[:endpoint] or raise ArgumentError.new("must pass endpoint")
11
+ @endpoint = @endpoint.chomp("/")
12
+ @api_key = args[:api_key] or raise ArgumentError.new("must pass api_key")
13
+ @secret_key = args[:secret_key] or raise ArgumentError.new("must pass secret_key")
14
+ @region = args[:region] || "default"
15
+ end
16
+
17
+ def heartbeat args
18
+ request_body = Reign.generate_heartbeat_body(args)
19
+ request_body = JSON.generate(request_body)
20
+ uri = Reign.generate_heartbeat_uri(args[:namespace])
21
+ headers = Reign.generate_header_for_request(:method => :post, :request_uri => uri, :api_key => @api_key,
22
+ :secret_key => @secret_key, :payload => request_body, :region => @region)
23
+ begin
24
+ return JSON.parse(RestClient::Request.execute(:method => :post, :url => @endpoint + uri,
25
+ :payload => request_body, :headers => headers))
26
+ rescue RestClient::ExceptionWithResponse => e
27
+ url = @endpoint + uri
28
+ raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}. payload = #{request_body}"
29
+ end
30
+ end
31
+
32
+ def get_regions
33
+ headers = Reign.generate_header_for_request(:method => :get, :request_uri => Reign::GET_REGIONS_URI, :api_key => @api_key,
34
+ :secret_key => @secret_key, :region => @region)
35
+ begin
36
+ return JSON.parse(RestClient::Request.execute(:method => :get, :url => @endpoint + Reign::GET_REGIONS_URI,
37
+ :headers => headers))
38
+ rescue RestClient::ExceptionWithResponse => e
39
+ url = @endpoint + uri
40
+ raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
41
+ end
42
+ end
43
+
44
+ def get_categories_in_namespace args
45
+ uri = Reign.generate_categories_uri(args[:namespace])
46
+ headers = Reign.generate_header_for_request(:method => :get, :request_uri => uri, :api_key => @api_key,
47
+ :secret_key => @secret_key, :region => @region)
48
+
49
+ begin
50
+ return JSON.parse(RestClient::Request.execute(:method => :get, :url => @endpoint + uri, :headers => headers))
51
+ rescue RestClient::ExceptionWithResponse => e
52
+ url = @endpoint + uri
53
+ raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
54
+ end
55
+ end
56
+
57
+ def get_nodes_in_category args
58
+ uri = Reign.generate_nodes_in_category_uri args
59
+ headers = Reign.generate_header_for_request(:method => :get, :request_uri => uri, :api_key => @api_key,
60
+ :secret_key => @secret_key, :region => @region)
61
+ begin
62
+ return JSON.parse(RestClient::Request.execute(:method => :get, :url => @endpoint + uri, :headers => headers))
63
+ rescue RestClient::ExceptionWithResponse => e
64
+ url = @endpoint + uri
65
+ raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
66
+ end
67
+ end
68
+
69
+ def upsert_key_value args
70
+ if args[:kv_data] == nil
71
+ raise ArgumentError.new("must pass some data in raw bytes for kv_data")
72
+ end
73
+ raw_data = Base64.strict_encode64(args[:kv_data])
74
+ uri = Reign.generate_upsert_kv_uri args
75
+ headers = Reign.generate_header_for_request(:method => :put, :request_uri => uri, :api_key => @api_key,
76
+ :secret_key => @secret_key, :region => @region, :payload => raw_data)
77
+ begin
78
+ response = RestClient::Request.execute(:method => :put, :url => @endpoint + uri, :headers => headers,
79
+ :payload => raw_data)
80
+ return response.headers[:etag]
81
+ rescue RestClient::ExceptionWithResponse => e
82
+ url = @endpoint + uri
83
+ raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
84
+ end
85
+ end
86
+
87
+ def get_key_value args
88
+ uri = Reign.generate_get_kv_uri args
89
+ headers = Reign.generate_header_for_request(:method => :get, :request_uri => uri, :api_key => @api_key,
90
+ :secret_key => @secret_key, :region => @region)
91
+ begin
92
+ response = RestClient::Request.execute(:method => :get, :url => @endpoint + uri, :headers => headers)
93
+ sig = response.headers[:etag]
94
+ owner_id = response.headers[:"reign_owner_id"]
95
+ data = Base64.strict_decode64(response.to_str)
96
+ return {:sig => sig, :kv_data => data, :owner_id => owner_id}
97
+ rescue RestClient::ExceptionWithResponse => e
98
+ url = @endpoint + uri
99
+ raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
100
+ end
101
+ end
102
+
103
+ def delete_key_value args
104
+ uri = Reign.generate_delete_kv_uri args
105
+ headers = Reign.generate_header_for_request(:method => :delete, :request_uri => uri, :api_key => @api_key,
106
+ :secret_key => @secret_key, :region => @region)
107
+ begin
108
+ RestClient::Request.execute(:method => :delete, :url => @endpoint + uri, :headers => headers)
109
+ rescue RestClient::ExceptionWithResponse => e
110
+ url = @endpoint + uri
111
+ raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
112
+ end
113
+ end
114
+
115
+ def acquire_lease args
116
+ uri = Reign.generate_acquire_lease_uri args
117
+ headers = Reign.generate_header_for_request(:method => :post, :request_uri => uri, :api_key => @api_key,
118
+ :secret_key => @secret_key, :region => @region)
119
+
120
+ begin
121
+ return JSON.parse(RestClient::Request.execute(:method => :post, :url => @endpoint + uri, :headers => headers))
122
+ rescue RestClient::ExceptionWithResponse => e
123
+ url = @endpoint + uri
124
+ raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
125
+ end
126
+ end
127
+
128
+ def release_lease args
129
+ uri = Reign.generate_release_lease_uri args
130
+ headers = Reign.generate_header_for_request(:method => :post, :request_uri => uri, :api_key => @api_key,
131
+ :secret_key => @secret_key, :region => @region)
132
+
133
+ begin
134
+ return RestClient::Request.execute(:method => :post, :url => @endpoint + uri, :headers => headers)
135
+ rescue RestClient::ExceptionWithResponse => e
136
+ url = @endpoint + uri
137
+ raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
138
+ end
139
+ end
140
+
141
+ def revoke_lease args
142
+ uri = Reign.generate_revoke_lease_uri args
143
+ headers = Reign.generate_header_for_request(:method => :post, :request_uri => uri, :api_key => @api_key,
144
+ :secret_key => @secret_key, :region => @region)
145
+ begin
146
+ return RestClient::Request.execute(:method => :post, :url => @endpoint + uri, :headers => headers)
147
+ rescue RestClient::ExceptionWithResponse => e
148
+ url = @endpoint + uri
149
+ raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
150
+ end
151
+ end
152
+
153
+ def get_lease_info args
154
+ uri = Reign.generate_get_lease_uri args
155
+ headers = Reign.generate_header_for_request(:method => :get, :request_uri => uri, :api_key => @api_key,
156
+ :secret_key => @secret_key, :region => @region)
157
+ begin
158
+ return JSON.parse(RestClient::Request.execute(:method => :get, :url => @endpoint + uri, :headers => headers))
159
+ rescue RestClient::ExceptionWithResponse => e
160
+ url = @endpoint + uri
161
+ raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
162
+ end
163
+ end
164
+
165
+ def create_watch_with_contacts args
166
+ args[:action] = "set"
167
+ return modify_watches_helper args
168
+ end
169
+
170
+ def add_contacts_to_watch args
171
+ args[:action] = "add"
172
+ if args[:sig] == nil
173
+ raise ArgumentError.new("Must pass sig parameter to modify watch")
174
+ end
175
+ return modify_watches_helper args
176
+ end
177
+
178
+ def remove_contacts_from_watch args
179
+ args[:action] = "rm"
180
+ if args[:sig] == nil
181
+ raise ArgumentError.new("Must pass sig parameter to modify watch")
182
+ end
183
+ return modify_watches_helper args
184
+ end
185
+
186
+ def get_watches args
187
+ uri = Reign.generate_watch_uri(args[:namespace], args[:watch_entity_path])
188
+ headers = Reign.generate_header_for_request(:method => :get, :request_uri => uri, :api_key => @api_key,
189
+ :secret_key => @secret_key, :region => @region)
190
+ return RestClient::Request.execute(:method => :get, :url => @endpoint + uri, :headers => headers)
191
+ end
192
+
193
+ def modify_watches_helper args
194
+ sig = args[:sig]
195
+ template = Reign.generate_watch_body_template(args[:owner_id], args[:emails], args[:webhooks])
196
+ request_payload = JSON.generate(template)
197
+
198
+ query_params = {"op" => args[:action]}
199
+ if sig != nil
200
+ query_params["sig"] = sig
201
+ end
202
+ uri = Reign.generate_watch_uri(args[:namespace], args[:watch_entity_path]) + Reign.generate_query_string(query_params)
203
+ headers = Reign.generate_header_for_request(:method => :post, :request_uri => uri, :api_key => @api_key,
204
+ :secret_key => @secret_key, :region => @region, :payload => request_payload)
205
+
206
+ begin
207
+ return JSON.parse(RestClient::Request.execute(:method => :post, :url => @endpoint + uri, :headers => headers,
208
+ :payload => request_payload))
209
+ rescue => e
210
+ url = @endpoint + uri
211
+ raise "Bad response from Reign server. Message: #{e.response}. URL: #{url}. Request body: #{request_payload}"
212
+ end
213
+ end
214
+ end
215
+ end
216
+
@@ -0,0 +1,11 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'reign-client'
3
+ s.version = '0.0.2'
4
+ s.summary = 'A simple blocking client for interaction against the Reign API.'
5
+ s.authors = ['Harrison Chu']
6
+ s.description = 'A simple blocking client for interaction against the Reign API.'
7
+ s.files = `git ls-files -z`.split("\0")
8
+
9
+ s.add_dependency('json', '~> 1.8')
10
+ s.add_dependency('rest-client', '~> 1.8')
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reign-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harrison Chu
@@ -43,7 +43,15 @@ email:
43
43
  executables: []
44
44
  extensions: []
45
45
  extra_rdoc_files: []
46
- files: []
46
+ files:
47
+ - ".gitignore"
48
+ - Gemfile
49
+ - Gemfile.lock
50
+ - LICENSE
51
+ - README.md
52
+ - lib/common.rb
53
+ - lib/reign-client.rb
54
+ - reign-client.gemspec
47
55
  homepage:
48
56
  licenses: []
49
57
  metadata: {}