CFSync.Entry.Extractors (CFSync v0.18.0)

View Source

Utility functions to extract data from contenful JSON.

Summary

Types

Entry's payload as provided to CFSync.Entry.Fields.new/1

Functions

Returns value of field_name as an atom.

Returns value of field_name as a binary.

Returns value of field_name as a boolean.

Returns value of field_name processed by fun.

Returns value of field_name as a Date.

Returns value of field_name as a DateTime.

Returns value of field_name as a CFSync.Link.

Returns value of field_name as a list of CFSync.Link.

Returns value of field_name as a list.

Returns value of field_name as a map.

Returns value of field_name as a number.

Types

data()

@type data() :: %{
  fields: map(),
  locales: %{required(atom()) => String.t()},
  store: CFSync.store(),
  locale: atom()
}

Entry's payload as provided to CFSync.Entry.Fields.new/1

Functions

extract_atom(data, field_name, mapping, opts \\ [])

@spec extract_atom(data(), String.t(), %{required(any()) => atom()}, keyword()) ::
  nil | atom()

Returns value of field_name as an atom.

  • data is the entry's payload as provided to CFSync.Entry.Fields.new/1
  • field_name is the field's id in Contentful (ie. What is configured in Contentful app)
  • mapping is a map of "value" => :atom used to find which atom correspond to the field's value

Returns nil or default on failure (field empty, no mapping...)

Options

  • default: default value to return if the field is empty or not in the mapping
  • force_locale: a locale to use instead of the entry's locale
  • fallback_locale: locale to fallback to if the field is empty

extract_binary(data, field_name, opts \\ [])

@spec extract_binary(data(), String.t(), keyword()) :: nil | String.t()

Returns value of field_name as a binary.

  • data is the entry's payload as provided to CFSync.Entry.Fields.new/1
  • field_name is the field's id in Contentful (ie. What is configured in Contentful app)

Returns nil or default on failure (field empty, not a string...)

Options

  • default: default value to return if the field is empty or not a binary
  • force_locale: a locale to use instead of the entry's locale
  • fallback_locale: locale to fallback to if the field is empty

extract_boolean(data, field_name, opts \\ [])

@spec extract_boolean(data(), String.t(), keyword()) :: nil | boolean()

Returns value of field_name as a boolean.

  • data is the entry's payload as provided to CFSync.Entry.Fields.new/1
  • field_name is the field's id in Contentful (ie. What is configured in Contentful app)

Returns nil or default on failure (field empty, not a boolean...)

Options

  • default: default value to return if the field is empty or not a boolean
  • force_locale: a locale to use instead of the entry's locale
  • fallback_locale: locale to fallback to if the field is empty

extract_custom(data, field_name, fun, opts \\ [])

@spec extract_custom(data(), String.t(), (any() -> any()), keyword()) :: any()

Returns value of field_name processed by fun.

  • data is the entry's payload as provided to CFSync.Entry.Fields.new/1
  • field_name is the field's id in Contentful (ie. What is configured in Contentful app)
  • fun is a function of arity 1

Returns nil if the field is not included in the payload.

Options

  • force_locale: a locale to use instead of the entry's locale
  • fallback_locale: locale to fallback to if the field is empty

extract_date(data, field_name, opts \\ [])

@spec extract_date(data(), String.t(), keyword()) :: nil | Date.t()

Returns value of field_name as a Date.

  • data is the entry's payload as provided to CFSync.Entry.Fields.new/1
  • field_name is the field's id in Contentful (ie. What is configured in Contentful app)

Returns nil or default on failure (field empty, invalid format, invalid date...)

Options

  • default: default value to return if the field is empty or not a date
  • force_locale: a locale to use instead of the entry's locale
  • fallback_locale: locale to fallback to if the field is empty

extract_datetime(data, field_name, opts \\ [])

@spec extract_datetime(data(), String.t(), keyword()) :: nil | DateTime.t()

Returns value of field_name as a DateTime.

  • data is the entry's payload as provided to CFSync.Entry.Fields.new/1
  • field_name is the field's id in Contentful (ie. What is configured in Contentful app)

Returns nil or default on failure (field empty, invalid format, invalid datetime...)

Options

  • default: default value to return if the field is empty or not a datetime
  • force_locale: a locale to use instead of the entry's locale
  • fallback_locale: locale to fallback to if the field is empty

extract_link(data, field_name, opts \\ [])

@spec extract_link(data(), String.t(), keyword()) :: nil | CFSync.Link.t()

Returns value of field_name as a CFSync.Link.

  • data is the entry's payload as provided to CFSync.Entry.Fields.new/1
  • field_name is the field's id in Contentful (ie. What is configured in Contentful app)

Returns nil or default on failure (field empty, not a link...)

Options

  • default: default value to return if the field is empty or not a link
  • force_locale: a locale to use instead of the entry's locale
  • fallback_locale: locale to fallback to if the field is empty

extract_links(data, field_name, opts \\ [])

@spec extract_links(data(), String.t(), keyword()) :: nil | [CFSync.Link.t()]

Returns value of field_name as a list of CFSync.Link.

  • data is the entry's payload as provided to CFSync.Entry.Fields.new/1
  • field_name is the field's id in Contentful (ie. What is configured in Contentful app)

Returns nil or default on failure (field empty, not a list...)

Options

  • default: default value to return if the field is empty or not a list
  • force_locale: a locale to use instead of the entry's locale
  • fallback_locale: locale to fallback to if the field is empty

extract_list(data, field_name, opts \\ [])

@spec extract_list(data(), String.t(), keyword()) :: nil | list()

Returns value of field_name as a list.

  • data is the entry's payload as provided to CFSync.Entry.Fields.new/1
  • field_name is the field's id in Contentful (ie. What is configured in Contentful app)

Returns nil or default on failure (field empty, not a list...)

Options

  • default: default value to return if the field is empty or not a list
  • force_locale: a locale to use instead of the entry's locale
  • fallback_locale: locale to fallback to if the field is empty

extract_map(data, field_name, opts \\ [])

@spec extract_map(data(), String.t(), keyword()) :: nil | map()

Returns value of field_name as a map.

  • data is the entry's payload as provided to CFSync.Entry.Fields.new/1
  • field_name is the field's id in Contentful (ie. What is configured in Contentful app)

Returns nil or default on failure (field empty, not a map...)

Options

  • default: default value to return if the field is empty or not a map
  • force_locale: a locale to use instead of the entry's locale
  • fallback_locale: locale to fallback to if the field is empty

extract_number(data, field_name, opts \\ [])

@spec extract_number(data(), String.t(), keyword()) :: nil | number()

Returns value of field_name as a number.

  • data is the entry's payload as provided to CFSync.Entry.Fields.new/1
  • field_name is the field's id in Contentful (ie. What is configured in Contentful app)

Be careful with the result as it can be either an integer or float, depending of it's value. A contentful decimal value of 1.0 will be stored as 1 in the JSON and read as an integer by JASON.

Returns nil or default on failure (field empty, not a number...)

Options

  • default: default value to return if the field is empty or not a number
  • force_locale: a locale to use instead of the entry's locale
  • fallback_locale: locale to fallback to if the field is empty

extract_rich_text(data, field_name, opts \\ [])

@spec extract_rich_text(data(), String.t(), keyword()) :: nil | CFSync.RichText.t()

Returns value of field_name as CFSync.RichText tree.

  • data is the entry's payload as provided to CFSync.Entry.Fields.new/1
  • field_name is the field's id in Contentful (ie. What is configured in Contentful app)

Returns nil or default on failure (field empty, not a richtext...)

Options

  • default: default value to return if the field is empty or not a richtext
  • force_locale: a locale to use instead of the entry's locale
  • fallback_locale: locale to fallback to if the field is empty