Module: Pipedrive::Fields::ClassMethods

Defined in:
lib/pipedrive/fields.rb

Instance Method Summary collapse

Instance Method Details

#fieldsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pipedrive/fields.rb', line 13

def fields
  url = fields_url || "#{class_name.downcase}Fields"

  data = []
  start = 0
  request_more_fields = true

  while request_more_fields
    response = request(:get, url, start: start)
    data.concat(response.dig(:data))
    # Check wether there are more fields to bring
     = response.dig(:additional_data, :pagination)
    request_more_fields = &.fetch(:more_items_in_collection, false)
    start = [:next_start] if request_more_fields
  end
  # return a hash prefilled with
  # the fields hash and name parameterized
  # and the original array of fields (schema)
  dicc = data.reduce({}) do |fields_dicc, field|
    # snakify the name
    snake_name = field.dig(:name).gsub(/\w+/).reduce([]) do |words, c|
      words << c.downcase
    end.join("_")

    fields_dicc.merge(
      field.dig(:key).to_sym => snake_name.to_sym
    )
  end
  [dicc, data]
end