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 =
Buildable Resources
%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.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#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 = [], query = {}) ⇒ 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 = [], 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_locale ⇒ Object (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 |
#depth ⇒ Object (readonly)
Returns the value of attribute depth.
36 37 38 |
# File 'lib/contentful/resource_builder.rb', line 36 def depth @depth end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
36 37 38 |
# File 'lib/contentful/resource_builder.rb', line 36 def endpoint @endpoint end |
#entry_mapping ⇒ Object (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 |
#json ⇒ Object (readonly)
Returns the value of attribute json.
36 37 38 |
# File 'lib/contentful/resource_builder.rb', line 36 def json @json end |
#localized ⇒ Object (readonly)
Returns the value of attribute localized.
36 37 38 |
# File 'lib/contentful/resource_builder.rb', line 36 def localized @localized end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
36 37 38 |
# File 'lib/contentful/resource_builder.rb', line 36 def query @query end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
36 37 38 |
# File 'lib/contentful/resource_builder.rb', line 36 def resource @resource end |
#resource_mapping ⇒ Object (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
#run ⇒ Contentful::Resource, Contentful::Error
Starts the parsing process.
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 |