warrant 2.0.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: 1a31b7215d1f5c43c9324a9456bfeed330a0c6656e899f670be1a41a9a9ab0a0
4
- data.tar.gz: a8b43559d231b5a54ab06ff22fdd37ce9123f0bc1442523d23cecf936da56aea
3
+ metadata.gz: b2de57e7f6362b34f03718a734f921659060d75eb720068cb4b891dfd0b54040
4
+ data.tar.gz: d410a14ae055941fd09cfc8476d6faf9d228f39596e5b6414b58a38520cf50ac
5
5
  SHA512:
6
- metadata.gz: 7e931736321f72d8ed39331104a5a72a3ebc85680b21659edd6eefc0ade2cd93206bd32865ebb0df51020f3067fc93e6136866c407ebe14d52fd6758019cadd8
7
- data.tar.gz: 8a273c5d903a66e3e5fc8a4d7bba657f2e3c6b9188f0ebe8cc25b27e2dda361e139eb62a9651ab0650026ba1b400c2bd3615346cffdfeda1e8352ec3123dd9e7
6
+ metadata.gz: ebd8297df53e8f8e2baa67bd602587344ef8ad2ffcc1db03a8e247a3a4cf2e41f55dd90916dd93196398a21a39a726ec8d4871cb3cd7f3f3c14ba9e6dd27ca14
7
+ data.tar.gz: 3e6b64ac8e89d4282f235190d9873045d608ce2a1eea4f4adbf0bf95924fbe2046321340d05b73c65eeffbfcd5c9d702ebdfed238af56cceb11aaa943b463ec3
@@ -2,16 +2,16 @@
2
2
 
3
3
  module Warrant
4
4
  class Warrant
5
- attr_reader :id, :object_type, :object_id, :relation, :subject, :context, :is_direct_match
5
+ attr_reader :id, :object_type, :object_id, :relation, :subject, :context, :is_implicit
6
6
 
7
7
  # @!visibility private
8
- def initialize(object_type, object_id, relation, subject, context = nil, is_direct_match = nil)
8
+ def initialize(object_type, object_id, relation, subject, context = nil, is_implicit = nil)
9
9
  @object_type = object_type
10
10
  @object_id = object_id
11
11
  @relation = relation
12
12
  @subject = subject
13
13
  @context = context
14
- @is_direct_match = is_direct_match
14
+ @is_implicit = is_implicit
15
15
  end
16
16
 
17
17
  # Create a new warrant that associates an object (object_type and object_id) to a subject via a relation.
@@ -88,35 +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] :object_type The type of object. Must be one of your system's existing object types. (optional)
94
- # @option params [String] :relation The relation for this object to subject association. The relation must be valid as per the object type definition. (optional)
95
- # @option params [String] :subject The subject to query warrants for. This should be in the format `OBJECT_TYPE:OBJECT_ID`, i.e. `user:8`
96
- # * subject (Hash) - The specific subject for which warrants will be queried for.
97
- # * object_type (String) - The type of object. Must be one of your system's existing object types.
98
- # * object_id (String) - The id of the specific object.
99
- # @option params [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)
100
- # @option params [Integer] :limit A positive integer representing the max number of items to return in response. (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)
101
96
  #
102
- # @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.
103
98
  #
104
99
  # @raise [Warrant::InternalError]
105
100
  # @raise [Warrant::InvalidParameterError]
106
101
  # @raise [Warrant::MissingRequiredParameterError]
107
102
  # @raise [Warrant::UnauthorizedError]
108
103
  # @raise [Warrant::WarrantError]
109
- def self.query(params = {})
110
- params[:subject] = Subject.new_from_hash(params[:subject])
111
- 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 })
112
106
 
113
107
  case res
114
108
  when Net::HTTPSuccess
115
- warrants = JSON.parse(res.body)
116
- warrants.map{ |warrant|
109
+ query_result = JSON.parse(res.body)
110
+ query_result['result'] = query_result['result'].map{ |warrant|
117
111
  subject = Subject.new(warrant['subject']['objectType'], warrant['subject']['objectId'], warrant['subject']['relation'])
118
- Warrant.new(warrant['objectType'], warrant['objectId'], warrant['relation'], subject, warrant['context'], warrant['isDirectMatch'])
112
+ Warrant.new(warrant['objectType'], warrant['objectId'], warrant['relation'], subject, warrant['context'], warrant['isImplicit'])
119
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
120
152
  else
121
153
  APIOperations.raise_error(res)
122
154
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Warrant
4
- VERSION = "2.0.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.0.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: 2022-12-22 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