module Ecoportal::API::Common::Content::DocHelpers

Public Instance Methods

array_id_index(arr, id) click to toggle source

Helper used to identify in an Array the `position` of an object with certain `id` @param doc [Array] the source Array @return [Integer] the `position` thereof

# File lib/ecoportal/api/common/content/doc_helpers.rb, line 49
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) click to toggle source

Helper used to get in an Array and `object` item with certain `id` @param doc [Array] the source Array @return [Integer] the `object` with that `id`

# File lib/ecoportal/api/common/content/doc_helpers.rb, line 57
def array_id_item(arr, id)
  if idx = array_id_index(arr, id)
    arr[idx]
  end
end
array_ids(arr) click to toggle source

Helper used to identify the `id` s of objects contained in an `Array` @param doc [Array] the source Array @return [Array<String>] the `id` s thereof

# File lib/ecoportal/api/common/content/doc_helpers.rb, line 41
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") click to toggle source

Helper used to build the `body` of an HTTP request @param doc [Page, Hash] hashable object @return [Hash, nil] the `patch` formated `data` ready to include as `body` of a HTTP request

# File lib/ecoportal/api/common/content/doc_helpers.rb, line 10
def get_body(doc, level: "page")
  {}.tap do |body|
    body["#{level}"] = case
      when doc.respond_to?(:as_update)
        doc.as_update
      when doc.respond_to?(:as_json)
        Common::Content::HashDiffPatch.patch_diff(doc.as_json, nil)
      when doc.is_a?(Hash)
        Common::Content::HashDiffPatch.patch_diff(doc, nil)
      else
        raise "Could not get body for doc: #{doc}"
      end
  end
end
get_id(doc, exception: true) click to toggle source

Helper used to identify the `id` of the target object @param doc [Page, Hash] hashable object @param exception [Boolean] states if `id` must be present @return [Hash, nil] the `patch` formated `data` ready to include as `body` of a HTTP request

# File lib/ecoportal/api/common/content/doc_helpers.rb, line 29
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