class Contentful::BaseResource

Base definition of a Contentful Resource containing Sys properties

Constants

TIMESTAMPS

Attributes

_metadata[R]
default_locale[R]
raw[R]
sys[R]

Public Class Methods

new(item, configuration = {}, _localized = false, _includes = Includes.new, entries = {}, depth = 0, _errors = []) click to toggle source

rubocop:disable Metrics/ParameterLists

# File lib/contentful/base_resource.rb, line 11
def initialize(item, configuration = {}, _localized = false, _includes = Includes.new, entries = {}, depth = 0, _errors = [])
  entries["#{item['sys']['type']}:#{item['sys']['id']}"] = self if entries && item.key?('sys')
  @raw = item
  @default_locale = configuration[:default_locale]
  @depth = depth
  @configuration = configuration
  @sys = hydrate_sys
  @_metadata = hydrate_metadata

  define_sys_methods!
end

Public Instance Methods

==(other) click to toggle source

Definition of equality

# File lib/contentful/base_resource.rb, line 29
def ==(other)
  self.class == other.class && sys[:id] == other.sys[:id]
end
inspect() click to toggle source

@private

# File lib/contentful/base_resource.rb, line 24
def inspect
  "<#{repr_name} id='#{sys[:id]}'>"
end
marshal_dump() click to toggle source

@private

# File lib/contentful/base_resource.rb, line 34
def marshal_dump
  entry_mapping = @configuration[:entry_mapping].each_with_object({}) do |(k, v), res|
    res[k] = v.to_s
  end

  {
    # loggers usually have a file handle that can't be marshalled, so let's not return that
    configuration: @configuration.merge(entry_mapping: entry_mapping, logger: nil),
    raw: raw
  }
end
marshal_load(raw_object) click to toggle source

@private

# File lib/contentful/base_resource.rb, line 47
def marshal_load(raw_object)
  raw_object[:configuration][:entry_mapping] = raw_object[:configuration].fetch(:entry_mapping, {}).each_with_object({}) do |(k, v), res|
    begin
      v = v.to_s unless v.is_a?(::String)
      res[k] = v.split('::').inject(Object) { |o, c| o.const_get c }
    rescue
      next
    end
  end

  @raw = raw_object[:raw]
  @configuration = raw_object[:configuration]
  @default_locale = @configuration[:default_locale]
  @sys = hydrate_sys
  @_metadata = hydrate_metadata
  @depth = 0
  define_sys_methods!
end
reload(client = nil) click to toggle source

Issues the request that was made to fetch this response again. Only works for Entry, Asset, ContentType and Space

# File lib/contentful/base_resource.rb, line 68
def reload(client = nil)
  return client.send(Support.snakify(self.class.name.split('::').last), id) unless client.nil?

  false
end

Protected Instance Methods

internal_resource_locale() click to toggle source
# File lib/contentful/base_resource.rb, line 113
def internal_resource_locale
  sys.fetch(:locale, nil) || default_locale
end
repr_name() click to toggle source
# File lib/contentful/base_resource.rb, line 109
def repr_name
  self.class
end

Private Instance Methods

define_sys_methods!() click to toggle source
# File lib/contentful/base_resource.rb, line 76
def define_sys_methods!
  @sys.each do |k, v|
    define_singleton_method(k) { v } unless self.class.method_defined?(k)
  end
end
hydrate_metadata() click to toggle source
# File lib/contentful/base_resource.rb, line 98
def hydrate_metadata
  result = {}
  raw.fetch('metadata', {}).each do |k, v|
    v = v.map { |tag| build_link(tag) } if k == 'tags'
    result[Support.snakify(k, @configuration[:use_camel_case]).to_sym] = v
  end
  result
end
hydrate_sys() click to toggle source
# File lib/contentful/base_resource.rb, line 85
def hydrate_sys
  result = {}
  raw.fetch('sys', {}).each do |k, v|
    if LINKS.include?(k)
      v = build_link(v)
    elsif TIMESTAMPS.include?(k)
      v = DateTime.parse(v)
    end
    result[Support.snakify(k, @configuration[:use_camel_case]).to_sym] = v
  end
  result
end