Module: EPlat::Countable

Included in:
Base
Defined in:
lib/e_plat/resource/countable.rb

Instance Method Summary collapse

Instance Method Details

#count(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/e_plat/resource/countable.rb', line 6

def count(options = {})
  initialize_singleton!

  data = 
    if counts_via_graphql?
      graphql_count
    elsif counts_via_meta_data?
      raw_data  = connection.get(collection_path, headers)
      data_hash = ActiveSupport::JSON.decode(raw_data.body)
      data_hash.dig("meta", "pagination", "total")
    elsif count_key_next_to_root?
      raw = get(:count, options)
      JSON.parse(raw.full_response.body)
    else
      get(:count, options)
    end

  count = 
    case data
    when Hash then data["count"]
    else data
    end

  Integer(count)
end