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 =

Buildable Resources

%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 = [], query = {}) ⇒ ResourceBuilder

Returns a new instance of ResourceBuilder.



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

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

Instance Attribute Details

#default_localeObject (readonly)

Returns the value of attribute default_locale.



36
37
38
# File 'lib/contentful/resource_builder.rb', line 36

def default_locale
  @default_locale
end

#depthObject (readonly)

Returns the value of attribute depth.



36
37
38
# File 'lib/contentful/resource_builder.rb', line 36

def depth
  @depth
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



36
37
38
# File 'lib/contentful/resource_builder.rb', line 36

def endpoint
  @endpoint
end

#entry_mappingObject (readonly)

Returns the value of attribute entry_mapping.



36
37
38
# File 'lib/contentful/resource_builder.rb', line 36

def entry_mapping
  @entry_mapping
end

#jsonObject (readonly)

Returns the value of attribute json.



36
37
38
# File 'lib/contentful/resource_builder.rb', line 36

def json
  @json
end

#localizedObject (readonly)

Returns the value of attribute localized.



36
37
38
# File 'lib/contentful/resource_builder.rb', line 36

def localized
  @localized
end

#queryObject (readonly)

Returns the value of attribute query.



36
37
38
# File 'lib/contentful/resource_builder.rb', line 36

def query
  @query
end

#resourceObject (readonly)

Returns the value of attribute resource.



36
37
38
# File 'lib/contentful/resource_builder.rb', line 36

def resource
  @resource
end

#resource_mappingObject (readonly)

Returns the value of attribute resource_mapping.



36
37
38
# File 'lib/contentful/resource_builder.rb', line 36

def resource_mapping
  @resource_mapping
end

Instance Method Details

#runContentful::Resource, Contentful::Error

Starts the parsing process.

Returns:



56
57
58
59
60
61
# File 'lib/contentful/resource_builder.rb', line 56

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