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' => :try_dynamic_entry,
  'Asset' => Asset,
  'Array' => Array,
  'Link' => Link,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, response, resource_mapping = {}) ⇒ ResourceBuilder

Returns a new instance of ResourceBuilder.



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

def initialize(client, response, resource_mapping = {})
  @response = response
  @client = client
  @included_resources = {}
  @resource_mapping = default_resource_mapping.merge(resource_mapping)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



24
25
26
# File 'lib/contentful/resource_builder.rb', line 24

def client
  @client
end

#resource_mappingObject (readonly)

Returns the value of attribute resource_mapping.



24
25
26
# File 'lib/contentful/resource_builder.rb', line 24

def resource_mapping
  @resource_mapping
end

#responseObject (readonly)

Returns the value of attribute response.



24
25
26
# File 'lib/contentful/resource_builder.rb', line 24

def response
  @response
end

Instance Method Details

#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 included resources

  • replace links in main resource with included resources

  • return main resource



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/contentful/resource_builder.rb', line 60

def create_all_resources!
  create_included_resources! response.object['includes']
  res = create_resource(response.object)

  unless @included_resources.empty?
    replace_links_in_included_resources_with_included_resources
    replace_links_with_included_resources(res)
  end

  res
end

#create_resource(object) ⇒ Object

Creates a single resource from the



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

def create_resource(object)
  res = detect_resource_class(object).new(object, response.request, client)
  replace_children res, object
  replace_child_array res.items if res.array?

  res
end

#default_resource_mappingObject

The default mapping for #detect_resource_class



118
119
120
# File 'lib/contentful/resource_builder.rb', line 118

def default_resource_mapping
  DEFAULT_RESOURCE_MAPPING
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



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/contentful/resource_builder.rb', line 103

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

#get_dynamic_entry(object) ⇒ Object

Finds the proper DynamicEntry class for an entry



87
88
89
90
91
92
93
94
# File 'lib/contentful/resource_builder.rb', line 87

def get_dynamic_entry(object)
  if id = object["sys"] &&
      object["sys"]["contentType"] &&
      object["sys"]["contentType"]["sys"] &&
      object["sys"]["contentType"]["sys"]["id"]
    client.dynamic_entry_cache[id.to_sym]
  end
end

#runObject

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



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

def run
  case response.status
  when :contentful_error
    Error[response.raw.response.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

When using Dynamic Entry Mode: Automatically converts Entry to DynamicEntry



82
83
84
# File 'lib/contentful/resource_builder.rb', line 82

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