etre-client 0.8.0 → 0.8.1

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: d7ee88bbc4eaef62c80b7fe886975cc09f6fe3fa
4
- data.tar.gz: 474060e882a53a6274772f2b21854651295d43f5
3
+ metadata.gz: 9a9a7c0360d700dabbe48fc8572a362ec8f2eae8
4
+ data.tar.gz: 73f22ebd75fd9621c33e72fa4fe10be856b368a5
5
5
  SHA512:
6
- metadata.gz: 5e3ec873dbc34c866833116196fce1b87a463a9f45360786bb48e6afb5c306bc1d7911a3c1d944736e81dee8dba6679793bf31afbeb63a2ad406b91930ea49d9
7
- data.tar.gz: b993c6f228ac3bc21f941d4a2cbd2b86eb0f43058a8e3e9a27da4082555507668312bcf446a257ce7eb0b9d1b19bb8cdbbd2fa3fee9fadbd19c38fef80e0413d
6
+ metadata.gz: f66a61bb2b27da32951328b2bbe2da2f29d7579fee33d780223f7af2a4dfc3ba0de7d38c6bbf7411625086ac4c684e60c72371652f4c8567d59a4da2857b464e
7
+ data.tar.gz: 7f9e30e3707341914aaad1a1adcaac4290c5a9f7b5572db162831f239ea1d3880dedfd727797481ea048167ed5eb1e0f10589b30567c672d505265397a06c853
data/.gitignore CHANGED
@@ -1,2 +1 @@
1
- # ignore gems
2
1
  *.gem
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3
4
+ script: bundle exec rake spec
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ Change Log
2
+ ==========
3
+
4
+ Version 0.8.0 *(2017-10-31)*
5
+ ----------------------------
6
+
7
+ Initial alpha release of etre-client.
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,17 @@
1
+ Contributing
2
+ ============
3
+
4
+ If you would like to contribute code to this project you can do so through GitHub by
5
+ forking the repository and sending a pull request.
6
+
7
+ When submitting code, please make every effort to follow existing conventions
8
+ and style in order to keep the code as readable as possible. Please also make
9
+ sure your code compiles by running `mvn clean verify`. Checkstyle failures
10
+ during compilation indicate errors in your style and can be viewed in the
11
+ `checkstyle-result.xml` file.
12
+
13
+ Before your code can be accepted into the project you must also sign the
14
+ [Individual Contributor License Agreement (CLA)][1].
15
+
16
+
17
+ [1]: https://spreadsheets.google.com/spreadsheet/viewform?formkey=dDViT2xzUHAwRkI3X3k5Z0lQM091OGc6MQ&ndplr=1
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 2017 Square, Inc.
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 CHANGED
@@ -1,27 +1,41 @@
1
+ ![Build Status](https://travis-ci.org/square/etre-client-ruby.svg?branch=master)
2
+
1
3
  Description
2
4
  ------
3
- etre-client is a gem that can be used to interact with an [Etre](https://github.com/square/etre) instance.
5
+ etre-client is a client gem for [Etre](https://github.com/square/etre).
4
6
 
5
7
  Installation
6
8
  ------
7
9
  etre-client is hosted on rubygems.org. To install it
8
10
  1. Add "etre-client" to your Gemfile
9
- 2. Run "bunndle install"
11
+ 2. Run "bundle install"
10
12
 
11
13
  Alternatively, you can just "gem install etre-client".
12
14
 
13
15
  Usage
14
16
  ------
17
+ Require the gem
18
+ ```
19
+ require 'etre-client'
20
+ ```
21
+
15
22
  Create a new client
16
23
  ```
17
- e = Etre::Client.new(entity_type: "node", url: "http://127.0.0.1:8080")
24
+ e = Etre::Client.new(entity_type: "node", url: "http://127.0.0.1:8080", ssl_cert: nil, ssl_key: nil, ssl_ca: nil, insecure: true)
18
25
  ```
19
26
 
20
27
  Insert entities
21
28
  ```
22
- entities = [{"foo" => "bar"}, {"foo" => "abc"}]
29
+ entities = [{"foo" => "bar"}, {"foo" => "abc"}, {"l1" => "a", "l2" => "b"}]
23
30
  e.insert(entities)
24
- => [{"id"=>"59f90caadd1b176f02eddcd8", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90caadd1b176f02eddcd8"}, {"id"=>"59f90caadd1b176f02eddcda", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90caadd1b176f02eddcda"}]
31
+ => [{"id"=>"59f90caadd1b176f02eddcd8", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90caadd1b176f02eddcd8"}, {"id"=>"59f90caadd1b176f02eddcda", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90caadd1b176f02eddcda"}, {"id"=>"59f90e3fdd1b176f02eddce5", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90e3fdd1b176f02eddce5"}]
32
+ ```
33
+
34
+ Read entities
35
+ ```
36
+ query = "foo=bar"
37
+ e.query(query)
38
+ => [{"_id"=>"59f90caadd1b176f02eddcd8", "_rev"=>0, "_type"=>"node", "foo"=>"bar"}]
25
39
  ```
26
40
 
27
41
  Update entities
@@ -35,30 +49,38 @@ e.update(query, patch)
35
49
  Update a single entity
36
50
  ```
37
51
  id = "59f90caadd1b176f02eddcda"
38
- patch = {"foo" => "newbar"}
52
+ patch = {"foo" => "slug"}
39
53
  e.update_one(id, patch)
40
-
41
54
  => {"id"=>"59f90caadd1b176f02eddcda", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90caadd1b176f02eddcda", "diff"=>{"_id"=>"59f90caadd1b176f02eddcda", "_rev"=>0, "_type"=>"node", "foo"=>"abc"}}
42
55
  ```
43
56
 
44
57
  Delete entities
45
58
  ```
46
- query = "foo=bar"
59
+ query = "foo=slug"
47
60
  e.delete(query)
61
+ => [{"id"=>"59f90caadd1b176f02eddcda", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90caadd1b176f02eddcda", "diff"=>{"_id"=>"59f90caadd1b176f02eddcda", "_rev"=>1, "_type"=>"node", "foo"=>"slug"}}]
48
62
  ```
49
63
 
50
64
  Delete a single entity
51
65
  ```
52
- id = "abc"
66
+ id = "59f90caadd1b176f02eddcd8"
53
67
  e.delete_one(id)
54
- => {"_id" => "abc", "foo" => "bar"}
68
+ => {"id"=>"59f90caadd1b176f02eddcd8", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90caadd1b176f02eddcd8", "diff"=>{"_id"=>"59f90caadd1b176f02eddcd8", "_rev"=>1, "_type"=>"node", "foo"=>"newbar"}}
55
69
  ```
56
70
 
57
71
  List the labels for an entity
58
72
  ```
59
- id = "abc"
73
+ id = "59f90e3fdd1b176f02eddce5"
60
74
  e.labels(id)
61
- => ["foo"]
75
+ => ["f1", "f2"]
76
+ ```
77
+
78
+ Delete the label on an entity
79
+ ```
80
+ id = "59f90e3fdd1b176f02eddce5"
81
+ label = "l1"
82
+ e.delete_label(id)
83
+ => {"id"=>"59f90e3fdd1b176f02eddce5", "uri"=>"127.0.0.1:8080/api/v1/entity/59f90e3fdd1b176f02eddce5", "diff"=>{"_id"=>"59f90e3fdd1b176f02eddce5", "_rev"=>0, "_type"=>"node", "l1"=>"a", "l2"=>"b"}}
62
84
  ```
63
85
 
64
86
  Development
data/etre-client.gemspec CHANGED
@@ -6,7 +6,6 @@ require 'etre-client/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'etre-client'
8
8
  spec.version = Etre::Client::VERSION
9
- spec.version = '0.8.0'
10
9
  spec.license = 'Apache-2.0'
11
10
  spec.authors = ['Michael Finch']
12
11
  spec.email = ['mfinch@squareup.com']
@@ -0,0 +1,306 @@
1
+ require 'etre-client/errors'
2
+ require 'rest-client'
3
+ require 'json'
4
+
5
+ module Etre
6
+ class Client
7
+ attr_reader :entity_type, :url
8
+
9
+ API_ROOT = "/api/v1"
10
+ META_LABEL_ID = "_id"
11
+ META_LABEL_TYPE = "_type"
12
+
13
+ def initialize(entity_type:, url:, ssl_cert: nil, ssl_key: nil, ssl_ca: nil, insecure: true)
14
+ @entity_type = entity_type
15
+ @url = url
16
+
17
+ @ssl_cert = ssl_cert
18
+ @ssl_key = ssl_key
19
+ @ssl_ca = ssl_ca
20
+ @insecure = insecure
21
+ end
22
+
23
+ # query returns an array of entities that satisfy a query.
24
+ def query(query, filter = nil)
25
+ if query.nil? || query.empty?
26
+ raise QueryNotProvided
27
+ end
28
+
29
+ # @todo: translate filter to query params
30
+
31
+ # Do the normal GET /entities?query unless the query is ~2k characters
32
+ # becuase that brings the entire URL length close to the max supported
33
+ # limit across most HTTP servers. In that case, switch to alternate
34
+ # endpoint to POST the long query.
35
+ if query.length < 2000
36
+ # Always escape the query.
37
+ query = CGI::escape(query)
38
+
39
+ begin
40
+ resp = etre_get("/entities/#{@entity_type}?query=#{query}")
41
+ rescue RestClient::ExceptionWithResponse => e
42
+ raise RequestFailed, e.response
43
+ end
44
+ else
45
+ # DO NOT ESCAPE THE QUERY! It's not sent via URL, so no escaping needed.
46
+ begin
47
+ resp = etre_post("/query/#{@entity_type}", query)
48
+ rescue RestClient::ExceptionWithResponse => e
49
+ raise RequestFailed, e.response
50
+ end
51
+ end
52
+
53
+ if resp.code != 200
54
+ raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
55
+ end
56
+
57
+ return JSON.parse(resp.body)
58
+ end
59
+
60
+ # insert inserts an array of entities.
61
+ def insert(entities)
62
+ if entities.nil? || !entities.any?
63
+ raise EntityNotProvided
64
+ end
65
+
66
+ entities.each do |e|
67
+ if e.key?(META_LABEL_ID)
68
+ raise EntityIdSet, "entity: #{e}"
69
+ end
70
+
71
+ if e.key?(META_LABEL_TYPE) && e[META_LABEL_TYPE] != @entity_type
72
+ raise EntityTypeMismatch, "only valid type is '#{@entity_type}', but " +
73
+ "entity has type '#{e[META_LABEL_TYPE]}'"
74
+ end
75
+ end
76
+
77
+ begin
78
+ resp = etre_post("/entities/#{@entity_type}", entities)
79
+ rescue RestClient::ExceptionWithResponse => e
80
+ raise RequestFailed, e.response
81
+ end
82
+
83
+ if ![200, 201].include?(resp.code)
84
+ raise UnexpectedResponseCode, "expected 200 or 201, got #{resp.code}"
85
+ end
86
+
87
+ return JSON.parse(resp.body)
88
+ end
89
+
90
+ # update updates entities with the given patch that satisfy the given query.
91
+ def update(query, patch)
92
+ if query.nil? || query.empty?
93
+ raise QueryNotProvided
94
+ end
95
+
96
+ # Always escape the query.
97
+ query = CGI::escape(query)
98
+
99
+ if patch.nil? || !patch.any?
100
+ raise PatchNotProvided
101
+ end
102
+
103
+ if patch.key?(META_LABEL_ID)
104
+ raise PatchIdSet, "patch: #{patch}"
105
+ end
106
+
107
+ if patch.key?(META_LABEL_TYPE) && patch[META_LABEL_TYPE] != @entity_type
108
+ raise EntityTypeMismatch, "only valid type is '#{@entity_type}', but " +
109
+ "patch has type '#{patch[META_LABEL_TYPE]}'"
110
+ end
111
+
112
+ begin
113
+ resp = etre_put("/entities/#{@entity_type}?query=#{query}", patch)
114
+ rescue RestClient::ExceptionWithResponse => e
115
+ raise RequestFailed, e.response
116
+ end
117
+
118
+ if ![200, 201].include?(resp.code)
119
+ raise UnexpectedResponseCode, "expected 200 or 201, got #{resp.code}"
120
+ end
121
+
122
+ return JSON.parse(resp.body)
123
+ end
124
+
125
+ # update_one updates the given entity id with the provided patch.
126
+ def update_one(id, patch)
127
+ if id.nil? || id.empty?
128
+ raise IdNotProvided
129
+ end
130
+
131
+ if patch.nil? || !patch.any?
132
+ raise PatchNotProvided
133
+ end
134
+
135
+ if patch.key?(META_LABEL_ID)
136
+ raise PatchIdSet, "patch: #{patch}"
137
+ end
138
+
139
+ if patch.key?(META_LABEL_TYPE) && patch[META_LABEL_TYPE] != @entity_type
140
+ raise EntityTypeMismatch, "only valid type is '#{@entity_type}', but " +
141
+ "patch has type '#{patch[META_LABEL_TYPE]}'"
142
+ end
143
+
144
+ begin
145
+ resp = etre_put("/entity/#{@entity_type}/#{id}", patch)
146
+ rescue RestClient::ExceptionWithResponse => e
147
+ raise RequestFailed, e.response
148
+ end
149
+
150
+ if ![200, 201].include?(resp.code)
151
+ raise UnexpectedResponseCode, "expected 200 or 201, got #{resp.code}"
152
+ end
153
+
154
+ return JSON.parse(resp.body)
155
+ end
156
+
157
+ # delete deletes the entities that satisfy the given query.
158
+ def delete(query)
159
+ if query.nil? || query.empty?
160
+ raise QueryNotProvided
161
+ end
162
+
163
+ # Always escape the query.
164
+ query = CGI::escape(query)
165
+
166
+ begin
167
+ resp = etre_delete("/entities/#{@entity_type}?query=#{query}")
168
+ rescue RestClient::ExceptionWithResponse => e
169
+ raise RequestFailed, e.response
170
+ end
171
+
172
+ if resp.code != 200
173
+ raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
174
+ end
175
+
176
+ return JSON.parse(resp.body)
177
+ end
178
+
179
+ # delete_one deletes the entity with the given id.
180
+ def delete_one(id)
181
+ if id.nil? || id.empty?
182
+ raise IdNotProvided
183
+ end
184
+
185
+ begin
186
+ resp = etre_delete("/entity/#{@entity_type}/#{id}")
187
+ rescue RestClient::ExceptionWithResponse => e
188
+ raise RequestFailed, e.response
189
+ end
190
+
191
+ if resp.code != 200
192
+ raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
193
+ end
194
+
195
+ return JSON.parse(resp.body)
196
+ end
197
+
198
+ # labels returns an array of labels for the given entity id.
199
+ def labels(id)
200
+ if id.nil? || id.empty?
201
+ raise IdNotProvided
202
+ end
203
+
204
+ begin
205
+ resp = etre_get("/entity/#{@entity_type}/#{id}/labels")
206
+ rescue RestClient::ExceptionWithResponse => e
207
+ raise RequestFailed, e.response
208
+ end
209
+
210
+ if resp.code != 200
211
+ raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
212
+ end
213
+
214
+ return JSON.parse(resp.body)
215
+ end
216
+
217
+ # delete_label deletes the given label on the provided entity id.
218
+ def delete_label(id, label)
219
+ if id.nil? || id.empty?
220
+ raise IdNotProvided
221
+ end
222
+
223
+ if label.nil? || label.empty?
224
+ raise LabelNotSet
225
+ end
226
+
227
+ begin
228
+ resp = etre_delete("/entity/#{@entity_type}/#{id}/labels/#{label}")
229
+ rescue RestClient::ExceptionWithResponse => e
230
+ raise RequestFailed, e.response
231
+ end
232
+
233
+ if resp.code != 200
234
+ raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
235
+ end
236
+
237
+ return JSON.parse(resp.body)
238
+ end
239
+
240
+ private
241
+
242
+ def etre_get(route)
243
+ resource_for_route(route).get(
244
+ get_headers,
245
+ )
246
+ end
247
+
248
+ def etre_post(route, params = nil)
249
+ resource_for_route(route).post(
250
+ params.to_json,
251
+ post_headers,
252
+ )
253
+ end
254
+
255
+ def etre_put(route, params = nil)
256
+ resource_for_route(route).put(
257
+ params.to_json,
258
+ put_headers,
259
+ )
260
+ end
261
+
262
+ def etre_delete(route)
263
+ resource_for_route(route).delete(
264
+ delete_headers,
265
+ )
266
+ end
267
+
268
+ def get_headers
269
+ {:accept => 'application/json'}
270
+ end
271
+
272
+ def post_headers
273
+ get_headers.merge!(:content_type => 'application/json')
274
+ end
275
+
276
+ def put_headers
277
+ post_headers
278
+ end
279
+
280
+ def delete_headers
281
+ get_headers
282
+ end
283
+
284
+ def resource_for_route(route)
285
+ opts = {}
286
+ opts.merge!(ssl_options) unless @insecure
287
+ RestClient::Resource.new(
288
+ @url + API_ROOT + route,
289
+ opts
290
+ )
291
+ end
292
+
293
+ def ssl_options
294
+ {
295
+ :ssl_client_cert => OpenSSL::X509::Certificate.new(File.read(@ssl_cert)),
296
+ :ssl_client_key => OpenSSL::PKey::RSA.new(File.read(@ssl_key)),
297
+ :ssl_ca_file => @ssl_ca,
298
+ :verify_ssl => OpenSSL::SSL::VERIFY_PEER,
299
+ }
300
+ end
301
+
302
+ def parse_response(response)
303
+ JSON.parse(response)
304
+ end
305
+ end
306
+ end
@@ -8,6 +8,7 @@ module Etre
8
8
  class PatchIdSet < StandardError; end
9
9
  class PatchNotProvided < StandardError; end
10
10
  class QueryNotProvided < StandardError; end
11
+ class RequestFailed < StandardError; end
11
12
  class UnexpectedResponseCode < StandardError; end
12
13
  end
13
14
  end
@@ -1,5 +1,5 @@
1
1
  module Etre
2
2
  class Client
3
- VERSION = "0.8.0"
3
+ VERSION = "0.8.1"
4
4
  end
5
5
  end
data/lib/etre-client.rb CHANGED
@@ -1,264 +1,2 @@
1
+ require 'etre-client/client'
1
2
  require 'etre-client/errors'
2
- require 'rest-client'
3
- require 'json'
4
-
5
- module Etre
6
- class Client
7
- attr_reader :entity_type, :url
8
-
9
- API_ROOT = "/api/v1"
10
- META_LABEL_ID = "_id"
11
- META_LABEL_TYPE = "_type"
12
-
13
- def initialize(entity_type:, url:, ssl_cert: nil, ssl_key: nil, ssl_ca: nil, insecure: true)
14
- @entity_type = entity_type
15
- @url = url
16
-
17
- @ssl_cert = ssl_cert
18
- @ssl_key = ssl_key
19
- @ssl_ca = ssl_ca
20
- @insecure = insecure
21
- end
22
-
23
- # query returns an array of entities that satisfy a query.
24
- def query(query, filter = nil)
25
- if query.nil? || query.empty?
26
- raise QueryNotProvided
27
- end
28
-
29
- # @todo: translate filter to query params
30
-
31
- # Do the normal GET /entities?query unless the query is ~2k characters
32
- # becuase that brings the entire URL length close to the max supported
33
- # limit across most HTTP servers. In that case, switch to alternate
34
- # endpoint to POST the long query.
35
- if query.length < 2000
36
- # Always escape the query.
37
- query = CGI::escape(query)
38
-
39
- resp = etre_get("/entities/#{@entity_type}?query=#{query}")
40
- else
41
- # DO NOT ESCAPE THE QUERY! It's not sent via URL, so no escaping needed.
42
- resp = etre_post("/query/#{@entity_type}", query)
43
- end
44
-
45
- if resp.code != 200
46
- raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
47
- end
48
-
49
- return JSON.parse(resp.body)
50
- end
51
-
52
- # insert inserts an array of entities.
53
- def insert(entities)
54
- if entities.nil? || !entities.any?
55
- raise EntityNotProvided
56
- end
57
-
58
- entities.each do |e|
59
- if e.key?(META_LABEL_ID)
60
- raise EntityIdSet, "entity: #{e}"
61
- end
62
-
63
- if e.key?(META_LABEL_TYPE) && e[META_LABEL_TYPE] != @entity_type
64
- raise EntityTypeMismatch, "only valid type is '#{@entity_type}', but " +
65
- "entity has type '#{e[META_LABEL_TYPE]}'"
66
- end
67
- end
68
-
69
- resp = etre_post("/entities/#{@entity_type}", entities)
70
- if ![200, 201].include?(resp.code)
71
- raise UnexpectedResponseCode, "expected 200 or 201, got #{resp.code}"
72
- end
73
-
74
- return JSON.parse(resp.body)
75
- end
76
-
77
- # update updates entities with the given patch that satisfy the given query.
78
- def update(query, patch)
79
- if query.nil? || query.empty?
80
- raise QueryNotProvided
81
- end
82
-
83
- # Always escape the query.
84
- query = CGI::escape(query)
85
-
86
- if patch.nil? || !patch.any?
87
- raise PatchNotProvided
88
- end
89
-
90
- if patch.key?(META_LABEL_ID)
91
- raise PatchIdSet, "patch: #{patch}"
92
- end
93
-
94
- if patch.key?(META_LABEL_TYPE) && patch[META_LABEL_TYPE] != @entity_type
95
- raise EntityTypeMismatch, "only valid type is '#{@entity_type}', but " +
96
- "patch has type '#{patch[META_LABEL_TYPE]}'"
97
- end
98
-
99
- resp = etre_put("/entities/#{@entity_type}?query=#{query}", patch)
100
- if ![200, 201].include?(resp.code)
101
- raise UnexpectedResponseCode, "expected 200 or 201, got #{resp.code}"
102
- end
103
-
104
- return JSON.parse(resp.body)
105
- end
106
-
107
- # update_one updates the given entity id with the provided patch.
108
- def update_one(id, patch)
109
- if id.nil? || id.empty?
110
- raise IdNotProvided
111
- end
112
-
113
- if patch.nil? || !patch.any?
114
- raise PatchNotProvided
115
- end
116
-
117
- if patch.key?(META_LABEL_ID)
118
- raise PatchIdSet, "patch: #{patch}"
119
- end
120
-
121
- if patch.key?(META_LABEL_TYPE) && patch[META_LABEL_TYPE] != @entity_type
122
- raise EntityTypeMismatch, "only valid type is '#{@entity_type}', but " +
123
- "patch has type '#{patch[META_LABEL_TYPE]}'"
124
- end
125
-
126
- resp = etre_put("/entity/#{@entity_type}/#{id}", patch)
127
- if ![200, 201].include?(resp.code)
128
- raise UnexpectedResponseCode, "expected 200 or 201, got #{resp.code}"
129
- end
130
-
131
- return JSON.parse(resp.body)
132
- end
133
-
134
- # delete deletes the entities that satisfy the given query.
135
- def delete(query)
136
- if query.nil? || query.empty?
137
- raise QueryNotProvided
138
- end
139
-
140
- # Always escape the query.
141
- query = CGI::escape(query)
142
-
143
- resp = etre_delete("/entities/#{@entity_type}?query=#{query}")
144
- if resp.code != 200
145
- raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
146
- end
147
-
148
- return JSON.parse(resp.body)
149
- end
150
-
151
- # delete_one deletes the entity with the given id.
152
- def delete_one(id)
153
- if id.nil? || id.empty?
154
- raise IdNotProvided
155
- end
156
-
157
- resp = etre_delete("/entity/#{@entity_type}/#{id}")
158
- if resp.code != 200
159
- raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
160
- end
161
-
162
- return JSON.parse(resp.body)
163
- end
164
-
165
- # labels returns an array of labels for the given entity id.
166
- def labels(id)
167
- if id.nil? || id.empty?
168
- raise IdNotProvided
169
- end
170
-
171
- resp = etre_get("/entity/#{@entity_type}/#{id}/labels")
172
-
173
- if resp.code != 200
174
- raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
175
- end
176
-
177
- return JSON.parse(resp.body)
178
- end
179
-
180
- # delete_label deletes the given label on the provided entity id.
181
- def delete_label(id, label)
182
- if id.nil? || id.empty?
183
- raise IdNotProvided
184
- end
185
-
186
- if label.nil? || label.empty?
187
- raise LabelNotSet
188
- end
189
-
190
- resp = etre_delete("/entity/#{@entity_type}/#{id}/labels/#{label}")
191
- if resp.code != 200
192
- raise UnexpectedResponseCode, "expected 200, got #{resp.code}"
193
- end
194
-
195
- return JSON.parse(resp.body)
196
- end
197
-
198
- private
199
-
200
- def etre_get(route)
201
- resource_for_route(route).get(
202
- get_headers,
203
- )
204
- end
205
-
206
- def etre_post(route, params = nil)
207
- resource_for_route(route).post(
208
- params.to_json,
209
- post_headers,
210
- )
211
- end
212
-
213
- def etre_put(route, params = nil)
214
- resource_for_route(route).put(
215
- params.to_json,
216
- put_headers,
217
- )
218
- end
219
-
220
- def etre_delete(route)
221
- resource_for_route(route).delete(
222
- delete_headers,
223
- )
224
- end
225
-
226
- def get_headers
227
- {:accept => 'application/json'}
228
- end
229
-
230
- def post_headers
231
- get_headers.merge!(:content_type => 'application/json')
232
- end
233
-
234
- def put_headers
235
- post_headers
236
- end
237
-
238
- def delete_headers
239
- get_headers
240
- end
241
-
242
- def resource_for_route(route)
243
- opts = {}
244
- opts.merge!(ssl_options) unless @insecure
245
- RestClient::Resource.new(
246
- @url + API_ROOT + route,
247
- opts
248
- )
249
- end
250
-
251
- def ssl_options
252
- {
253
- :ssl_client_cert => OpenSSL::X509::Certificate.new(File.read(@ssl_cert)),
254
- :ssl_client_key => OpenSSL::PKey::RSA.new(File.read(@ssl_key)),
255
- :ssl_ca_file => @ssl_ca,
256
- :verify_ssl => OpenSSL::SSL::VERIFY_PEER,
257
- }
258
- end
259
-
260
- def parse_response(response)
261
- JSON.parse(response)
262
- end
263
- end
264
- end
data/spec/client_spec.rb CHANGED
@@ -7,8 +7,6 @@ describe Etre::Client do
7
7
  let(:post_headers) { get_headers.merge({:content_type => "application/json"}) }
8
8
  let(:put_headers) { post_headers }
9
9
  let(:delete_headers) { get_headers }
10
- let(:migration_id) { 3 }
11
- let(:cluster) { "cluster-001" }
12
10
  let(:entity1) { {"_id" => "abc", "foo" => "bar"} }
13
11
  let(:entity2) { {"oof" => "rab"} }
14
12
  let(:entity3) { {"blah" => "slug"} }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: etre-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Finch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-31 00:00:00.000000000 Z
11
+ date: 2017-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -80,12 +80,16 @@ extensions: []
80
80
  extra_rdoc_files: []
81
81
  files:
82
82
  - ".gitignore"
83
+ - ".travis.yml"
84
+ - CHANGELOG.md
85
+ - CONTRIBUTING.md
83
86
  - Gemfile
84
- - Gemfile.lock
87
+ - LICENSE
85
88
  - README.md
86
89
  - Rakefile
87
90
  - etre-client.gemspec
88
91
  - lib/etre-client.rb
92
+ - lib/etre-client/client.rb
89
93
  - lib/etre-client/errors.rb
90
94
  - lib/etre-client/version.rb
91
95
  - spec/client_spec.rb
data/Gemfile.lock DELETED
@@ -1,97 +0,0 @@
1
- GEM
2
- remote: https://rubygems.org/
3
- specs:
4
- actionpack (5.1.4)
5
- actionview (= 5.1.4)
6
- activesupport (= 5.1.4)
7
- rack (~> 2.0)
8
- rack-test (>= 0.6.3)
9
- rails-dom-testing (~> 2.0)
10
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
11
- actionview (5.1.4)
12
- activesupport (= 5.1.4)
13
- builder (~> 3.1)
14
- erubi (~> 1.4)
15
- rails-dom-testing (~> 2.0)
16
- rails-html-sanitizer (~> 1.0, >= 1.0.3)
17
- activesupport (5.1.4)
18
- concurrent-ruby (~> 1.0, >= 1.0.2)
19
- i18n (~> 0.7)
20
- minitest (~> 5.1)
21
- tzinfo (~> 1.1)
22
- builder (3.2.3)
23
- concurrent-ruby (1.0.5)
24
- crass (1.0.2)
25
- diff-lcs (1.3)
26
- domain_name (0.5.20170404)
27
- unf (>= 0.0.5, < 1.0.0)
28
- erubi (1.7.0)
29
- http-cookie (1.0.3)
30
- domain_name (~> 0.5)
31
- i18n (0.9.0)
32
- concurrent-ruby (~> 1.0)
33
- loofah (2.1.1)
34
- crass (~> 1.0.2)
35
- nokogiri (>= 1.5.9)
36
- method_source (0.9.0)
37
- mime-types (3.1)
38
- mime-types-data (~> 3.2015)
39
- mime-types-data (3.2016.0521)
40
- mini_portile2 (2.3.0)
41
- minitest (5.10.3)
42
- netrc (0.11.0)
43
- nokogiri (1.8.1)
44
- mini_portile2 (~> 2.3.0)
45
- rack (2.0.3)
46
- rack-test (0.7.0)
47
- rack (>= 1.0, < 3)
48
- rails-dom-testing (2.0.3)
49
- activesupport (>= 4.2.0)
50
- nokogiri (>= 1.6)
51
- rails-html-sanitizer (1.0.3)
52
- loofah (~> 2.0)
53
- railties (5.1.4)
54
- actionpack (= 5.1.4)
55
- activesupport (= 5.1.4)
56
- method_source
57
- rake (>= 0.8.7)
58
- thor (>= 0.18.1, < 2.0)
59
- rake (12.2.1)
60
- rest-client (2.0.2)
61
- http-cookie (>= 1.0.2, < 2.0)
62
- mime-types (>= 1.16, < 4.0)
63
- netrc (~> 0.8)
64
- rspec-core (3.7.0)
65
- rspec-support (~> 3.7.0)
66
- rspec-expectations (3.7.0)
67
- diff-lcs (>= 1.2.0, < 2.0)
68
- rspec-support (~> 3.7.0)
69
- rspec-mocks (3.7.0)
70
- diff-lcs (>= 1.2.0, < 2.0)
71
- rspec-support (~> 3.7.0)
72
- rspec-rails (3.7.1)
73
- actionpack (>= 3.0)
74
- activesupport (>= 3.0)
75
- railties (>= 3.0)
76
- rspec-core (~> 3.7.0)
77
- rspec-expectations (~> 3.7.0)
78
- rspec-mocks (~> 3.7.0)
79
- rspec-support (~> 3.7.0)
80
- rspec-support (3.7.0)
81
- thor (0.20.0)
82
- thread_safe (0.3.6)
83
- tzinfo (1.2.4)
84
- thread_safe (~> 0.1)
85
- unf (0.1.4)
86
- unf_ext
87
- unf_ext (0.0.7.4)
88
-
89
- PLATFORMS
90
- ruby
91
-
92
- DEPENDENCIES
93
- rest-client
94
- rspec-rails
95
-
96
- BUNDLED WITH
97
- 1.15.4