Module: Contentful::Resource

Included in:
Array, Asset, ContentType, Entry, Field, File, Link, Locale, Location, Space
Defined in:
lib/contentful/resource.rb,
lib/contentful/resource/fields.rb,
lib/contentful/resource/asset_fields.rb,
lib/contentful/resource/system_properties.rb

Overview

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

Defined Under Namespace

Modules: AssetFields, ClassMethods, Fields, SystemProperties

Constant Summary collapse

COERCIONS =
{
  string:  ->(v) { v.to_s },
  integer: ->(v) { v.to_i },
  float:   ->(v) { v.to_f },
  boolean: ->(v) { !!v },
  date:    ->(v) { DateTime.parse(v) }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



22
23
24
# File 'lib/contentful/resource.rb', line 22

def client
  @client
end

#propertiesObject (readonly)

Returns the value of attribute properties.



22
23
24
# File 'lib/contentful/resource.rb', line 22

def properties
  @properties
end

#requestObject (readonly)

Returns the value of attribute request.



22
23
24
# File 'lib/contentful/resource.rb', line 22

def request
  @request
end

Instance Method Details

#array?Boolean

Returns true for resources that behave like an array

Returns:

  • (Boolean)


38
39
40
# File 'lib/contentful/resource.rb', line 38

def array?
  false
end

#fieldsObject

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



48
49
50
# File 'lib/contentful/resource.rb', line 48

def fields
  nil
end

#initialize(object, request = nil, client = nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/contentful/resource.rb', line 24

def initialize(object, request = nil, client = nil)
  self.class.update_coercions!

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

#inspect(info = nil) ⇒ Object



32
33
34
35
# File 'lib/contentful/resource.rb', line 32

def inspect(info = nil)
  properties_info = properties.empty? ? '' : " @properties=#{properties.inspect}"
  "#<#{self.class}:#{properties_info}#{info}>"
end

#reloadObject

Issues the request that was made to fetch this response again. Only works for top-level resources



54
55
56
57
58
59
60
# File 'lib/contentful/resource.rb', line 54

def reload
  if request
    request.get
  else
    false
  end
end

#sysObject

Resources that don’t include SystemProperties return nil for #sys



43
44
45
# File 'lib/contentful/resource.rb', line 43

def sys
  nil
end