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 collapse

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,
  'Locale' => Locale
}.freeze
DEFAULT_ENTRY_MAPPING =

Default Entry Mapping

See Also:

  • README for more information on Entry Mapping
{}.freeze
BUILDABLES =
%w[Entry Asset ContentType Space DeletedEntry DeletedAsset Locale].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json, configuration = {}, localized = false, depth = 0, errors = []) ⇒ ResourceBuilder

Returns a new instance of ResourceBuilder.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/contentful/resource_builder.rb', line 36

def initialize(json, configuration = {}, localized = false, depth = 0, errors = [])
  @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 = configuration.fetch(:endpoint, nil)
  @configuration = configuration
  @resource_cache = configuration[:_entries_cache] || {}
  @errors = errors
end

Instance Attribute Details

#default_localeObject (readonly)

Returns the value of attribute default_locale.



34
35
36
# File 'lib/contentful/resource_builder.rb', line 34

def default_locale
  @default_locale
end

#depthObject (readonly)

Returns the value of attribute depth.



34
35
36
# File 'lib/contentful/resource_builder.rb', line 34

def depth
  @depth
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



34
35
36
# File 'lib/contentful/resource_builder.rb', line 34

def endpoint
  @endpoint
end

#entry_mappingObject (readonly)

Returns the value of attribute entry_mapping.



34
35
36
# File 'lib/contentful/resource_builder.rb', line 34

def entry_mapping
  @entry_mapping
end

#jsonObject (readonly)

Returns the value of attribute json.



34
35
36
# File 'lib/contentful/resource_builder.rb', line 34

def json
  @json
end

#localizedObject (readonly)

Returns the value of attribute localized.



34
35
36
# File 'lib/contentful/resource_builder.rb', line 34

def localized
  @localized
end

#resourceObject (readonly)

Returns the value of attribute resource.



34
35
36
# File 'lib/contentful/resource_builder.rb', line 34

def resource
  @resource
end

#resource_mappingObject (readonly)

Returns the value of attribute resource_mapping.



34
35
36
# File 'lib/contentful/resource_builder.rb', line 34

def resource_mapping
  @resource_mapping
end

Instance Method Details

#runContentful::Resource, Contentful::Error

Starts the parsing process.

Returns:



53
54
55
56
57
58
# File 'lib/contentful/resource_builder.rb', line 53

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