warrant 3.1.0 → 5.0.0

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: eedc398b4f1277eabdabf8252acde068e1a1398f0b3752d217793ec1d22570b3
4
- data.tar.gz: 035f98bda116c4177ead75b9895a86582b2b37fd7d04b1b2175e68f73c0adff5
3
+ metadata.gz: e2f6af0b73e1d0ade950db2c0bce0e14176bb3f380914099ea5bb21382076ba0
4
+ data.tar.gz: 973470f3ac7c430e07258004d9e425d39ce338c7130ce9a23e694c49f6411286
5
5
  SHA512:
6
- metadata.gz: b5d81834bffb0ac19ed371259e6b321ca23aa42a8ab0dd1bcf8c720fec168910ed8dcfe620942d17d4d399834593856080dbe802f786d273466db347089b10b4
7
- data.tar.gz: 717cce9e98093c01c7412323894e7eb9f87742ae412eac1564e8e91184a3cea327dbb76ee7d5131196389c8df7e6b5a112ed24a6ccbd270f75a0277f2356b651
6
+ metadata.gz: 6d6a7b4e2b0a7a69a31ce8f04b6bb856edf7d4793795a237f3ba23a46cb3f9aab09caab3ab0511c005f7c018384d9c325987194e61956dfda0588274b7b74ce1
7
+ data.tar.gz: c488dc7bdc1d19775e793efe3eff35c785c870409e70c76c997f15473de93bb8b4f42f9203e4d807e0c5c7b90f641da286b2f4d067335664c441225522dc217a
data/README.md CHANGED
@@ -6,7 +6,6 @@ Use [Warrant](https://warrant.dev/) in ruby projects.
6
6
  [![Slack](https://img.shields.io/badge/slack-join-brightgreen)](https://join.slack.com/t/warrantcommunity/shared_invite/zt-12g84updv-5l1pktJf2bI5WIKN4_~f4w)
7
7
 
8
8
  ## Installation
9
- ---
10
9
 
11
10
  Add this line to your application's Gemfile:
12
11
 
@@ -27,18 +26,15 @@ You can also build the gem from source:
27
26
  $ gem build warrant.gemspec
28
27
 
29
28
  ## Documentation
30
- ---
31
29
 
32
30
  - [Ruby API Docs](https://rubydoc.info/gems/warrant)
33
31
  - [Warrant Docs](https://docs.warrant.dev/)
34
32
 
35
33
  ## Requirements
36
- ---
37
34
 
38
35
  - Ruby 2.3+.
39
36
 
40
37
  ## Usage
41
- ---
42
38
 
43
39
  ```ruby
44
40
  require 'warrant'
@@ -52,7 +48,6 @@ Warrant::Warrant.is_authorized?(object_type: "report", object_id: "7asm24", rela
52
48
  ```
53
49
 
54
50
  ## Configuring the API and Authorize Endpoints
55
- ---
56
51
  The API and Authorize endpoints the SDK makes requests to is configurable via the `Warrant.api_base` and `Warrant.authorize_endpoint` attributes:
57
52
 
58
53
  ```ruby
@@ -64,7 +59,6 @@ Warrant.authorize_endpoint = 'http://localhost:8000'
64
59
  ```
65
60
 
66
61
  ## Configuring SSL
67
- ---
68
62
  By default, the SDK will attempt to use SSL when making requests to the API. This setting is configurable via the `Warrant.use_ssl` attribute:
69
63
 
70
64
  ```ruby
@@ -74,7 +68,6 @@ require 'warrant'
74
68
  Warrant.use_ssl = false
75
69
  ```
76
70
 
77
-
78
71
  We’ve used a random API key in these code examples. Replace it with your [actual publishable API keys](https://app.warrant.dev) to
79
72
  test this code through your own Warrant account.
80
73
 
@@ -4,49 +4,51 @@ module Warrant
4
4
  # @!visibility private
5
5
  class APIOperations
6
6
  class << self
7
- def post(uri, params = {})
7
+ def post(uri, params: {}, options: {})
8
8
  http = Net::HTTP.new(uri.host, uri.port)
9
9
  http.use_ssl = ::Warrant.config.use_ssl
10
10
  headers = {
11
11
  "User-Agent": "warrant-ruby/#{VERSION}"
12
12
  }
13
13
  headers["Authorization"] = "ApiKey #{::Warrant.config.api_key}" unless ::Warrant.config.api_key.empty?
14
+ headers["Warrant-Token"] = options[:warrant_token] if options.has_key?(:warrant_token)
14
15
  http.post(uri.path, params.to_json, headers)
15
16
  end
16
17
 
17
- def delete(uri, params = {})
18
+ def delete(uri, params: {}, options: {})
18
19
  http = Net::HTTP.new(uri.host, uri.port)
19
20
  http.use_ssl = ::Warrant.config.use_ssl
20
21
  request = Net::HTTP::Delete.new(uri.path)
21
22
  request["Authorization"] = "ApiKey #{::Warrant.config.api_key}" unless ::Warrant.config.api_key.empty?
22
23
  request["User-Agent"] = "warrant-ruby/#{VERSION}"
23
-
24
+ headers["Warrant-Token"] = options[:warrant_token] if options.has_key?(:warrant_token)
24
25
  http.request(request, params.to_json)
25
26
  end
26
27
 
27
- def get(uri, params = {})
28
+ def get(uri, params: {}, options: {})
28
29
  http = Net::HTTP.new(uri.host, uri.port)
29
30
  http.use_ssl = ::Warrant.config.use_ssl
30
31
  headers = {
31
32
  "User-Agent": "warrant-ruby/#{VERSION}"
32
33
  }
33
34
  headers["Authorization"] = "ApiKey #{::Warrant.config.api_key}" unless ::Warrant.config.api_key.empty?
35
+ headers["Warrant-Token"] = options[:warrant_token] if options.has_key?(:warrant_token)
34
36
 
35
37
  unless params.empty?
36
- normalized_params = Util.normalize_params(params.compact)
37
- uri.query = URI.encode_www_form(normalized_params)
38
+ uri.query = URI.encode_www_form(params)
38
39
  end
39
40
 
40
41
  http.get(uri, headers)
41
42
  end
42
43
 
43
- def put(uri, params = {})
44
+ def put(uri, params: {}, options: {})
44
45
  http = Net::HTTP.new(uri.host, uri.port)
45
46
  http.use_ssl = ::Warrant.config.use_ssl
46
47
  headers = {
47
48
  "User-Agent": "warrant-ruby/#{VERSION}"
48
49
  }
49
50
  headers["Authorization"] = "ApiKey #{::Warrant.config.api_key}" unless ::Warrant.config.api_key.empty?
51
+ headers["Warrant-Token"] = options[:warrant_token] if options.has_key?(:warrant_token)
50
52
  http.put(uri.path, params.to_json, headers)
51
53
  end
52
54
 
@@ -1,21 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Warrant
4
- class Feature
4
+ class Feature < Warrant::Object
5
5
  OBJECT_TYPE = "feature"
6
6
 
7
7
  include Warrant::WarrantObject
8
8
 
9
- attr_reader :feature_id
9
+ alias :feature_id :object_id
10
10
 
11
11
  # @!visibility private
12
- def initialize(feature_id)
13
- @feature_id = feature_id
12
+ def initialize(feature_id, meta = {}, created_at = nil)
13
+ super(OBJECT_TYPE, feature_id, meta, created_at)
14
14
  end
15
15
 
16
16
  # Creates a feature with the given parameters
17
17
  #
18
- # @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 '_'.
18
+ # @option params [String] :feature_id User defined string identifier for this feature. If not provided, Warrant will create an id for the feature and return it. In this case, you should store the id in your system as you will need to provide it for any authorization requests for that feature. Note that featureIds in Warrant must be composed of alphanumeric chars, '-', and/or '_'. (optional)
19
+ # @option params [Hash] :meta A JSON object containing additional information about this feature (e.g. name/description) to be persisted to Warrant. (optional)
19
20
  #
20
21
  # @return [Feature] created feature
21
22
  #
@@ -26,16 +27,9 @@ module Warrant
26
27
  # @raise [Warrant::InternalError]
27
28
  # @raise [Warrant::InvalidRequestError]
28
29
  # @raise [Warrant::UnauthorizedError]
29
- def self.create(params = {})
30
- res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v1/features"), Util.normalize_params(params))
31
-
32
- case res
33
- when Net::HTTPSuccess
34
- res_json = JSON.parse(res.body)
35
- Feature.new(res_json['featureId'])
36
- else
37
- APIOperations.raise_error(res)
38
- end
30
+ def self.create(params = {}, options = {})
31
+ object = Object.create({ object_type: OBJECT_TYPE, object_id: params[:feature_id], meta: params[:meta] }, options)
32
+ return Feature.new(object.object_id, object.meta, object.created_at)
39
33
  end
40
34
 
41
35
  # Deletes a feature with given feature id
@@ -51,21 +45,20 @@ module Warrant
51
45
  # @raise [Warrant::InvalidParameterError]
52
46
  # @raise [Warrant::NotFoundError]
53
47
  # @raise [Warrant::UnauthorizedError]
54
- def self.delete(feature_id)
55
- res = APIOperations.delete(URI.parse("#{::Warrant.config.api_base}/v1/features/#{feature_id}"))
56
-
57
- case res
58
- when Net::HTTPSuccess
59
- return
60
- else
61
- APIOperations.raise_error(res)
62
- end
48
+ def self.delete(feature_id, options = {})
49
+ return Object.delete(OBJECT_TYPE, feature_id, options)
63
50
  end
64
51
 
65
52
  # Lists all features for your organization
66
53
  #
67
- # @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)
68
- # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
54
+ # @param [Hash] filters Filters to apply to result set
55
+ # @param [Hash] options Options to apply on a per-request basis
56
+ # @option filters [Integer] :limit A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)
57
+ # @option filters [String] :prev_cursor A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)
58
+ # @option filters [String] :next_cursor A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)
59
+ # @option filters [String] :sort_by The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are +object_type+, +object_id+, and +created_at+ (optional)
60
+ # @option filters [String] :sort_order The order in which to sort the result by. Valid values are +ASC+ and +DESC+. Defaults to +ASC+. (optional)
61
+ # @option options [String] :warrant_token A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)
69
62
  #
70
63
  # @return [Array<Feature>] all features for your organization
71
64
  #
@@ -74,16 +67,11 @@ module Warrant
74
67
  #
75
68
  # @raise [Warrant::InternalError]
76
69
  # @raise [Warrant::UnauthorizedError]
77
- def self.list(filters = {})
78
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/features"), Util.normalize_params(filters))
79
-
80
- case res
81
- when Net::HTTPSuccess
82
- features = JSON.parse(res.body)
83
- features.map{ |feature| Feature.new(feature['featureId']) }
84
- else
85
- APIOperations.raise_error(res)
86
- end
70
+ def self.list(filters = {}, options = {})
71
+ filters.merge({ object_type: OBJECT_TYPE })
72
+ list_response = Object.list(filters, options)
73
+ features = list_response.results.map{ |object| Feature.new(object.object_id, object.meta, object.created_at)}
74
+ return ListResponse.new(features, list_response.prev_cursor, list_response.next_cursor)
87
75
  end
88
76
 
89
77
  # Get a feature with the given feature_id
@@ -96,45 +84,79 @@ module Warrant
96
84
  # @raise [Warrant::InvalidParameterError]
97
85
  # @raise [Warrant::NotFoundError]
98
86
  # @raise [Warrant::UnauthorizedError]
99
- def self.get(feature_id)
100
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/features/#{feature_id}"))
87
+ def self.get(feature_id, options = {})
88
+ object = Object.get(OBJECT_TYPE, feature_id, options)
89
+ return Feature.new(object.object_id, object.meta, object.created_at)
90
+ end
101
91
 
102
- case res
103
- when Net::HTTPSuccess
104
- feature = JSON.parse(res.body)
105
- Feature.new(feature['featureId'])
106
- else
107
- APIOperations.raise_error(res)
108
- end
92
+ # Updates a feature with the given feature_id and params
93
+ #
94
+ # @param feature_id [String] The feature_id of the feature to be updated.
95
+ # @param meta [Hash] A JSON object containing additional information about this feature (e.g. name/description, etc.) to be persisted to Warrant.
96
+ #
97
+ # @return [Feature] updated feature
98
+ #
99
+ # @example Update feature "test-feature"'s name
100
+ # Warrant::Feature.update("test-feature", { name: "Test Feature" })
101
+ #
102
+ # @raise [Warrant::InternalError]
103
+ # @raise [Warrant::InvalidParameterError]
104
+ # @raise [Warrant::InvalidRequestError]
105
+ # @raise [Warrant::NotFoundError]
106
+ # @raise [Warrant::UnauthorizedError]
107
+ def self.update(feature_id, meta, options = {})
108
+ object = Object.update(OBJECT_TYPE, feature_id, meta, options)
109
+ return Feature.new(object.object_id, object.meta, object.created_at)
110
+ end
111
+
112
+ # Updates a feature with the given params
113
+ #
114
+ # @param meta [Hash] A JSON object containing additional information about this feature (e.g. name/description, etc.) to be persisted to Warrant.
115
+ #
116
+ # @return [Feature] updated feature
117
+ #
118
+ # @example Update feature "test-feature"'s name
119
+ # feature = Warrant::Feature.get("test-feature")
120
+ # feature.update({ name: "Test Feature" })
121
+ #
122
+ # @raise [Warrant::InternalError]
123
+ # @raise [Warrant::InvalidParameterError]
124
+ # @raise [Warrant::InvalidRequestError]
125
+ # @raise [Warrant::NotFoundError]
126
+ # @raise [Warrant::UnauthorizedError]
127
+ def update(meta, options = {})
128
+ return Feature.update(feature_id, meta)
109
129
  end
110
130
 
111
131
  # List features for tenant
112
132
  #
113
133
  # @param tenant_id [String] The tenant_id of the tenant to list features for.
114
- # @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)
115
- # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
134
+ # @param [Hash] filters Filters to apply to result set
135
+ # @param [Hash] options Options to apply on a per-request basis
136
+ # @option filters [String] :object_type Only return objects with an `objectType` matching this value
137
+ # @option filters [Integer] :limit A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)
138
+ # @option filters [String] :prev_cursor A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)
139
+ # @option filters [String] :next_cursor A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)
140
+ # @option filters [String] :sort_by The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are +object_type+, +object_id+, and +created_at+ (optional)
141
+ # @option filters [String] :sort_order The order in which to sort the result by. Valid values are +ASC+ and +DESC+. Defaults to +ASC+. (optional)
142
+ # @option options [String] :warrant_token A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)
116
143
  #
117
144
  # @return [Array<Feature>] assigned features for the tenant
118
145
  #
119
146
  # @raise [Warrant::InternalError]
120
147
  # @raise [Warrant::InvalidParameterError]
121
148
  # @raise [Warrant::UnauthorizedError]
122
- def self.list_for_tenant(tenant_id, filters = {})
123
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/tenants/#{tenant_id}/features"), Util.normalize_params(filters))
124
-
125
- case res
126
- when Net::HTTPSuccess
127
- features = JSON.parse(res.body)
128
- features.map{ |feature| Feature.new(feature['featureId']) }
129
- else
130
- APIOperations.raise_error(res)
131
- end
149
+ def self.list_for_tenant(tenant_id, filters = {}, options = {})
150
+ query_response = Warrant.query("select feature where tenant:#{tenant_id} is *", filters: filters, options: options)
151
+ features = query_response.results.map{ |result| Feature.new(result.object_id, result.meta) }
152
+ return ListResponse.new(features, query_response.prev_cursor, query_response.next_cursor)
132
153
  end
133
154
 
134
155
  # Assign a feature to a tenant
135
156
  #
136
157
  # @param tenant_id [String] The tenant_id of the tenant you want to assign a feature to.
137
158
  # @param feature_id [String] The feature_id of the feature you want to assign to a tenant.
159
+ # @param relation [String] The relation for this feature to tenant association. The relation must be valid as per the +feature+ object type definition.
138
160
  #
139
161
  # @return [Warrant] warrant assigning feature to tenant
140
162
  #
@@ -143,14 +165,15 @@ module Warrant
143
165
  # @raise [Warrant::InvalidParameterError]
144
166
  # @raise [Warrant::NotFoundError]
145
167
  # @raise [Warrant::UnauthorizedError]
146
- def self.assign_to_tenant(tenant_id, feature_id)
147
- Warrant.create({ object_type: Feature::OBJECT_TYPE, object_id: feature_id }, "member", { object_type: Tenant::OBJECT_TYPE, object_id: tenant_id })
168
+ def self.assign_to_tenant(tenant_id, feature_id, relation: "member", options: {})
169
+ Warrant.create({ object_type: Feature::OBJECT_TYPE, object_id: feature_id }, relation, { object_type: Tenant::OBJECT_TYPE, object_id: tenant_id }, nil, options)
148
170
  end
149
171
 
150
172
  # Remove a feature from a tenant
151
173
  #
152
174
  # @param tenant_id [String] The tenant_id of the tenant you want to remove a feature from.
153
175
  # @param feature_id [String] The feature_id of the feature you want to remove from a tenant.
176
+ # @param relation [String] The relation for this feature to tenant association. The relation must be valid as per the +feature+ object type definition.
154
177
  #
155
178
  # @return [nil] if remove was successful
156
179
  #
@@ -159,37 +182,39 @@ module Warrant
159
182
  # @raise [Warrant::NotFoundError]
160
183
  # @raise [Warrant::UnauthorizedError]
161
184
  # @raise [Warrant::WarrantError]
162
- def self.remove_from_tenant(tenant_id, feature_id)
163
- Warrant.delete({ object_type: Feature::OBJECT_TYPE, object_id: feature_id }, "member", { object_type: Tenant::OBJECT_TYPE, object_id: tenant_id })
185
+ def self.remove_from_tenant(tenant_id, feature_id, relation: "member", options: {})
186
+ Warrant.delete({ object_type: Feature::OBJECT_TYPE, object_id: feature_id }, relation, { object_type: Tenant::OBJECT_TYPE, object_id: tenant_id }, nil, options)
164
187
  end
165
188
 
166
189
  # List features for user
167
190
  #
168
191
  # @param user_id [String] The user_id of the user to list features for.
169
- # @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)
170
- # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
192
+ # @param [Hash] filters Filters to apply to result set
193
+ # @param [Hash] options Options to apply on a per-request basis
194
+ # @option filters [String] :object_type Only return objects with an `objectType` matching this value
195
+ # @option filters [Integer] :limit A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)
196
+ # @option filters [String] :prev_cursor A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)
197
+ # @option filters [String] :next_cursor A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)
198
+ # @option filters [String] :sort_by The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are +object_type+, +object_id+, and +created_at+ (optional)
199
+ # @option filters [String] :sort_order The order in which to sort the result by. Valid values are +ASC+ and +DESC+. Defaults to +ASC+. (optional)
200
+ # @option options [String] :warrant_token A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)
171
201
  #
172
202
  # @return [Array<Feature>] assigned features for the user
173
203
  #
174
204
  # @raise [Warrant::InternalError]
175
205
  # @raise [Warrant::InvalidParameterError]
176
206
  # @raise [Warrant::UnauthorizedError]
177
- def self.list_for_user(user_id, filters = {})
178
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/features"), Util.normalize_params(filters))
179
-
180
- case res
181
- when Net::HTTPSuccess
182
- features = JSON.parse(res.body)
183
- features.map{ |feature| Feature.new(feature['featureId']) }
184
- else
185
- APIOperations.raise_error(res)
186
- end
207
+ def self.list_for_user(user_id, filters = {}, options = {})
208
+ query_response = Warrant.query("select feature where user:#{user_id} is *", filters: filters, options: options)
209
+ features = query_response.results.map{ |result| Feature.new(result.object_id, result.meta) }
210
+ return ListResponse.new(features, query_response.prev_cursor, query_response.next_cursor)
187
211
  end
188
212
 
189
213
  # Assign a feature to a user
190
214
  #
191
215
  # @param user_id [String] The user_id of the user you want to assign a feature to.
192
216
  # @param feature_id [String] The feature_id of the feature you want to assign to a user.
217
+ # @param relation [String] The relation for this feature to user association. The relation must be valid as per the +feature+ object type definition.
193
218
  #
194
219
  # @return [Warrant] warrant assigning feature to user
195
220
  #
@@ -198,14 +223,15 @@ module Warrant
198
223
  # @raise [Warrant::InvalidParameterError]
199
224
  # @raise [Warrant::NotFoundError]
200
225
  # @raise [Warrant::UnauthorizedError]
201
- def self.assign_to_user(user_id, feature_id)
202
- Warrant.create({ object_type: Feature::OBJECT_TYPE, object_id: feature_id }, "member", { object_type: User::OBJECT_TYPE, object_id: user_id })
226
+ def self.assign_to_user(user_id, feature_id, relation: "member", options: {})
227
+ Warrant.create({ object_type: Feature::OBJECT_TYPE, object_id: feature_id }, relation, { object_type: User::OBJECT_TYPE, object_id: user_id }, nil, options)
203
228
  end
204
229
 
205
230
  # Remove a feature from a user
206
231
  #
207
232
  # @param user_id [String] The user_id of the user you want to remove a feature from.
208
233
  # @param feature_id [String] The feature_id of the feature you want to remove from a user.
234
+ # @param relation [String] The relation for this feature to user association. The relation must be valid as per the +feature+ object type definition.
209
235
  #
210
236
  # @return [nil] if remove was successful
211
237
  #
@@ -214,37 +240,39 @@ module Warrant
214
240
  # @raise [Warrant::NotFoundError]
215
241
  # @raise [Warrant::UnauthorizedError]
216
242
  # @raise [Warrant::WarrantError]
217
- def self.remove_from_user(user_id, feature_id)
218
- Warrant.delete({ object_type: Feature::OBJECT_TYPE, object_id: feature_id }, "member", { object_type: User::OBJECT_TYPE, object_id: user_id })
243
+ def self.remove_from_user(user_id, feature_id, relation: "member", options: {})
244
+ Warrant.delete({ object_type: Feature::OBJECT_TYPE, object_id: feature_id }, relation, { object_type: User::OBJECT_TYPE, object_id: user_id }, nil, options)
219
245
  end
220
246
 
221
247
  # List features for pricing tier
222
248
  #
223
249
  # @param pricing_tier_id [String] The pricing_tier_id of the pricing tier to list features for.
224
- # @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)
225
- # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
250
+ # @param [Hash] filters Filters to apply to result set
251
+ # @param [Hash] options Options to apply on a per-request basis
252
+ # @option filters [String] :object_type Only return objects with an `objectType` matching this value
253
+ # @option filters [Integer] :limit A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)
254
+ # @option filters [String] :prev_cursor A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)
255
+ # @option filters [String] :next_cursor A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)
256
+ # @option filters [String] :sort_by The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are +object_type+, +object_id+, and +created_at+ (optional)
257
+ # @option filters [String] :sort_order The order in which to sort the result by. Valid values are +ASC+ and +DESC+. Defaults to +ASC+. (optional)
258
+ # @option options [String] :warrant_token A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)
226
259
  #
227
260
  # @return [Array<Feature>] assigned features for the pricing tier
228
261
  #
229
262
  # @raise [Warrant::InternalError]
230
263
  # @raise [Warrant::InvalidParameterError]
231
264
  # @raise [Warrant::UnauthorizedError]
232
- def self.list_for_pricing_tier(pricing_tier_id, filters = {})
233
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/pricing-tiers/#{pricing_tier_id}/features"), Util.normalize_params(filters))
234
-
235
- case res
236
- when Net::HTTPSuccess
237
- features = JSON.parse(res.body)
238
- features.map{ |feature| Feature.new(feature['featureId']) }
239
- else
240
- APIOperations.raise_error(res)
241
- end
265
+ def self.list_for_pricing_tier(pricing_tier_id, filters = {}, options = {})
266
+ query_response = Warrant.query("select feature where pricing-tier:#{pricing_tier_id} is *", filters: filters, options: options)
267
+ features = query_response.results.map{ |result| Feature.new(result.object_id, result.meta) }
268
+ return ListResponse.new(features, query_response.prev_cursor, query_response.next_cursor)
242
269
  end
243
270
 
244
271
  # Assign a feature to a pricing tier
245
272
  #
246
273
  # @param pricing_tier_id [String] The pricing_tier_id of the pricing tier you want to assign a feature to.
247
274
  # @param feature_id [String] The feature_id of the feature you want to assign to a pricing tier.
275
+ # @param relation [String] The relation for this feature to pricing tier association. The relation must be valid as per the +feature+ object type definition.
248
276
  #
249
277
  # @return [Warrant] warrant assigning feature to pricing tier
250
278
  #
@@ -253,14 +281,15 @@ module Warrant
253
281
  # @raise [Warrant::InvalidParameterError]
254
282
  # @raise [Warrant::NotFoundError]
255
283
  # @raise [Warrant::UnauthorizedError]
256
- def self.assign_to_pricing_tier(pricing_tier_id, feature_id)
257
- Warrant.create({ object_type: Feature::OBJECT_TYPE, object_id: feature_id }, "member", { object_type: PricingTier::OBJECT_TYPE, object_id: pricing_tier_id })
284
+ def self.assign_to_pricing_tier(pricing_tier_id, feature_id, relation: "member", options: {})
285
+ Warrant.create({ object_type: Feature::OBJECT_TYPE, object_id: feature_id }, relation, { object_type: PricingTier::OBJECT_TYPE, object_id: pricing_tier_id }, nil, options)
258
286
  end
259
287
 
260
288
  # Remove a feature from a pricing tier
261
289
  #
262
290
  # @param pricing_tier_id [String] The pricing_tier_id of the pricing tier you want to remove a feature from.
263
291
  # @param feature_id [String] The feature_id of the feature you want to remove from a pricing tier.
292
+ # @param relation [String] The relation for this feature to pricing tier association. The relation must be valid as per the +feature+ object type definition.
264
293
  #
265
294
  # @return [nil] if remove was successful
266
295
  #
@@ -269,8 +298,8 @@ module Warrant
269
298
  # @raise [Warrant::NotFoundError]
270
299
  # @raise [Warrant::UnauthorizedError]
271
300
  # @raise [Warrant::WarrantError]
272
- def self.remove_from_pricing_tier(pricing_tier_id, feature_id)
273
- Warrant.delete({ object_type: Feature::OBJECT_TYPE, object_id: feature_id }, "member", { object_type: PricingTier::OBJECT_TYPE, object_id: pricing_tier_id })
301
+ def self.remove_from_pricing_tier(pricing_tier_id, feature_id, relation: "member", options: {})
302
+ Warrant.delete({ object_type: Feature::OBJECT_TYPE, object_id: feature_id }, relation, { object_type: PricingTier::OBJECT_TYPE, object_id: pricing_tier_id }, nil, options)
274
303
  end
275
304
 
276
305
  def warrant_object_type
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Warrant
4
+ class ListResponse
5
+ attr_reader :results, :prev_cursor, :next_cursor
6
+
7
+ # @!visibility private
8
+ def initialize(results, prev_cursor, next_cursor)
9
+ @results = results
10
+ @prev_cursor = prev_cursor
11
+ @next_cursor = next_cursor
12
+ end
13
+ end
14
+ end