shotgun_api_ruby 0.0.4.1 → 0.0.5

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
  SHA256:
3
- metadata.gz: 2df57cc4c367e23b089c897b18dacf2232b3650d7acbfae545c248b6559be9a4
4
- data.tar.gz: 3429d8d5c2e12b84b9434d76a25420f134ecca300c6836dea001a45df2539547
3
+ metadata.gz: 6e8f35c879d4875bb2fd7aaf64717735ef915ca5dd4229097eb9a9d3eb8265b1
4
+ data.tar.gz: 0576b201159e2dcf3cfdcf7805e26e70f6796475153b0c7712c634aa4bb7fd48
5
5
  SHA512:
6
- metadata.gz: 6411ed8b2d37c6b8968ebc360eeb52c48331effeaa1d9c7e9c04826270e3ab553660ba72bb0f59fb91da8219a5b15d6bc614c42f117f1c3ce2cfa8e0d7c41497
7
- data.tar.gz: b8ab75e066f1314028c3a7d1191f83291bbe475050b1327bb99f6411abe7d0a2d1af908a649e70ae92a94fc7f95792909ea7c33352596f6c36c3e861b23b3126
6
+ metadata.gz: 193682e3bbd32fdac36a9bebd5bd6c48d2e2e1652e6d6f1149a2bd103ef8028bcaae91f85320c322468e37bdbfb8e8667134a32dbe4c31c98b62799fa6551b70
7
+ data.tar.gz: e782ea8785bb79dbed938e966bfb80fddafcc4b058f62a65342451be01b4587fff6f67461ab3648c54bbfb01196b0121cbf92fb3067b98ed1b85ab3dcc596d1f
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shotgun_api_ruby (0.0.4.1)
4
+ shotgun_api_ruby (0.0.5)
5
5
  activesupport
6
6
  faraday (~> 1)
7
7
  zeitwerk (~> 2.2)
@@ -51,7 +51,7 @@ GEM
51
51
  method_source (~> 1.0)
52
52
  rainbow (3.0.0)
53
53
  rake (13.0.1)
54
- regexp_parser (1.7.1)
54
+ regexp_parser (1.8.0)
55
55
  reverse_markdown (2.0.0)
56
56
  nokogiri
57
57
  rexml (3.2.4)
@@ -77,10 +77,11 @@ GEM
77
77
  rubocop-ast (>= 0.4.0, < 1.0)
78
78
  ruby-progressbar (~> 1.7)
79
79
  unicode-display_width (>= 1.4.0, < 2.0)
80
- rubocop-ast (0.4.1)
80
+ rubocop-ast (0.4.2)
81
81
  parser (>= 2.7.1.4)
82
- rubocop-performance (1.8.0)
82
+ rubocop-performance (1.8.1)
83
83
  rubocop (>= 0.87.0)
84
+ rubocop-ast (>= 0.4.0)
84
85
  ruby-progressbar (1.10.1)
85
86
  solargraph (0.39.15)
86
87
  backport (~> 1.1)
data/README.md CHANGED
@@ -166,7 +166,7 @@ Does the same thing as `all`
166
166
 
167
167
  #### first
168
168
 
169
- Will return only the first entity found (same thing as setting the page_size to 1)
169
+ Will return only the first entity found (same thing as setting the page_size to 1 and then getting the first result)
170
170
 
171
171
  ```
172
172
  client.assets.first
@@ -203,6 +203,14 @@ Example:
203
203
  client.assets.all(fields: [:code, :description], sort: {code: :asc, description: :desc})
204
204
  ```
205
205
 
206
+ ##### logical_operator
207
+
208
+ Default: "and"
209
+
210
+ This will be only used on complex queries. This is how we treat multiple first level conditions.
211
+
212
+ Accepted values: 'and', 'or'
213
+
206
214
  ##### filter
207
215
 
208
216
  For simple filters, the filter field is waiting for a hash.
@@ -218,7 +226,33 @@ Example:
218
226
  client.assets.all(fields: [:code, :description], filter: {code: ['Buck', :Darcy], description: 'I LOVE SG'})
219
227
  ```
220
228
 
221
- For complex filters, see the documentation [here](https://developer.shotgunsoftware.com/rest-api/#searching)
229
+ For complex filters, see the documentation [here](https://developer.shotgunsoftware.com/rest-api/#searching).
230
+
231
+ If the filters are complex there's many cases:
232
+
233
+ * If they are a hash containing logical_operator and conditions => we will use them
234
+ * If the filter is **not** a hash => we will use it without translation
235
+ * If the filter is a hash not containing "conditions". We will try to translate this to SG compatible query.
236
+
237
+ Example:
238
+ ```ruby
239
+ client.assets.all(
240
+ filter: {
241
+ project: { id: 2 },
242
+ sg_status_list: ["act", "hld", "omt"]
243
+ },
244
+ )
245
+ # Will be translated to:
246
+ {
247
+ "filters"=>{
248
+ "conditions"=> [
249
+ ["project.Project.id", "is", 2],
250
+ ["sg_status_list", "in", ["act", "hld", "omt"]]
251
+ ],
252
+ "logical_operator"=>"and"
253
+ }
254
+ }
255
+ ```
222
256
 
223
257
  The complexity of calling a different route and passing different headers/body/params will be taken care of automatically.
224
258
 
@@ -15,16 +15,18 @@ module ShotgunApiRuby
15
15
  sort: nil,
16
16
  filter: nil,
17
17
  retired: nil,
18
- include_archived_projects: nil
18
+ include_archived_projects: nil,
19
+ logical_operator: 'and'
19
20
  )
20
21
  all(
21
22
  fields: fields,
22
23
  sort: sort,
23
24
  filter: filter,
24
25
  retired: retired,
26
+ logical_operator: logical_operator,
25
27
  include_archived_projects: include_archived_projects,
26
28
  page_size: 1
27
- )
29
+ ).first
28
30
  end
29
31
 
30
32
  def find(id, fields: nil, retired: nil, include_archived_projects: nil)
@@ -52,6 +54,7 @@ module ShotgunApiRuby
52
54
 
53
55
  def all(
54
56
  fields: nil,
57
+ logical_operator: 'and',
55
58
  sort: nil,
56
59
  filter: nil,
57
60
  page: nil,
@@ -62,6 +65,7 @@ module ShotgunApiRuby
62
65
  if filter && !filters_are_simple?(filter)
63
66
  return search(
64
67
  fields: fields,
68
+ logical_operator: logical_operator,
65
69
  sort: sort,
66
70
  filter: filter,
67
71
  page: page,
@@ -99,6 +103,7 @@ module ShotgunApiRuby
99
103
 
100
104
  def search(
101
105
  fields: nil,
106
+ logical_operator: 'and',
102
107
  sort: nil,
103
108
  filter: nil,
104
109
  page: nil,
@@ -109,6 +114,7 @@ module ShotgunApiRuby
109
114
  if filter.nil? || filters_are_simple?(filter)
110
115
  return all(
111
116
  fields: fields,
117
+ logical_operator: logical_operator,
112
118
  sort: sort,
113
119
  filter: filter,
114
120
  page: page,
@@ -123,6 +129,16 @@ module ShotgunApiRuby
123
129
  params.add_sort(sort)
124
130
  params.add_page(page, page_size)
125
131
  params.add_options(retired, include_archived_projects)
132
+ new_filter = {}
133
+ if filter.is_a?(Hash)
134
+ new_filter[:conditions] =
135
+ (filter[:conditions] || translate_complex_to_sg_filters(filter))
136
+ new_filter[:logical_operator] = filter[:logical_operator] || filter['logical_operator'] || logical_operator
137
+ else
138
+ new_filter[:conditions] = filter
139
+ new_filter[:logical_operator] = logical_operator
140
+ end
141
+ filter = new_filter
126
142
 
127
143
  resp =
128
144
  @connection.post('_search', params) do |req|
@@ -132,6 +148,7 @@ module ShotgunApiRuby
132
148
  req.headers['Content-Type'] = 'application/vnd+shotgun.api3_hash+json'
133
149
  end
134
150
  req.body = params.to_h.merge(filters: filter).to_json
151
+ pp JSON.parse(req.body)
135
152
  end
136
153
  resp_body = JSON.parse(resp.body)
137
154
 
@@ -156,8 +173,39 @@ module ShotgunApiRuby
156
173
  return false if filters.is_a? Array
157
174
 
158
175
  filters.values.all? do |filter_val|
159
- (filter_val.is_a?(String) || filter_val.is_a?(Symbol)) ||
160
- (filter_val.is_a?(Array) && filter_val.all?{ |val| val.is_a?(String) || val.is_a?(Symbol) })
176
+ (filter_val.is_a?(Integer) || filter_val.is_a?(String) || filter_val.is_a?(Symbol)) ||
177
+ (filter_val.is_a?(Array) && filter_val.all?{ |val|
178
+ val.is_a?(String) || val.is_a?(Symbol) || val.is_a?(Integer)
179
+ } )
180
+ end
181
+ end
182
+
183
+ def translate_complex_to_sg_filters(filters)
184
+ # We don't know how to translate anything but hashes
185
+ return filters if !filters.is_a?(Hash)
186
+
187
+ filters.each.with_object([]) do |item, result|
188
+ field, value = item
189
+ case value
190
+ when String, Symbol, Integer, Float
191
+ result << [field, "is", value]
192
+ when Hash
193
+ value.each do |subfield, subvalue|
194
+ sanitized_subfield = "#{field.capitalize}.#{subfield}" unless subfield.to_s.include?(".")
195
+ case subvalue
196
+ when String, Symbol, Integer, Float
197
+ result << ["#{field}.#{sanitized_subfield}", "is", subvalue]
198
+ when Array
199
+ result << ["#{field}.#{sanitized_subfield}", "in", subvalue]
200
+ else
201
+ raise "This case is too complex to auto-translate. Please use shotgun query syntax."
202
+ end
203
+ end
204
+ when Array
205
+ result << [field, "in", value]
206
+ else
207
+ raise "This case is too complex to auto-translate. Please use shotgun query syntax."
208
+ end
161
209
  end
162
210
  end
163
211
  end
@@ -25,7 +25,12 @@ module ShotgunApiRuby
25
25
  end
26
26
 
27
27
  def add_fields(fields)
28
- self[:fields] = [fields].flatten.join(',') if fields
28
+ self[:fields] =
29
+ if fields
30
+ [fields].flatten.join(',')
31
+ else
32
+ "*"
33
+ end
29
34
  end
30
35
 
31
36
  def add_options(return_only, include_archived_projects)
@@ -41,7 +46,13 @@ module ShotgunApiRuby
41
46
  return unless filters
42
47
 
43
48
  # filter
44
- self['filter'] = filters.map do |field, value|
49
+ self['filter'] = translate_simple_filter_to_sg(filters)
50
+ end
51
+
52
+ private
53
+
54
+ def translate_simple_filter_to_sg(filters)
55
+ filters.map do |field, value|
45
56
  [
46
57
  field.to_s,
47
58
  value.is_a?(Array) ? value.map(&:to_s).join(',') : value.to_s,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ShotgunApiRuby
4
- VERSION = "0.0.4.1"
4
+ VERSION = "0.0.5"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shotgun_api_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4.1
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis <Zaratan> Pasin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-18 00:00:00.000000000 Z
11
+ date: 2020-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport