Class: Contentful::ResourceBuilder
- Inherits:
-
Object
- Object
- Contentful::ResourceBuilder
- 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
{ '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
{}.freeze
- BUILDABLES =
%w[Entry Asset ContentType Space DeletedEntry DeletedAsset Locale].freeze
Instance Attribute Summary collapse
-
#default_locale ⇒ Object
readonly
Returns the value of attribute default_locale.
-
#depth ⇒ Object
readonly
Returns the value of attribute depth.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#entry_mapping ⇒ Object
readonly
Returns the value of attribute entry_mapping.
-
#json ⇒ Object
readonly
Returns the value of attribute json.
-
#localized ⇒ Object
readonly
Returns the value of attribute localized.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#resource_mapping ⇒ Object
readonly
Returns the value of attribute resource_mapping.
Instance Method Summary collapse
-
#initialize(json, configuration = {}, localized = false, depth = 0, errors = []) ⇒ ResourceBuilder
constructor
A new instance of ResourceBuilder.
-
#run ⇒ Contentful::Resource, Contentful::Error
Starts the parsing process.
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_locale ⇒ Object (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 |
#depth ⇒ Object (readonly)
Returns the value of attribute depth
34 35 36 |
# File 'lib/contentful/resource_builder.rb', line 34 def depth @depth end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint
34 35 36 |
# File 'lib/contentful/resource_builder.rb', line 34 def endpoint @endpoint end |
#entry_mapping ⇒ Object (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 |
#json ⇒ Object (readonly)
Returns the value of attribute json
34 35 36 |
# File 'lib/contentful/resource_builder.rb', line 34 def json @json end |
#localized ⇒ Object (readonly)
Returns the value of attribute localized
34 35 36 |
# File 'lib/contentful/resource_builder.rb', line 34 def localized @localized end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource
34 35 36 |
# File 'lib/contentful/resource_builder.rb', line 34 def resource @resource end |
#resource_mapping ⇒ Object (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
#run ⇒ Contentful::Resource, Contentful::Error
Starts the parsing process.
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 |