Class: ShotgunApiRuby::Entities::Summarize

Inherits:
Object
  • Object
show all
Defined in:
lib/shotgun_api_ruby/entities/summarize.rb

Defined Under Namespace

Classes: Summary

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, type, base_url_prefix) ⇒ Summarize

Returns a new instance of Summarize.



6
7
8
9
10
# File 'lib/shotgun_api_ruby/entities/summarize.rb', line 6

def initialize(connection, type, base_url_prefix)
  @connection = connection.dup
  @type = type
  @connection.url_prefix = "#{base_url_prefix}/entity/#{type}/_summarize"
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



11
12
13
# File 'lib/shotgun_api_ruby/entities/summarize.rb', line 11

def connection
  @connection
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/shotgun_api_ruby/entities/summarize.rb', line 11

def type
  @type
end

Instance Method Details

#count(filter: nil, logical_operator: 'and') ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/shotgun_api_ruby/entities/summarize.rb', line 13

def count(filter: nil, logical_operator: 'and')
  result =
    summarize(
      filter: filter,
      logical_operator: logical_operator,
      summary_fields: [{ type: :record_count, field: 'id' }],
    )
  result.summaries&.[]('id') || 0
end

#summarize(filter: nil, grouping: nil, summary_fields: nil, logical_operator: 'and', include_archived_projects: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/shotgun_api_ruby/entities/summarize.rb', line 23

def summarize(
  filter: nil,
  grouping: nil,
  summary_fields: nil,
  logical_operator: 'and',
  include_archived_projects: nil
)
  params = Params.new

  params.add_filter(filter, logical_operator)

  params[:filters] = params[:filter] if params[:filter]
  params.delete(:filter)

  params.add_grouping(grouping)
  params.add_summary_fields(summary_fields)
  params.add_options(nil, include_archived_projects)

  resp =
    @connection.post('', params) do |req|
      req.headers['Content-Type'] =
        if params[:filters].is_a? Array
          'application/vnd+shotgun.api3_array+json'
        else
          'application/vnd+shotgun.api3_hash+json'
        end
      req.body = params.to_h.to_json
    end
  resp_body = JSON.parse(resp.body)

  if resp.status >= 300
    raise "Error while getting summarize for #{type}: #{resp_body['errors']}"
  end

  Summary.new(
    resp_body['data']['summaries'],
    resp_body['data']&.[]('groups'),
  )
end