Class: NotionToMd::Blocks::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/notion_to_md/blocks/builder.rb

Constant Summary collapse

BLOCKS_WITH_PERMITTED_CHILDREN =

Array containing the block types allowed to have nested blocks (children).

%i[
  bulleted_list_item
  numbered_list_item
  paragraph
  to_do
  table
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block_id:, &fetch_blocks) ⇒ Builder

Parameters

block_id

A string representing a notion block id .

fetch_blocks

A block that fetches the blocks from the Notion API.

Returns

An array of NotionToMd::Blocks::Block.



39
40
41
42
# File 'lib/notion_to_md/blocks/builder.rb', line 39

def initialize(block_id:, &fetch_blocks)
  @block_id = block_id
  @fetch_blocks = fetch_blocks
end

Instance Attribute Details

#block_idObject (readonly)

Returns the value of attribute block_id.



28
29
30
# File 'lib/notion_to_md/blocks/builder.rb', line 28

def block_id
  @block_id
end

#fetch_blocksObject (readonly)

Returns the value of attribute fetch_blocks.



28
29
30
# File 'lib/notion_to_md/blocks/builder.rb', line 28

def fetch_blocks
  @fetch_blocks
end

Class Method Details

.permitted_children_for?(block:) ⇒ Boolean

Parameters

block

A Notion::Messages::Message object.

Returns

A boolean indicating if the blocked passed in is permitted to have children based on its type.

Returns:

  • (Boolean)


24
25
26
# File 'lib/notion_to_md/blocks/builder.rb', line 24

def self.permitted_children_for?(block:)
  BLOCKS_WITH_PERMITTED_CHILDREN.include?(block.type.to_sym) && block.has_children
end

Instance Method Details

#buildObject

Parameters

Returns

An array of NotionToMd::Blocks::Block.



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/notion_to_md/blocks/builder.rb', line 49

def build
  notion_messages = fetch_blocks.call(block_id)
  blocks = notion_messages.results.map do |block|
    children = if Builder.permitted_children_for?(block: block)
                 Builder.new(block_id: block.id, &fetch_blocks).build
               else
                 []
               end
    Factory.build(block: block, children: children)
  end

  Normalizer.normalize(blocks: blocks)
end