Class: WCC::Contentful::ContentTypeIndexer

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/wcc/contentful/content_type_indexer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#constant_from_content_type, #content_type_from_constant, #content_type_from_raw, #shared_prefix

Constructor Details

#initializeContentTypeIndexer

Returns a new instance of ContentTypeIndexer.



25
26
27
28
29
# File 'lib/wcc/contentful/content_type_indexer.rb', line 25

def initialize
  @types = IndexedRepresentation.new({
    'Asset' => create_asset_type
  })
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



23
24
25
# File 'lib/wcc/contentful/content_type_indexer.rb', line 23

def types
  @types
end

Class Method Details

.from_json_schema(schema) ⇒ Object



16
17
18
19
20
# File 'lib/wcc/contentful/content_type_indexer.rb', line 16

def from_json_schema(schema)
  new.tap do |ixr|
    schema.each { |type| ixr.index(type) }
  end
end

.load(schema_file) ⇒ Object



10
11
12
13
14
# File 'lib/wcc/contentful/content_type_indexer.rb', line 10

def load(schema_file)
  from_json_schema(
    JSON.parse(File.read(schema_file))['contentTypes']
  )
end

Instance Method Details

#create_asset_typeObject

hardcoded because the Asset type is a “magic type” in their system



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wcc/contentful/content_type_indexer.rb', line 57

def create_asset_type
  IndexedRepresentation::ContentType.new({
    name: 'Asset',
    content_type: 'Asset',
    fields: {
      'title' => { name: 'title', type: :String },
      'description' => { name: 'description', type: :String },
      'file' => { name: 'file', type: :Json }
    }
  })
end

#create_type(content_type_id, fields) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wcc/contentful/content_type_indexer.rb', line 42

def create_type(content_type_id, fields)
  content_type = IndexedRepresentation::ContentType.new({
    name: constant_from_content_type(content_type_id),
    content_type: content_type_id
  })

  fields.each do |f|
    field = create_field(f)
    content_type.fields[field.name] = field
  end

  content_type
end

#index(content_type) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/wcc/contentful/content_type_indexer.rb', line 31

def index(content_type)
  content_type =
    if content_type.respond_to?(:fields)
      create_type(content_type.id, content_type.fields)
    else
      create_type(content_type.dig('sys', 'id'), content_type['fields'])
    end

  @types[content_type.content_type] = content_type
end