warrant 1.1.0 → 2.0.0.rc1

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
  SHA256:
3
- metadata.gz: 6fc7f30432a9ab516978da52e77811011ee0625a819124ad8dcb5b20d8610a34
4
- data.tar.gz: 54dc068a58982fc5f8fdcc2d781ce5b6b5ec320c3f7a5dfdbd8338eaa20fac93
3
+ metadata.gz: e4b223824bcd7432223d20591698f3160529fbe71d846fc608e389d6733b4ef2
4
+ data.tar.gz: 59ac3e03836bc44313afa2377875313573b2d4fe15627ab1bde09de1f74381ee
5
5
  SHA512:
6
- metadata.gz: 4e892e951b8107a9d6bdeb621a1ee2fb1674c8a9195eb7271e127f77961d5cfa8fe673e93cfd6bb2784452630ade845b65aa3455379c169580ea8b32fb9dad2f
7
- data.tar.gz: b7e7c396ff14cbc500b9bf1e1882db09b84002e78fc2ffa11a019de0cc3da25d60766f78b6650075baa648125c87ed9ed48e8cf250cc2a41818fef5b3d2b9209
6
+ metadata.gz: 2c3ac854c8720abdbc663df3e943299b262602d8d14296ff9595845b01961c8ead99f77c07c538f6b29319916007e6aba0ed859b95ac38b2912099e067ef521f
7
+ data.tar.gz: ae74c5ffce7718d20c758f922d2ab384cde9b050d71bafc08a0a37452f39ebf482bb45d533dc7598396e32f76fc0d5c890002f3c5d49947b05bc4701c79242d7
data/Gemfile CHANGED
@@ -8,3 +8,7 @@ gemspec
8
8
  gem "rake"
9
9
  gem "minitest", "~> 5.0"
10
10
  gem "rubocop", "~> 1.21"
11
+
12
+ group :test do
13
+ gem "webmock"
14
+ end
@@ -8,7 +8,8 @@ module Warrant
8
8
  http = Net::HTTP.new(uri.host, uri.port)
9
9
  http.use_ssl = use_ssl
10
10
  headers = {
11
- "Authorization": "ApiKey #{::Warrant.config.api_key}"
11
+ "Authorization": "ApiKey #{::Warrant.config.api_key}",
12
+ "User-Agent": "warrant-ruby/#{VERSION}"
12
13
  }
13
14
  http.post(uri.path, params.to_json, headers)
14
15
  end
@@ -18,6 +19,7 @@ module Warrant
18
19
  http.use_ssl = true
19
20
  request = Net::HTTP::Delete.new(uri.path)
20
21
  request["Authorization"] = "ApiKey #{::Warrant.config.api_key}"
22
+ request["User-Agent"] = "warrant-ruby/#{VERSION}"
21
23
 
22
24
  http.request(request, params.to_json)
23
25
  end
@@ -26,7 +28,8 @@ module Warrant
26
28
  http = Net::HTTP.new(uri.host, uri.port)
27
29
  http.use_ssl = true
28
30
  headers = {
29
- "Authorization": "ApiKey #{::Warrant.config.api_key}"
31
+ "Authorization": "ApiKey #{::Warrant.config.api_key}",
32
+ "User-Agent": "warrant-ruby/#{VERSION}"
30
33
  }
31
34
 
32
35
  unless params.empty?
@@ -41,7 +44,8 @@ module Warrant
41
44
  http = Net::HTTP.new(uri.host, uri.port)
42
45
  http.use_ssl = true
43
46
  headers = {
44
- "Authorization": "ApiKey #{::Warrant.config.api_key}"
47
+ "Authorization": "ApiKey #{::Warrant.config.api_key}",
48
+ "User-Agent": "warrant-ruby/#{VERSION}"
45
49
  }
46
50
  http.put(uri.path, params.to_json, headers)
47
51
  end
@@ -52,6 +56,8 @@ module Warrant
52
56
  case error_code
53
57
  when Error::DUPLICATE_RECORD_ERROR
54
58
  raise DuplicateRecordError.initialize_error_from_response(response)
59
+ when Error::FORBIDDEN_ERROR
60
+ raise ForbiddenError.initialize_error_from_response(response)
55
61
  when Error::INTERNAL_ERROR
56
62
  raise InternalError.initialize_error_from_response(response)
57
63
  when Error::INVALID_REQUEST_ERROR
@@ -3,6 +3,7 @@
3
3
  module Warrant
4
4
  class Error
5
5
  DUPLICATE_RECORD_ERROR = "duplicate_record"
6
+ FORBIDDEN_ERROR = "forbidden"
6
7
  INTERNAL_ERROR = "internal_error"
7
8
  INVALID_REQUEST_ERROR = "invalid_request"
8
9
  INVALID_PARAMETER_ERROR = "invalid_parameter"
@@ -38,6 +39,7 @@ module Warrant
38
39
  end
39
40
 
40
41
  class DuplicateRecordError < WarrantError; end
42
+ class ForbiddenError < WarrantError; end
41
43
  class InternalError < WarrantError; end
42
44
  class InvalidRequestError < WarrantError; end
43
45
  class InvalidParameterError < WarrantError; end
@@ -0,0 +1,327 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Warrant
4
+ class Feature
5
+ include Warrant::WarrantObject
6
+
7
+ attr_reader :feature_id
8
+
9
+ # @!visibility private
10
+ def initialize(feature_id)
11
+ @feature_id = feature_id
12
+ end
13
+
14
+ # Creates a feature with the given parameters
15
+ #
16
+ # @option params [String] :feature_id A string identifier for this new feature. The feature_id can only be composed of lower-case alphanumeric chars and/or '-' and '_'.
17
+ #
18
+ # @return [Feature] created feature
19
+ #
20
+ # @example Create a new Feature with the feature id "test-feature"
21
+ # Warrant::Feature.create(feature_id: "test-feature")
22
+ #
23
+ # @raise [Warrant::DuplicateRecordError]
24
+ # @raise [Warrant::InternalError]
25
+ # @raise [Warrant::InvalidRequestError]
26
+ # @raise [Warrant::UnauthorizedError]
27
+ def self.create(params = {})
28
+ res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v1/features"), Util.normalize_params(params))
29
+
30
+ case res
31
+ when Net::HTTPSuccess
32
+ res_json = JSON.parse(res.body)
33
+ Feature.new(res_json['featureId'])
34
+ else
35
+ APIOperations.raise_error(res)
36
+ end
37
+ end
38
+
39
+ # Deletes a feature with given feature id
40
+ #
41
+ # @param feature_id [String] The feature_id of the feature to delete.
42
+ #
43
+ # @return [nil] if delete was successful
44
+ #
45
+ # @example Delete a Feature with the feature id "test-feature"
46
+ # Warrant::Feature.delete("test-feature")
47
+ #
48
+ # @raise [Warrant::InternalError]
49
+ # @raise [Warrant::InvalidParameterError]
50
+ # @raise [Warrant::NotFoundError]
51
+ # @raise [Warrant::UnauthorizedError]
52
+ def self.delete(feature_id)
53
+ res = APIOperations.delete(URI.parse("#{::Warrant.config.api_base}/v1/features/#{feature_id}"))
54
+
55
+ case res
56
+ when Net::HTTPSuccess
57
+ return
58
+ else
59
+ APIOperations.raise_error(res)
60
+ end
61
+ end
62
+
63
+ # Lists all features for your organization
64
+ #
65
+ # @option filters [Integer] :page A positive integer (starting with 1) representing the page of items to return in response. Used in conjunction with the limit param. (optional)
66
+ # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
67
+ #
68
+ # @return [Array<Feature>] all features for your organization
69
+ #
70
+ # @example List all features
71
+ # Warrant::Feature.list()
72
+ #
73
+ # @raise [Warrant::InternalError]
74
+ # @raise [Warrant::UnauthorizedError]
75
+ def self.list(filters = {})
76
+ res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/features"), Util.normalize_params(filters))
77
+
78
+ case res
79
+ when Net::HTTPSuccess
80
+ features = JSON.parse(res.body)
81
+ features.map{ |feature| Feature.new(feature['featureId']) }
82
+ else
83
+ APIOperations.raise_error(res)
84
+ end
85
+ end
86
+
87
+ # Get a feature with the given feature_id
88
+ #
89
+ # @param feature_id [String] The feature_id of the feature to retrieve.
90
+ #
91
+ # @return [Feature] retrieved feature
92
+ #
93
+ # @raise [Warrant::InternalError]
94
+ # @raise [Warrant::InvalidParameterError]
95
+ # @raise [Warrant::NotFoundError]
96
+ # @raise [Warrant::UnauthorizedError]
97
+ def self.get(feature_id)
98
+ res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/features/#{feature_id}"))
99
+
100
+ case res
101
+ when Net::HTTPSuccess
102
+ feature = JSON.parse(res.body)
103
+ Feature.new(feature['featureId'])
104
+ else
105
+ APIOperations.raise_error(res)
106
+ end
107
+ end
108
+
109
+ # List features for tenant
110
+ #
111
+ # @param tenant_id [String] The tenant_id of the tenant to list features for.
112
+ # @option filters [Integer] :page A positive integer (starting with 1) representing the page of items to return in response. Used in conjunction with the limit param. (optional)
113
+ # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
114
+ #
115
+ # @return [Array<Feature>] assigned features for the tenant
116
+ #
117
+ # @raise [Warrant::InternalError]
118
+ # @raise [Warrant::InvalidParameterError]
119
+ # @raise [Warrant::UnauthorizedError]
120
+ def self.list_for_tenant(tenant_id, filters = {})
121
+ res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/tenants/#{tenant_id}/features"), Util.normalize_params(filters))
122
+
123
+ case res
124
+ when Net::HTTPSuccess
125
+ features = JSON.parse(res.body)
126
+ features.map{ |feature| Feature.new(feature['featureId']) }
127
+ else
128
+ APIOperations.raise_error(res)
129
+ end
130
+ end
131
+
132
+ # Assign a feature to a tenant
133
+ #
134
+ # @param tenant_id [String] The tenant_id of the tenant you want to assign a feature to.
135
+ # @param feature_id [String] The feature_id of the feature you want to assign to a tenant.
136
+ #
137
+ # @return [Feature] assigned feature
138
+ #
139
+ # @raise [Warrant::DuplicateRecordError]
140
+ # @raise [Warrant::InternalError]
141
+ # @raise [Warrant::InvalidParameterError]
142
+ # @raise [Warrant::NotFoundError]
143
+ # @raise [Warrant::UnauthorizedError]
144
+ def self.assign_to_tenant(tenant_id, feature_id)
145
+ res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v1/tenants/#{tenant_id}/features/#{feature_id}"))
146
+
147
+ case res
148
+ when Net::HTTPSuccess
149
+ feature = JSON.parse(res.body)
150
+ Feature.new(feature['featureId'])
151
+ else
152
+ APIOperations.raise_error(res)
153
+ end
154
+ end
155
+
156
+ # Remove a feature from a tenant
157
+ #
158
+ # @param tenant_id [String] The tenant_id of the tenant you want to remove a feature from.
159
+ # @param feature_id [String] The feature_id of the feature you want to remove from a tenant.
160
+ #
161
+ # @return [nil] if remove was successful
162
+ #
163
+ # @raise [Warrant::InternalError]
164
+ # @raise [Warrant::InvalidParameterError]
165
+ # @raise [Warrant::NotFoundError]
166
+ # @raise [Warrant::UnauthorizedError]
167
+ # @raise [Warrant::WarrantError]
168
+ def self.remove_from_tenant(tenant_id, feature_id)
169
+ res = APIOperations.delete(URI.parse("#{::Warrant.config.api_base}/v1/tenants/#{tenant_id}/features/#{feature_id}"))
170
+
171
+ case res
172
+ when Net::HTTPSuccess
173
+ return
174
+ else
175
+ APIOperations.raise_error(res)
176
+ end
177
+ end
178
+
179
+ # List features for user
180
+ #
181
+ # @param user_id [String] The user_id of the user to list features for.
182
+ # @option filters [Integer] :page A positive integer (starting with 1) representing the page of items to return in response. Used in conjunction with the limit param. (optional)
183
+ # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
184
+ #
185
+ # @return [Array<Feature>] assigned features for the user
186
+ #
187
+ # @raise [Warrant::InternalError]
188
+ # @raise [Warrant::InvalidParameterError]
189
+ # @raise [Warrant::UnauthorizedError]
190
+ def self.list_for_user(user_id, filters = {})
191
+ res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/features"), Util.normalize_params(filters))
192
+
193
+ case res
194
+ when Net::HTTPSuccess
195
+ features = JSON.parse(res.body)
196
+ features.map{ |feature| Feature.new(feature['featureId']) }
197
+ else
198
+ APIOperations.raise_error(res)
199
+ end
200
+ end
201
+
202
+ # Assign a feature to a user
203
+ #
204
+ # @param user_id [String] The user_id of the user you want to assign a feature to.
205
+ # @param feature_id [String] The feature_id of the feature you want to assign to a user.
206
+ #
207
+ # @return [Feature] assigned feature
208
+ #
209
+ # @raise [Warrant::DuplicateRecordError]
210
+ # @raise [Warrant::InternalError]
211
+ # @raise [Warrant::InvalidParameterError]
212
+ # @raise [Warrant::NotFoundError]
213
+ # @raise [Warrant::UnauthorizedError]
214
+ def self.assign_to_user(user_id, feature_id)
215
+ res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/features/#{feature_id}"))
216
+
217
+ case res
218
+ when Net::HTTPSuccess
219
+ feature = JSON.parse(res.body)
220
+ Feature.new(feature['featureId'])
221
+ else
222
+ APIOperations.raise_error(res)
223
+ end
224
+ end
225
+
226
+ # Remove a feature from a user
227
+ #
228
+ # @param user_id [String] The user_id of the user you want to remove a feature from.
229
+ # @param feature_id [String] The feature_id of the feature you want to remove from a user.
230
+ #
231
+ # @return [nil] if remove was successful
232
+ #
233
+ # @raise [Warrant::InternalError]
234
+ # @raise [Warrant::InvalidParameterError]
235
+ # @raise [Warrant::NotFoundError]
236
+ # @raise [Warrant::UnauthorizedError]
237
+ # @raise [Warrant::WarrantError]
238
+ def self.remove_from_user(user_id, feature_id)
239
+ res = APIOperations.delete(URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/features/#{feature_id}"))
240
+
241
+ case res
242
+ when Net::HTTPSuccess
243
+ return
244
+ else
245
+ APIOperations.raise_error(res)
246
+ end
247
+ end
248
+
249
+ # List features for pricing tier
250
+ #
251
+ # @param pricing_tier_id [String] The pricing_tier_id of the pricing tier to list features for.
252
+ # @option filters [Integer] :page A positive integer (starting with 1) representing the page of items to return in response. Used in conjunction with the limit param. (optional)
253
+ # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
254
+ #
255
+ # @return [Array<Feature>] assigned features for the pricing tier
256
+ #
257
+ # @raise [Warrant::InternalError]
258
+ # @raise [Warrant::InvalidParameterError]
259
+ # @raise [Warrant::UnauthorizedError]
260
+ def self.list_for_pricing_tier(pricing_tier_id, filters = {})
261
+ res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/pricing-tiers/#{pricing_tier_id}/features"), Util.normalize_params(filters))
262
+
263
+ case res
264
+ when Net::HTTPSuccess
265
+ features = JSON.parse(res.body)
266
+ features.map{ |feature| Feature.new(feature['featureId']) }
267
+ else
268
+ APIOperations.raise_error(res)
269
+ end
270
+ end
271
+
272
+ # Assign a feature to a pricing tier
273
+ #
274
+ # @param pricing_tier_id [String] The pricing_tier_id of the pricing tier you want to assign a feature to.
275
+ # @param feature_id [String] The feature_id of the feature you want to assign to a pricing tier.
276
+ #
277
+ # @return [Feature] assigned pricing tier
278
+ #
279
+ # @raise [Warrant::DuplicateRecordError]
280
+ # @raise [Warrant::InternalError]
281
+ # @raise [Warrant::InvalidParameterError]
282
+ # @raise [Warrant::NotFoundError]
283
+ # @raise [Warrant::UnauthorizedError]
284
+ def self.assign_to_pricing_tier(pricing_tier_id, feature_id)
285
+ res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v1/pricing-tiers/#{pricing_tier_id}/features/#{feature_id}"))
286
+
287
+ case res
288
+ when Net::HTTPSuccess
289
+ feature = JSON.parse(res.body)
290
+ Feature.new(feature['featureId'])
291
+ else
292
+ APIOperations.raise_error(res)
293
+ end
294
+ end
295
+
296
+ # Remove a feature from a pricing tier
297
+ #
298
+ # @param pricing_tier_id [String] The pricing_tier_id of the pricing tier you want to remove a feature from.
299
+ # @param feature_id [String] The feature_id of the feature you want to remove from a pricing tier.
300
+ #
301
+ # @return [nil] if remove was successful
302
+ #
303
+ # @raise [Warrant::InternalError]
304
+ # @raise [Warrant::InvalidParameterError]
305
+ # @raise [Warrant::NotFoundError]
306
+ # @raise [Warrant::UnauthorizedError]
307
+ # @raise [Warrant::WarrantError]
308
+ def self.remove_from_pricing_tier(pricing_tier_id, feature_id)
309
+ res = APIOperations.delete(URI.parse("#{::Warrant.config.api_base}/v1/pricing-tiers/#{pricing_tier_id}/features/#{feature_id}"))
310
+
311
+ case res
312
+ when Net::HTTPSuccess
313
+ return
314
+ else
315
+ APIOperations.raise_error(res)
316
+ end
317
+ end
318
+
319
+ def warrant_object_type
320
+ "feature"
321
+ end
322
+
323
+ def warrant_object_id
324
+ feature_id
325
+ end
326
+ end
327
+ end