warrant 2.1.0 → 2.2.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: 78f048c8c4f4611aafd280b84cce9cd09d8d664b6892030ec564bf34b80215ea
4
- data.tar.gz: bba96bc3e9c504487af9b5ebad98b673b3592eb9b72fecba81ebf33e52ae54de
3
+ metadata.gz: b2de57e7f6362b34f03718a734f921659060d75eb720068cb4b891dfd0b54040
4
+ data.tar.gz: d410a14ae055941fd09cfc8476d6faf9d228f39596e5b6414b58a38520cf50ac
5
5
  SHA512:
6
- metadata.gz: 0e4a8f1e22266df4957bbd40f45c0d42a336a13091d33cccb40482d632f98b494f05218de73c61a5bb85216116595751f5fc331f626afc3b6d7ea5ae017cf581
7
- data.tar.gz: b4f41491a47957d3bfb888231e2b110d70d0fad7e24ff82a4f64a51460dd4a78fd8ab8a86d26b20433ff8ff403ca3f5b9051d976f1b0adb6869e98f92a08cecc
6
+ metadata.gz: ebd8297df53e8f8e2baa67bd602587344ef8ad2ffcc1db03a8e247a3a4cf2e41f55dd90916dd93196398a21a39a726ec8d4871cb3cd7f3f3c14ba9e6dd27ca14
7
+ data.tar.gz: 3e6b64ac8e89d4282f235190d9873045d608ce2a1eea4f4adbf0bf95924fbe2046321340d05b73c65eeffbfcd5c9d702ebdfed238af56cceb11aaa943b463ec3
@@ -88,29 +88,67 @@ module Warrant
88
88
  end
89
89
  end
90
90
 
91
- # Query to find all warrants for a given subject.
91
+ # Query to find all warrants for a given object or subject.
92
92
  #
93
- # @option params [String] :select Specifies the type of results to be returned by the query. Optionally, the `explicit` keyword can be provided (i.e. `explicit warrants`) to specify that only explicit results be returned. By default, both implicit and explicit results are returned.
94
- # @option params [String] :for A list of conditions specifying which resources to query results for (i.e. "get all warrants **for role:admin**"). Only those warrants matching **all** of the conditions in the `for` clause are selected. If the `explicit` keyword is not specified in the `select` param, the resulting warrants are then expanded to determine if they imply other warrants (i.e. "the owner of a report is also an editor of that report"). Must be zero or more comma separated values in the format `object|relation|subject|context = val`. For object and subject filters, you can filter on all object ids by using the `*` character (i.e. `role:*`). (optional)
95
- # @option params [String] :where A list of conditions to be applied to the result set before it is returned. If a where clause is provided, the query will only return results matching **all** conditions. Must be zero or more comma separated values in the format `object|relation|subject|context = val``. For object and subject filters, you can filter on all object ids by using the `*` character, i.e. `role:*`. (optional)
93
+ # @param warrant_query [WarrantQuery] Query to run for a set of warrants.
94
+ # @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)
95
+ # @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
96
96
  #
97
- # @return [Array<Warrant>] list of all warrants with provided params
97
+ # @return [Hash] Query result with `result` listing warrants returned and `meta` with metadata for the selected object types.
98
98
  #
99
99
  # @raise [Warrant::InternalError]
100
100
  # @raise [Warrant::InvalidParameterError]
101
101
  # @raise [Warrant::MissingRequiredParameterError]
102
102
  # @raise [Warrant::UnauthorizedError]
103
103
  # @raise [Warrant::WarrantError]
104
- def self.query(params = {})
105
- res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/query"), params)
104
+ def self.query(warrant_query = WarrantQuery.new, filters = {})
105
+ res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v1/query"), { "q": warrant_query.to_query_param, **filters })
106
106
 
107
107
  case res
108
108
  when Net::HTTPSuccess
109
- warrants = JSON.parse(res.body)
110
- warrants.map{ |warrant|
109
+ query_result = JSON.parse(res.body)
110
+ query_result['result'] = query_result['result'].map{ |warrant|
111
111
  subject = Subject.new(warrant['subject']['objectType'], warrant['subject']['objectId'], warrant['subject']['relation'])
112
112
  Warrant.new(warrant['objectType'], warrant['objectId'], warrant['relation'], subject, warrant['context'], warrant['isImplicit'])
113
113
  }
114
+
115
+ if query_result['meta']['feature']
116
+ query_result['meta']['feature'].each{ |featureId, feature|
117
+ query_result['meta']['feature'][featureId] = Feature.new(feature['featureId'])
118
+ }
119
+ end
120
+
121
+ if query_result['meta']['pricing-tier']
122
+ query_result['meta']['pricing-tier'].each{ |pricingTierId, pricingTier|
123
+ query_result['meta']['pricing-tier'][pricingTierId] = PricingTier.new(pricingTier['pricingTierId'])
124
+ }
125
+ end
126
+
127
+ if query_result['meta']['permission']
128
+ query_result['meta']['permission'].each{ |permissionId, permission|
129
+ query_result['meta']['permission'][permissionId] = Permission.new(permission['permissionId'], permission['name'], permission['description'])
130
+ }
131
+ end
132
+
133
+ if query_result['meta']['role']
134
+ query_result['meta']['role'].each{ |roleId, role|
135
+ query_result['meta']['role'][roleId] = Role.new(role['roleId'], role['name'], role['description'])
136
+ }
137
+ end
138
+
139
+ if query_result['meta']['user']
140
+ query_result['meta']['user'].each{ |userId, user|
141
+ query_result['meta']['user'][userId] = User.new(user['userId'], user['email'], user['createdAt'])
142
+ }
143
+ end
144
+
145
+ if query_result['meta']['tenant']
146
+ query_result['meta']['tenant'].each{ |tenantId, tenant|
147
+ query_result['meta']['tenant'][tenantId] = Tenant.new(tenant['tenantId'], tenant['name'], tenant['createdAt'])
148
+ }
149
+ end
150
+
151
+ query_result
114
152
  else
115
153
  APIOperations.raise_error(res)
116
154
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Warrant
4
- VERSION = "2.1.0"
4
+ VERSION = "2.2.0"
5
5
  end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Warrant
4
+ class WarrantQuery
5
+ attr_accessor :select_clause, :for_clause, :where_clause
6
+
7
+ def initialize
8
+ @select_clause = []
9
+ @for_clause = {}
10
+ @where_clause = {}
11
+ end
12
+
13
+ def select(*object_types)
14
+ @select_clause = object_types
15
+ self
16
+ end
17
+
18
+ def select_explicit(*object_types)
19
+ @select_clause = "explicit #{object_types}"
20
+ self
21
+ end
22
+
23
+ def for(for_filters)
24
+ @for_clause = @for_clause.merge(for_filters)
25
+ self
26
+ end
27
+
28
+ def where(where_filters)
29
+ @where_clause = @where_clause.merge(where_filters)
30
+ self
31
+ end
32
+
33
+ def to_query_param
34
+ if @select_clause.length == 0 || @for_clause.empty?
35
+ raise "Must have a select and for clause"
36
+ end
37
+
38
+ query = "SELECT #{@select_clause.join(",")} FOR #{filters_hash_to_string(@for_clause)}"
39
+ query += " WHERE #{filters_hash_to_string(@where_clause)}" unless @where_clause.empty?
40
+
41
+ query
42
+ end
43
+
44
+ private
45
+
46
+ def filters_hash_to_string(filters)
47
+ filter_string = ""
48
+
49
+ if filters[:object]
50
+ filter_string += "object=#{filters[:object]}"
51
+ elsif filters[:subject]
52
+ filter_string += "subject=#{filters[:subject]}"
53
+ end
54
+
55
+ if filters[:context]
56
+ context_values = []
57
+ filters[:context].each{ |k, v|
58
+ context_values.push("#{k}=#{v}")
59
+ }
60
+
61
+ filter_string += " AND context=[#{context_values.join(" ")}]"
62
+ end
63
+
64
+ filter_string
65
+ end
66
+ end
67
+ end
data/lib/warrant.rb CHANGED
@@ -7,6 +7,7 @@ require "json"
7
7
  require "forwardable"
8
8
 
9
9
  require "warrant/warrant_object"
10
+ require "warrant/warrant_query"
10
11
 
11
12
  require "warrant/api_operations"
12
13
  require "warrant/errors"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warrant
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Warrant
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-06 00:00:00.000000000 Z
11
+ date: 2023-01-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby library for the Warrant API at https://warrant.dev.
14
14
  email: hello@warrant.dev
@@ -41,6 +41,7 @@ files:
41
41
  - lib/warrant/version.rb
42
42
  - lib/warrant/warrant_configuration.rb
43
43
  - lib/warrant/warrant_object.rb
44
+ - lib/warrant/warrant_query.rb
44
45
  homepage: https://github.com/warrant-dev/warrant-ruby
45
46
  licenses:
46
47
  - MIT