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 avanced usage

Constant Summary collapse

DEFAULT_RESOURCE_MAPPING =
{
  'Space' => Space,
  'ContentType' => ContentType,
  'Entry' => :find_entry_class,
  'Asset' => Asset,
  'Array' => :array_or_sync_page,
  'Link' => Link,
  'DeletedEntry' => DeletedEntry,
  'DeletedAsset' => DeletedAsset,
}
DEFAULT_ENTRY_MAPPING =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, response, resource_mapping = {}, entry_mapping = {}, default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale]) ⇒ ResourceBuilder



32
33
34
35
36
37
38
39
40
41
# File 'lib/contentful/resource_builder.rb', line 32

def initialize(client, response, resource_mapping = {}, entry_mapping = {}, default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale])
  @response = response
  @client = client
  @included_resources = {}
  @known_resources = Hash.new{ |h,k| h[k] = {} }
  @nested_locales = false
  @default_locale = default_locale
  @resource_mapping = default_resource_mapping.merge(resource_mapping)
  @entry_mapping = default_entry_mapping.merge(entry_mapping)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



29
30
31
# File 'lib/contentful/resource_builder.rb', line 29

def client
  @client
end

#entry_mappingObject (readonly)

Returns the value of attribute entry_mapping.



29
30
31
# File 'lib/contentful/resource_builder.rb', line 29

def entry_mapping
  @entry_mapping
end

#resourceObject (readonly)

Returns the value of attribute resource.



29
30
31
# File 'lib/contentful/resource_builder.rb', line 29

def resource
  @resource
end

#resource_mappingObject (readonly)

Returns the value of attribute resource_mapping.



29
30
31
# File 'lib/contentful/resource_builder.rb', line 29

def resource_mapping
  @resource_mapping
end

#responseObject (readonly)

Returns the value of attribute response.



29
30
31
# File 'lib/contentful/resource_builder.rb', line 29

def response
  @response
end

Instance Method Details

#array_or_sync_page(object) ⇒ Object

Detects if a resource is an Contentful::Array or a SyncPage



120
121
122
123
124
125
126
# File 'lib/contentful/resource_builder.rb', line 120

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



112
113
114
115
116
117
# File 'lib/contentful/resource_builder.rb', line 112

def content_type_id_for_entry(object)
  object["sys"] &&
  object["sys"]["contentType"] &&
  object["sys"]["contentType"]["sys"] &&
  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



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/contentful/resource_builder.rb', line 69

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
end

#create_resource(object) ⇒ Object

Creates a single resource from the response object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/contentful/resource_builder.rb', line 82

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, @default_locale)

  add_to_known_resources res
  replace_children res, object
  replace_child_array res.items if res.array?

  res
end

#default_entry_mappingObject

The default entry mapping



156
157
158
# File 'lib/contentful/resource_builder.rb', line 156

def default_entry_mapping
  DEFAULT_ENTRY_MAPPING.dup
end

#default_resource_mappingObject

The default mapping for #detect_resource_class



151
152
153
# File 'lib/contentful/resource_builder.rb', line 151

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/contentful/resource_builder.rb', line 135

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
    raise UnparsableResource.new(response)
  else
    res_class
  end
end

#find_entry_class(object) ⇒ Object

Checks in a custom class for an entry was defined in entry_mapping



95
96
97
# File 'lib/contentful/resource_builder.rb', line 95

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



105
106
107
108
109
# File 'lib/contentful/resource_builder.rb', line 105

def get_dynamic_entry(object)
  if content_id = content_type_id_for_entry(object)
    client.dynamic_entry_cache[content_id.to_sym]
  end
end

#runObject

Starts the parsing process. Either returns an Error, or the parsed Resource



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/contentful/resource_builder.rb', line 45

def run
  case response.status
  when :contentful_error
    Error[response.raw.status].new(response)
  when :unparsable_json
    UnparsableJson.new(response)
  when :not_contentful
    Error.new(response)
  else
    begin
      create_all_resources!
    rescue UnparsableResource => error
      error
    end
  end
end

#try_dynamic_entry(object) ⇒ Object

Automatically converts Entry to DynamicEntry if in cache



100
101
102
# File 'lib/contentful/resource_builder.rb', line 100

def try_dynamic_entry(object)
  get_dynamic_entry(object) || Entry
end