Class: Contentful::ResourceBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful/resource_builder.rb

Overview

Transforms a Contentful::Response into a Contentful::Resource or a Contentful::Error See example/resource_mapping.rb for advanced usage

Constant Summary

DEFAULT_RESOURCE_MAPPING =

Default Resource Mapping

See Also:

  • README for more information on Resource Mapping
{
  'Space' => Space,
  'ContentType' => ContentType,
  'Entry' => Entry,
  'Asset' => Asset,
  'Array' => Array,
  'Link' => Link,
  'DeletedEntry' => DeletedEntry,
  'DeletedAsset' => DeletedAsset
}
DEFAULT_ENTRY_MAPPING =

Default Entry Mapping

See Also:

  • README for more information on Entry Mapping
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json, configuration = {}, localized = false, depth = 0, endpoint = nil) ⇒ ResourceBuilder

Returns a new instance of ResourceBuilder



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/contentful/resource_builder.rb', line 33

def initialize(json, configuration = {}, localized = false, depth = 0, endpoint = nil)
  @json = json
  @default_locale = configuration.fetch(:default_locale, ::Contentful::Client::DEFAULT_CONFIGURATION[:default_locale])
  @resource_mapping = default_resource_mapping.merge(configuration.fetch(:resource_mapping, {}))
  @entry_mapping = default_entry_mapping.merge(configuration.fetch(:entry_mapping, {}))
  @includes_for_single = configuration.fetch(:includes_for_single, [])
  @localized = localized
  @depth = depth
  @endpoint = endpoint
  @configuration = configuration
  @entries = configuration[:_entries_cache] || {}
end

Instance Attribute Details

#default_localeObject (readonly)

Returns the value of attribute default_locale



31
32
33
# File 'lib/contentful/resource_builder.rb', line 31

def default_locale
  @default_locale
end

#depthObject (readonly)

Returns the value of attribute depth



31
32
33
# File 'lib/contentful/resource_builder.rb', line 31

def depth
  @depth
end

#endpointObject (readonly)

Returns the value of attribute endpoint



31
32
33
# File 'lib/contentful/resource_builder.rb', line 31

def endpoint
  @endpoint
end

#entry_mappingObject (readonly)

Returns the value of attribute entry_mapping



31
32
33
# File 'lib/contentful/resource_builder.rb', line 31

def entry_mapping
  @entry_mapping
end

#jsonObject (readonly)

Returns the value of attribute json



31
32
33
# File 'lib/contentful/resource_builder.rb', line 31

def json
  @json
end

#localizedObject (readonly)

Returns the value of attribute localized



31
32
33
# File 'lib/contentful/resource_builder.rb', line 31

def localized
  @localized
end

#resourceObject (readonly)

Returns the value of attribute resource



31
32
33
# File 'lib/contentful/resource_builder.rb', line 31

def resource
  @resource
end

#resource_mappingObject (readonly)

Returns the value of attribute resource_mapping



31
32
33
# File 'lib/contentful/resource_builder.rb', line 31

def resource_mapping
  @resource_mapping
end

Instance Method Details

#runContentful::Resource, Contentful::Error

Starts the parsing process.

Returns:



49
50
51
52
53
54
# File 'lib/contentful/resource_builder.rb', line 49

def run
  return build_array if array?
  build_single
rescue UnparsableResource => error
  error
end