Module: Ecoportal::API::Common::Content::DocHelpers

Included in:
HashDiffPatch::InstanceMethods, V2::Pages, V2::Pages::Stages, V2::Registers
Defined in:
lib/ecoportal/api/common/content/doc_helpers.rb

Instance Method Summary collapse

Instance Method Details

#array_id_index(arr, id) ⇒ Integer

Helper used to identify in an Array the position of an object with certain id



51
52
53
54
55
# File 'lib/ecoportal/api/common/content/doc_helpers.rb', line 51

def array_id_index(arr, id)
  return unless arr.is_a?(Array)

  arr.index {|item| get_id(item, exception: false) == id}
end

#array_id_item(arr, id) ⇒ Integer

Helper used to get in an Array and object item with certain id



60
61
62
63
64
# File 'lib/ecoportal/api/common/content/doc_helpers.rb', line 60

def array_id_item(arr, id)
  return unless (idx = array_id_index(arr, id))

  arr[idx]
end

#array_ids(arr) ⇒ Array<String>

Helper used to identify the id s of objects contained in an Array



42
43
44
45
46
# File 'lib/ecoportal/api/common/content/doc_helpers.rb', line 42

def array_ids(arr)
  return [] if !arr.is_a?(Array) || arr.empty?

  arr.map {|item| get_id(item, exception: false)}
end

#get_body(doc, level: 'page') ⇒ Hash?

Helper used to build the body of an HTTP request



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ecoportal/api/common/content/doc_helpers.rb', line 9

def get_body(doc, level: 'page')
  {}.tap do |body|
    body[level.to_s] =
      if doc.respond_to?(:as_update)
        doc.as_update
      elsif doc.respond_to?(:as_json)
        HashDiffPatch.patch_diff(doc.as_json, nil)
      elsif doc.is_a?(Hash)
        HashDiffPatch.patch_diff(doc, nil)
      else
        raise ArgumentError, "Could not get body for doc: #{doc}"
      end
  end
end

#get_id(doc, exception: true) ⇒ Hash?

Helper used to identify the id of the target object



28
29
30
31
32
33
34
35
36
37
# File 'lib/ecoportal/api/common/content/doc_helpers.rb', line 28

def get_id(doc, exception: true)
  id   = nil
  id ||= doc.id    if doc.respond_to?(:id)
  id ||= doc['id'] if doc.is_a?(Hash)
  id ||= doc       if doc.is_a?(String)

  raise 'No ID has been given!' unless id || !exception

  id
end