shotgun_api_ruby 0.1.0.1 → 0.1.1

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: ebb179d1404b03fd02a47b587b48e8a3cbbdfa69aa56141f2c4b3a9d73819eaf
4
- data.tar.gz: ce95e7a5d7e7567ada62ac2604a6fc6f639cdacc3c61804fdd171a04de8309f1
3
+ metadata.gz: 4ba416f8dc0858ce91112f5f283d36878e31e777438b293c07ecfa63bfda0553
4
+ data.tar.gz: 2a8d1af31bd3936b5290250f6865bd832d76e4f92734bf0175d581be29bb71b3
5
5
  SHA512:
6
- metadata.gz: 03fb2158143962471d55adc758434ed2817e6d04989d00694025bb2f876d6d7f2bc44525c5eb16832fe55f6dd87e9bb9335a7bbf55d9af5be011bcf9ffb29b55
7
- data.tar.gz: 9d7d48612614d7db28ad1c10342dc604a6bd19638c019fcc6c5599da93623c65414be561c8855ff63e8bd97de3ed745c81f39f9fe8666914e1d6e0eab438917a
6
+ metadata.gz: 42f2984ef2a0f98e0e54d9e6c470bed71647d3f8a49d9dff17a8111f8081478425320f44f21ac65b2e41f6c5e7c395c9fbcf382393ff2c9ff4017be48c50c068
7
+ data.tar.gz: d1bb4d22f4375688fe930b36c31a9a90c0bab8081103924671fe834cb7ee036ca9106cea64faf8ffbb52d1f2da63e3d9c1a39aa433d64ba9363db18c28d46eae
data/CHANGELOG.md CHANGED
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.1.1] - 2021-06-15
10
+ ### Added
11
+ - summarize and count support
12
+
9
13
  ## [0.1.0] - 2021-06-14
10
14
  ### Added
11
15
  - Complete test coverage (Unit + Integration tests)
@@ -42,7 +46,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
42
46
  - Entities: delete
43
47
  - Entities: revive
44
48
 
45
- [Unreleased]: https://github.com/shotgunsoftware/shotgun_api_ruby/compare/v0.1.0...HEAD
49
+ [Unreleased]: https://github.com/shotgunsoftware/shotgun_api_ruby/compare/v0.1.1...HEAD
50
+ [0.1.1]: https://github.com/shotgunsoftware/shotgun_api_ruby/releases/tag/v0.1.1
46
51
  [0.1.0]: https://github.com/shotgunsoftware/shotgun_api_ruby/releases/tag/v0.1.0
47
52
  [0.0.8.5]: https://github.com/shotgunsoftware/shotgun_api_ruby/releases/tag/v0.0.8.5
48
53
  [0.0.8.4]: https://github.com/shotgunsoftware/shotgun_api_ruby/releases/tag/v0.0.8.4
data/README.md CHANGED
@@ -306,7 +306,7 @@ Example:
306
306
  client.assets.find(724, fields: [:code, 'description'], retired: false)
307
307
  ```
308
308
 
309
- #### Create
309
+ ### Create
310
310
 
311
311
  Will create the entity referenced by the id with the following fields.
312
312
  If successful, it will return the newly created entity.
@@ -315,7 +315,7 @@ If successful, it will return the newly created entity.
315
315
  client.assets.create(code: 'New Asset', project: {type: 'Project', id: 63})
316
316
  ```
317
317
 
318
- #### Update
318
+ ### Update
319
319
 
320
320
  Will update the entity referenced by the id with the following fields.
321
321
  If successful, it will return the modified entity.
@@ -324,7 +324,7 @@ If successful, it will return the modified entity.
324
324
  client.assets.update(1226, code: 'Updated Asset', sg_status_list: 'fin')
325
325
  ```
326
326
 
327
- #### Delete
327
+ ### Delete
328
328
 
329
329
  Will destroys the entity referenced by the id. Will return true if successful.
330
330
 
@@ -332,7 +332,7 @@ Will destroys the entity referenced by the id. Will return true if successful.
332
332
  client.assets.delete(1226)
333
333
  ```
334
334
 
335
- #### Revive
335
+ ### Revive
336
336
 
337
337
  Will try to revive the entity referenced by the id. Will return true if successful.
338
338
 
@@ -340,6 +340,89 @@ Will try to revive the entity referenced by the id. Will return true if successf
340
340
  client.assets.revive(1226)
341
341
  ```
342
342
 
343
+ ### Summarize
344
+
345
+ Will summarize data for an entity type.
346
+
347
+ Example:
348
+ ```ruby
349
+ # Simplest example
350
+ client.assets.summarize(summary_fields: {id: :count})
351
+
352
+ # Full complex example
353
+ client.assets.summarize(
354
+ filter: { project: { id: 155 }, sg_status_list: :act },
355
+ logical_operator: 'or',
356
+ include_archived_projects: true,
357
+ grouping: {
358
+ code: {direction: :desc, type: 'exact'}
359
+ },
360
+ summary_fields: { id: :count }
361
+ )
362
+
363
+ # Raw shotgun queries
364
+ client.assets.summarize(
365
+ grouping: [
366
+ {
367
+ "field": "sg_asset_type",
368
+ "type": "exact",
369
+ "direction": "asc"
370
+ }
371
+ ],
372
+ summary_fields: [
373
+ {
374
+ "field": "id",
375
+ "type": "count"
376
+ }
377
+ ],
378
+ )
379
+ ```
380
+
381
+ It accepts the same `filter` and `logical_operator` as a `search` will.
382
+
383
+ #### Summary fields
384
+
385
+ Those can have two forms:
386
+
387
+ ##### The normal API form
388
+
389
+ You need to supply the summary_fields as an array and it will be passed directly to the SG REST API
390
+
391
+ #### The convenient form
392
+
393
+ Using an array isn't very convenient most of the time. You can use a hash instead and it will be translated into a "SG summary_fields array".
394
+
395
+ Each key of the hash is the field name and the corresponding value is the type a summary you want (can be a string or a symbol)
396
+
397
+ #### Grouping
398
+
399
+ Those can have two forms:
400
+
401
+ ##### The normal API form
402
+
403
+ You need to supply the grouping as an array and it will be passed directly to the SG REST API
404
+
405
+ #### The convenient form
406
+
407
+ Using an array isn't very convenient most of the time. You can use a hash instead and it will be translated into a "SG grouping array".
408
+
409
+ Each key of the hash is the field name and the corresponding value can either be :
410
+ * A String/Symbol and then will be used a a direction. The type will be 'exact'
411
+ * A Hash with optional 'type' and 'direction' keys. If a key is not specified it will be 'exact' and 'asc' respectively.
412
+
413
+ ### Count
414
+
415
+ This is a helper for more a readable count summary. This can be passed `filter` and `logical_operator`.
416
+
417
+ Example:
418
+
419
+ ```ruby
420
+ client.assets.count
421
+
422
+ # This will be equivalent as doing:
423
+ client.assets.summarize(summary_fields: [{type: :record_count, field: :id}])
424
+ ```
425
+
343
426
  ### Schema
344
427
 
345
428
  Those calls allow to inspect the schema for a shotgun site.
@@ -212,7 +212,7 @@ module ShotgunApiRuby
212
212
  params.add_filter(filter, logical_operator)
213
213
 
214
214
  # In search: The name is filters and not filter
215
- params[:filters] = params[:filter]
215
+ params[:filters] = params[:filter] if params[:filter]
216
216
  params.delete(:filter)
217
217
 
218
218
  resp =
@@ -253,5 +253,29 @@ module ShotgunApiRuby
253
253
  def fields
254
254
  schema_client.fields
255
255
  end
256
+
257
+ def summary_client
258
+ @summary_client ||= Summarize.new(connection, type, @base_url_prefix)
259
+ end
260
+
261
+ def count(filter: nil, logical_operator: 'and')
262
+ summary_client.count(filter: filter, logical_operator: logical_operator)
263
+ end
264
+
265
+ def summarize(
266
+ filter: nil,
267
+ grouping: nil,
268
+ summary_fields: nil,
269
+ logical_operator: 'and',
270
+ include_archived_projects: nil
271
+ )
272
+ summary_client.summarize(
273
+ filter: filter,
274
+ grouping: grouping,
275
+ summary_fields: summary_fields,
276
+ logical_operator: logical_operator,
277
+ include_archived_projects: include_archived_projects,
278
+ )
279
+ end
256
280
  end
257
281
  end
@@ -63,6 +63,51 @@ module ShotgunApiRuby
63
63
  end
64
64
  end
65
65
 
66
+ def add_grouping(grouping)
67
+ return unless grouping
68
+
69
+ if grouping.is_a? Array
70
+ self[:grouping] = grouping
71
+ return
72
+ end
73
+
74
+ self[:grouping] =
75
+ grouping
76
+ .each
77
+ .with_object([]) do |(key, options), result|
78
+ if options.is_a? Hash
79
+ result << {
80
+ field: key.to_s,
81
+ type:
82
+ options[:type]&.to_s || options['type']&.to_s || 'exact',
83
+ direction:
84
+ options[:direction]&.to_s || options['direction']&.to_s ||
85
+ 'asc',
86
+ }
87
+ else
88
+ result << {
89
+ field: key.to_s,
90
+ type: 'exact',
91
+ direction: options.to_s,
92
+ }
93
+ end
94
+ end
95
+ end
96
+
97
+ def add_summary_fields(summary_fields)
98
+ return unless summary_fields
99
+
100
+ if summary_fields.is_a? Array
101
+ self[:summary_fields] = summary_fields
102
+ return
103
+ end
104
+
105
+ if summary_fields.is_a? Hash
106
+ self[:summary_fields] =
107
+ summary_fields.map { |k, v| { field: k.to_s, type: v.to_s } }
108
+ end
109
+ end
110
+
66
111
  def self.filters_are_simple?(filters)
67
112
  return false if filters.is_a? Array
68
113
 
@@ -0,0 +1,64 @@
1
+ module ShotgunApiRuby
2
+ class Entities
3
+ class Summarize
4
+ Summary = Struct.new(:summaries, :groups)
5
+
6
+ def initialize(connection, type, base_url_prefix)
7
+ @connection = connection.dup
8
+ @type = type
9
+ @connection.url_prefix = "#{base_url_prefix}/entity/#{type}/_summarize"
10
+ end
11
+ attr_reader :type, :connection
12
+
13
+ def count(filter: nil, logical_operator: 'and')
14
+ result =
15
+ summarize(
16
+ filter: filter,
17
+ logical_operator: logical_operator,
18
+ summary_fields: [{ type: :record_count, field: 'id' }],
19
+ )
20
+ result.summaries&.[]('id') || 0
21
+ end
22
+
23
+ def summarize(
24
+ filter: nil,
25
+ grouping: nil,
26
+ summary_fields: nil,
27
+ logical_operator: 'and',
28
+ include_archived_projects: nil
29
+ )
30
+ params = Params.new
31
+
32
+ params.add_filter(filter, logical_operator)
33
+
34
+ params[:filters] = params[:filter] if params[:filter]
35
+ params.delete(:filter)
36
+
37
+ params.add_grouping(grouping)
38
+ params.add_summary_fields(summary_fields)
39
+ params.add_options(nil, include_archived_projects)
40
+
41
+ resp =
42
+ @connection.post('', params) do |req|
43
+ req.headers['Content-Type'] =
44
+ if params[:filters].is_a? Array
45
+ 'application/vnd+shotgun.api3_array+json'
46
+ else
47
+ 'application/vnd+shotgun.api3_hash+json'
48
+ end
49
+ req.body = params.to_h.to_json
50
+ end
51
+ resp_body = JSON.parse(resp.body)
52
+
53
+ if resp.status >= 300
54
+ raise "Error while getting summarize for #{type}: #{resp_body['errors']}"
55
+ end
56
+
57
+ Summary.new(
58
+ resp_body['data']['summaries'],
59
+ resp_body['data']&.[]('groups'),
60
+ )
61
+ end
62
+ end
63
+ end
64
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ShotgunApiRuby
4
- VERSION = '0.1.0.1'
4
+ VERSION = '0.1.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shotgun_api_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis <Zaratan> Pasin
@@ -353,6 +353,7 @@ files:
353
353
  - lib/shotgun_api_ruby/entities.rb
354
354
  - lib/shotgun_api_ruby/entities/params.rb
355
355
  - lib/shotgun_api_ruby/entities/schema.rb
356
+ - lib/shotgun_api_ruby/entities/summarize.rb
356
357
  - lib/shotgun_api_ruby/entity.rb
357
358
  - lib/shotgun_api_ruby/preferences.rb
358
359
  - lib/shotgun_api_ruby/server_info.rb