module Contentful::Management::Resource::ClassMethods

Register the resources properties on class level by using the property method

Public Instance Methods

all(client, space_id, environment_id = nil, parameters = {}, organization_id = nil) click to toggle source

Gets a collection of resources.

@param [Contentful::Management::Client] client @param [String] space_id @param [Hash] parameters Search Options @see _ For complete option list: www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters

@return [Contentful::Management::Array<Contentful::Management::Resource>]

# File lib/contentful/management/resource.rb, line 229
def all(client, space_id, environment_id = nil, parameters = {}, organization_id = nil)
  ResourceRequester.new(client, self).all(
    { space_id: space_id, environment_id: environment_id, organization_id: organization_id }, parameters
  )
end
build_endpoint(endpoint_options) click to toggle source

@private

# File lib/contentful/management/resource.rb, line 207
def build_endpoint(endpoint_options)
  if endpoint_options.key?(:public)
    base = "spaces/#{endpoint_options[:space_id]}"
    base = "#{base}/environments/#{endpoint_options[:environment_id]}" if endpoint_options[:environment_id]
    return "#{base}/public/#{endpoint}"
  end

  base = "spaces/#{endpoint_options[:space_id]}"
  base = "#{base}/environments/#{endpoint_options[:environment_id]}" if endpoint_options[:environment_id]
  base = "#{base}/#{endpoint}"
  return "#{base}/#{endpoint_options[:resource_id]}#{endpoint_options[:suffix]}" if endpoint_options[:resource_id]
  base
end
create(client, space_id, environment_id = nil, attributes = {}) click to toggle source

Creates a resource.

@param [Contentful::Management::Client] client @param [String] space_id @param [String] environment_id @param [Hash] attributes @see _ README for full attribute list for each resource.

@return [Contentful::Management::Resource]

# File lib/contentful/management/resource.rb, line 256
def create(client, space_id, environment_id = nil, attributes = {})
  endpoint_options = { space_id: space_id, environment_id: environment_id }
  endpoint_options[:resource_id] = attributes[:id] if attributes.respond_to?(:key) && attributes.key?(:id)
  ResourceRequester.new(client, self).create(
    endpoint_options,
    attributes
  )
end
create_attributes(_client, _attributes) click to toggle source

@private

# File lib/contentful/management/resource.rb, line 271
def create_attributes(_client, _attributes)
  {}
end
create_headers(_client, _attributes, _instance = nil) click to toggle source

@private

# File lib/contentful/management/resource.rb, line 276
def create_headers(_client, _attributes, _instance = nil)
  {}
end
endpoint() click to toggle source

@private

# File lib/contentful/management/resource.rb, line 202
def endpoint
  "#{Support.snakify(name.split('::')[-1])}s"
end
find(client, space_id, environment_id = nil, resource_id = nil, organization_id = nil) click to toggle source

Gets a specific resource.

@param [Contentful::Management::Client] client @param [String] space_id @param [String] resource_id

@return [Contentful::Management::Resource]

# File lib/contentful/management/resource.rb, line 242
def find(client, space_id, environment_id = nil, resource_id = nil, organization_id = nil)
  ResourceRequester.new(client, self).find(space_id: space_id, environment_id: environment_id,
                                           resource_id: resource_id, organization_id: organization_id)
end
nested_locale_fields?() click to toggle source

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

# File lib/contentful/management/resource.rb, line 281
def nested_locale_fields?
  false
end
pre_process_params(parameters) click to toggle source

@private

# File lib/contentful/management/resource.rb, line 266
def pre_process_params(parameters)
  parameters
end
property(name, property_class = nil) click to toggle source

Defines which properties of a resource your class expects Define them in :camelCase, they will be available as snake_cased methods

You can pass in a second “type” argument:

  • If it is a class, it will be initialized for the property

  • Symbols are looked up in the COERCION constant for a lambda that defines a type conversion to apply

Note: This second argument is not meant for contentful sub-resources, but for structured objects (like locales in a space) Sub-resources are handled by the resource builder

# File lib/contentful/management/resource.rb, line 301
def property(name, property_class = nil)
  property_coercions[name.to_sym] = property_class
  accessor_name = Contentful::Management::Support.snakify(name)
  define_method accessor_name do
    properties[name.to_sym]
  end
  define_method "#{accessor_name}=" do |value|
    properties[name.to_sym] = value
  end
end
property_coercions() click to toggle source

Default property coercions

# File lib/contentful/management/resource.rb, line 286
def property_coercions
  @property_coercions ||= {}
end
update_coercions!() click to toggle source

Ensure inherited classes pick up coercions

# File lib/contentful/management/resource.rb, line 313
def update_coercions!
  return if @coercions_updated

  if superclass.respond_to? :property_coercions
    @property_coercions = superclass.property_coercions.dup.merge(@property_coercions || {})
  end

  if superclass.respond_to? :sys_coercions
    @sys_coercions = superclass.sys_coercions.dup.merge(@sys_coercions || {})
  end

  if superclass.respond_to? :fields_coercions
    @fields_coercions = superclass.fields_coercions.dup.merge(@fields_coercions || {})
  end

  @coercions_updated = true
end