module Contentful::Management::Resource

Include this module to declare a class to be a contentful resource. This is done by the default in the existing resource classes

You can define your own classes that behave like contentful resources: See examples/custom_classes.rb to see how.

Take a look at examples/resource_mapping.rb on how to register them to be returned by the client by default

@see _ examples/custom_classes.rb Custom Class as Resource @see _ examples/resource_mapping.rb Mapping a Custom Class

Constants

COERCIONS

@private rubocop:disable Style/DoubleNegation

Attributes

client[RW]
properties[R]

rubocop:enable Style/DoubleNegation

raw_object[R]

rubocop:enable Style/DoubleNegation

request[R]

rubocop:enable Style/DoubleNegation

Public Class Methods

included(base) click to toggle source

@private

# File lib/contentful/management/resource.rb, line 50
def self.included(base)
  base.extend(ClassMethods)
end
new(object = nil, request = nil, client = nil, nested_locale_fields = false) click to toggle source

@private

# File lib/contentful/management/resource.rb, line 36
def initialize(object = nil,
               request = nil,
               client = nil,
               nested_locale_fields = false)
  self.class.update_coercions!
  @nested_locale_fields = nested_locale_fields

  @properties = extract_from_object object, :property, self.class.property_coercions.keys
  @request = request
  @client = client
  @raw_object = object
end

Public Instance Methods

after_create(_attributes) click to toggle source

@private

# File lib/contentful/management/resource.rb, line 55
def after_create(_attributes); end
array?() click to toggle source

Returns true for resources that behave like an array

# File lib/contentful/management/resource.rb, line 101
def array?
  false
end
default_locale() click to toggle source

Get default_locale from client

# File lib/contentful/management/resource.rb, line 138
def default_locale
  client.default_locale
end
destroy() click to toggle source

Destroys a resource.

@return [true, Contentful::Management::Error] success

# File lib/contentful/management/resource.rb, line 86
def destroy
  ResourceRequester.new(client, self.class).destroy(
    space_id: space.id,
    environment_id: environment_id,
    resource_id: id
  )
end
environment_id() click to toggle source

Returns the Environment ID

# File lib/contentful/management/resource.rb, line 133
def environment_id
  nil
end
fields() click to toggle source

Resources that don’t include Fields or AssetFields return nil for fields

# File lib/contentful/management/resource.rb, line 118
def fields
  nil
end
inspect(info = nil) click to toggle source

@private

# File lib/contentful/management/resource.rb, line 95
def inspect(info = nil)
  properties_info = properties.empty? ? '' : " @properties=#{properties.inspect}"
  "#<#{self.class}:#{properties_info}#{info}>"
end
nested_locale_fields?() click to toggle source

By default, fields come flattened in the current locale. This is different for syncs

# File lib/contentful/management/resource.rb, line 106
def nested_locale_fields?
  # rubocop:disable Style/DoubleNegation
  !!@nested_locale_fields
  # rubocop:enable Style/DoubleNegation
end
resource?() click to toggle source

@return [true]

# File lib/contentful/management/resource.rb, line 128
def resource?
  true
end
save() click to toggle source

Creates or updates a resource.

@return [Contentful::Management::Resource]

# File lib/contentful/management/resource.rb, line 79
def save
  update({})
end
sys() click to toggle source

Resources that don’t include SystemProperties return nil for sys

# File lib/contentful/management/resource.rb, line 113
def sys
  nil
end
update(attributes) click to toggle source

Updates a resource.

@param [Hash] attributes

@see _ README for more information on how to create each resource

@return [Contentful::Management::Resource]

# File lib/contentful/management/resource.rb, line 64
def update(attributes)
  headers = self.class.create_headers(client, attributes, self)
  headers = headers.merge(update_headers)

  ResourceRequester.new(client, self.class).update(
    self,
    update_url_attributes,
    query_attributes(attributes),
    headers
  )
end

Protected Instance Methods

query_attributes(attributes) click to toggle source
# File lib/contentful/management/resource.rb, line 156
def query_attributes(attributes)
  attributes
end
update_headers() click to toggle source
# File lib/contentful/management/resource.rb, line 144
def update_headers
  { version: sys[:version] }
end
update_url_attributes() click to toggle source
# File lib/contentful/management/resource.rb, line 148
def update_url_attributes
  {
    space_id: space.id,
    environment_id: environment_id,
    resource_id: id
  }
end

Private Instance Methods

coerce_or_create_class(value, what) click to toggle source
# File lib/contentful/management/resource.rb, line 188
def coerce_or_create_class(value, what)
  case what
  when Symbol
    COERCIONS[what] ? COERCIONS[what][value] : value
  when Class
    what.new(value, client)
  when Proc
    what[value]
  else
    value
  end
end
coerce_value_or_array(value, what = nil) click to toggle source
# File lib/contentful/management/resource.rb, line 180
def coerce_value_or_array(value, what = nil)
  if value.is_a? ::Array
    value.map { |row| coerce_or_create_class(row, what) }
  else
    coerce_or_create_class(value, what)
  end
end
extract_from_object(object, namespace, keys = nil) click to toggle source
# File lib/contentful/management/resource.rb, line 166
def extract_from_object(object, namespace, keys = nil)
  if object
    keys ||= object.keys
    keys.each.with_object({}) do |name, res|
      res[name.to_sym] = coerce_value_or_array(
        object.is_a?(::Array) ? object : object[name.to_s],
        self.class.public_send(:"#{namespace}_coercions")[name.to_sym]
      )
    end
  else
    {}
  end
end
internal_resource_locale() click to toggle source
# File lib/contentful/management/resource.rb, line 162
def internal_resource_locale
  sys.fetch(:locale, nil) || default_locale
end