Class: Graphiti::Stats::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/graphiti/stats/payload.rb

Overview

Generate the stats payload so we can return it in the response.

{
  data: [...],
  meta: { stats: the_generated_payload }
}

For example:

{
  data: [...],
  meta: { stats: { total: { count: 100 } } }
}

Instance Method Summary collapse

Constructor Details

#initialize(resource, query, scope, data) ⇒ Payload

Returns a new instance of Payload.



17
18
19
20
21
22
# File 'lib/graphiti/stats/payload.rb', line 17

def initialize(resource, query, scope, data)
  @resource = resource
  @query = query
  @scope = scope
  @data = data
end

Instance Method Details

#calculate_stat(name, function) ⇒ Object



40
41
42
43
44
45
# File 'lib/graphiti/stats/payload.rb', line 40

def calculate_stat(name, function)
  args = [@scope, name]
  args << @resource.context if function.arity >= 3
  args << @data if function.arity == 4
  function.call(*args)
end

#generateHash

Generate the payload for { meta: { stats: { … } } } Loops over all calculations, computes then, and gives back a hash of stats and their results.

Returns:

  • (Hash)

    the generated payload



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/graphiti/stats/payload.rb', line 28

def generate
  {}.tap do |stats|
    @query.stats.each_pair do |name, calculation|
      stats[name] = {}

      each_calculation(name, calculation) do |calc, function|
        stats[name][calc] = calculate_stat(name, function)
      end
    end
  end
end