Class: Contentful::Management::ResourceBuilder
- Inherits:
-
Object
- Object
- Contentful::Management::ResourceBuilder
- Defined in:
- lib/contentful/management/resource_builder.rb
Overview
Transforms a Contentful::Response into a Contentful::Resource or a Contentful::Error See example/resource_mapping.rb for avanced usage
Constant Summary collapse
- DEFAULT_RESOURCE_MAPPING =
Default Resource Mapping
{ 'Space' => Contentful::Management::Space, 'SpaceMembership' => Contentful::Management::SpaceMembership, 'Organization' => Contentful::Management::Organization, 'SpacePeriodicUsage' => Contentful::Management::SpacePeriodicUsage, 'OrganizationPeriodicUsage' => Contentful::Management::OrganizationPeriodicUsage, 'User' => Contentful::Management::User, 'Environment' => Contentful::Management::Environment, 'ContentType' => Contentful::Management::ContentType, 'Entry' => :find_entry_class, 'Asset' => Contentful::Management::Asset, 'Array' => :array_or_sync_page, 'Link' => Contentful::Management::Link, 'WebhookDefinition' => Contentful::Management::Webhook, 'WebhookCallOverview' => Contentful::Management::WebhookCall, 'WebhookCallDetails' => Contentful::Management::WebhookCall, 'Webhook' => Contentful::Management::WebhookHealth, 'ApiKey' => Contentful::Management::ApiKey, 'PreviewApiKey' => Contentful::Management::PreviewApiKey, 'PersonalAccessToken' => Contentful::Management::PersonalAccessToken, 'Locale' => Contentful::Management::Locale, 'Role' => Contentful::Management::Role, 'Extension' => Contentful::Management::UIExtension, 'EditorInterface' => Contentful::Management::EditorInterface, 'Snapshot' => Contentful::Management::Snapshot, 'Upload' => Contentful::Management::Upload, 'Tag' => Contentful::Management::Tag }.freeze
- DEFAULT_ENTRY_MAPPING =
Default Entry Mapping
{}.freeze
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#entry_mapping ⇒ Object
readonly
Returns the value of attribute entry_mapping.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#resource_mapping ⇒ Object
readonly
Returns the value of attribute resource_mapping.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#array_or_sync_page(object) ⇒ Object
Detects if a resource is an Contentful::Array or a SyncPage.
-
#content_type_id_for_entry(object) ⇒ Object
Returns the id of the related ContentType, if there is one.
-
#create_all_resources! ⇒ Object
PARSING MECHANISM - raise error if response not valid - look for included objects and parse them to resources - parse main object to resource - replace links in included resources with known resources - replace links in main resource with known resources - return main resource.
-
#create_resource(object) ⇒ Object
Creates a single resource from the response object.
-
#default_entry_mapping ⇒ Object
The default entry mapping.
-
#default_resource_mapping ⇒ Object
The default mapping for #detect_resource_class.
-
#detect_resource_class(object) ⇒ Object
Uses the resource mapping to find the proper Resource class to initialize for this Response object type.
-
#find_entry_class(object) ⇒ Object
Checks in a custom class for an entry was defined in entry_mapping.
-
#get_dynamic_entry(object) ⇒ Object
Finds the proper DynamicEntry class for an entry.
-
#initialize(response, client, resource_mapping = {}, entry_mapping = {}) ⇒ ResourceBuilder
constructor
A new instance of ResourceBuilder.
-
#run ⇒ Contentful::Management::Resource, Contentful::Management::Error
Starts the parsing process.
-
#try_dynamic_entry(object) ⇒ Object
Automatically converts Entry to DynamicEntry if in cache.
Constructor Details
#initialize(response, client, resource_mapping = {}, entry_mapping = {}) ⇒ ResourceBuilder
Returns a new instance of ResourceBuilder.
74 75 76 77 78 79 80 81 82 |
# File 'lib/contentful/management/resource_builder.rb', line 74 def initialize(response, client, resource_mapping = {}, entry_mapping = {}) @response = response @client = client @included_resources = {} @known_resources = Hash.new { |hash, key| hash[key] = {} } @nested_locales = true @resource_mapping = default_resource_mapping.merge(resource_mapping) @entry_mapping = default_entry_mapping.merge(entry_mapping) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
72 73 74 |
# File 'lib/contentful/management/resource_builder.rb', line 72 def client @client end |
#entry_mapping ⇒ Object (readonly)
Returns the value of attribute entry_mapping.
72 73 74 |
# File 'lib/contentful/management/resource_builder.rb', line 72 def entry_mapping @entry_mapping end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
72 73 74 |
# File 'lib/contentful/management/resource_builder.rb', line 72 def resource @resource end |
#resource_mapping ⇒ Object (readonly)
Returns the value of attribute resource_mapping.
72 73 74 |
# File 'lib/contentful/management/resource_builder.rb', line 72 def resource_mapping @resource_mapping end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
72 73 74 |
# File 'lib/contentful/management/resource_builder.rb', line 72 def response @response end |
Instance Method Details
#array_or_sync_page(object) ⇒ Object
Detects if a resource is an Contentful::Array or a SyncPage
150 151 152 153 154 155 156 |
# File 'lib/contentful/management/resource_builder.rb', line 150 def array_or_sync_page(object) if object['nextPageUrl'] || object['nextSyncUrl'] SyncPage else Array end end |
#content_type_id_for_entry(object) ⇒ Object
Returns the id of the related ContentType, if there is one
145 146 147 |
# File 'lib/contentful/management/resource_builder.rb', line 145 def content_type_id_for_entry(object) object['sys']['contentType']['sys']['id'] end |
#create_all_resources! ⇒ Object
PARSING MECHANISM
-
raise error if response not valid
-
look for included objects and parse them to resources
-
parse main object to resource
-
replace links in included resources with known resources
-
replace links in main resource with known resources
-
return main resource
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/contentful/management/resource_builder.rb', line 101 def create_all_resources! create_included_resources! response.object['includes'] @resource = create_resource(response.object) unless @included_resources.empty? replace_links_in_included_resources_with_known_resources replace_links_with_known_resources @resource end @resource rescue UnparsableResource => e e end |
#create_resource(object) ⇒ Object
Creates a single resource from the response object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/contentful/management/resource_builder.rb', line 116 def create_resource(object) res_class = detect_resource_class(object) @nested_locales ||= res_class.nested_locale_fields? res = res_class.new(object, response.request, client, @nested_locales) add_to_known_resources res replace_children res, object replace_child_array res.items if res.array? res end |
#default_entry_mapping ⇒ Object
The default entry mapping
185 186 187 |
# File 'lib/contentful/management/resource_builder.rb', line 185 def default_entry_mapping DEFAULT_ENTRY_MAPPING.dup end |
#default_resource_mapping ⇒ Object
The default mapping for #detect_resource_class
180 181 182 |
# File 'lib/contentful/management/resource_builder.rb', line 180 def default_resource_mapping DEFAULT_RESOURCE_MAPPING.dup end |
#detect_resource_class(object) ⇒ Object
Uses the resource mapping to find the proper Resource class to initialize for this Response object type
The mapping value can be a
-
Class
-
Proc: Will be called, expected to return the proper Class
-
Symbol: Will be called as method of the ResourceBuilder itself
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/contentful/management/resource_builder.rb', line 165 def detect_resource_class(object) type = object['sys'] && object['sys']['type'] case res_class = resource_mapping[type] when Symbol public_send(res_class, object) when Proc res_class[object] when nil fail UnparsableResource, response else res_class end end |
#find_entry_class(object) ⇒ Object
Checks in a custom class for an entry was defined in entry_mapping
129 130 131 |
# File 'lib/contentful/management/resource_builder.rb', line 129 def find_entry_class(object) entry_mapping[content_type_id_for_entry(object)] || try_dynamic_entry(object) end |
#get_dynamic_entry(object) ⇒ Object
Finds the proper DynamicEntry class for an entry
139 140 141 142 |
# File 'lib/contentful/management/resource_builder.rb', line 139 def get_dynamic_entry(object) content_id = content_type_id_for_entry(object) client.dynamic_entry_cache[content_id.to_sym] if content_id end |
#run ⇒ Contentful::Management::Resource, Contentful::Management::Error
Starts the parsing process.
86 87 88 89 90 91 92 |
# File 'lib/contentful/management/resource_builder.rb', line 86 def run if response.status == :ok create_all_resources! else response.object end end |
#try_dynamic_entry(object) ⇒ Object
Automatically converts Entry to DynamicEntry if in cache
134 135 136 |
# File 'lib/contentful/management/resource_builder.rb', line 134 def try_dynamic_entry(object) get_dynamic_entry(object) || Contentful::Management::Entry end |