Class: NotionRubyMapping::Block

Inherits:
Base
  • Object
show all
Defined in:
lib/notion_ruby_mapping/blocks/block.rb

Overview

Notion block

Instance Attribute Summary collapse

Attributes inherited from Base

#archived, #has_children, #id, #json

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#append_block_children, #assert_parent_children_pair, #assign_property, #block?, block_id, #children, #comments, #cover, create_from_json, #created_time, #database?, database_id, dry_run_script, #get, #icon, #inspect, #json_properties, #last_edited_time, #new_record?, #page?, page_id, #parent, #parent_id, #properties, #property_values_json, #reload, #restore_from_json, #save, #set_cover, #set_icon, #synced_block_original?, #update_json

Constructor Details

#initialize(json: nil, id: nil, parent: nil) ⇒ Block

Returns a new instance of Block.



10
11
12
13
14
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 10

def initialize(json: nil, id: nil, parent: nil)
  super
  @can_have_children = false
  @can_append = true
end

Instance Attribute Details

#can_appendObject (readonly)

Returns the value of attribute can_append.



15
16
17
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 15

def can_append
  @can_append
end

#can_have_childrenObject (readonly)

Returns the value of attribute can_have_children.



15
16
17
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 15

def can_have_children
  @can_have_children
end

#captionObject (readonly)

Returns the value of attribute caption.



15
16
17
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 15

def caption
  @caption
end

#colorObject (readonly)

Returns the value of attribute color.



15
16
17
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 15

def color
  @color
end

#languageObject (readonly)

Returns the value of attribute language.



15
16
17
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 15

def language
  @language
end

#rich_text_arrayObject (readonly)

Returns the value of attribute rich_text_array.



15
16
17
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 15

def rich_text_array
  @rich_text_array
end

#typeObject (readonly)

Returns the value of attribute type.



15
16
17
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 15

def type
  @type
end

#urlObject (readonly)

Returns the value of attribute url.



15
16
17
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 15

def url
  @url
end

Class Method Details

.decode_block(json) ⇒ Object



61
62
63
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 61

def self.decode_block(json)
  type2class(json["type"], json["has_children"]).new json: json
end

.find(id, dry_run: false) ⇒ String, NotionRubyMapping::Block

Parameters:

  • id (String)
  • dry_run (Boolean) (defaults to: false)

    true if dry_run

Returns:

See Also:



69
70
71
72
73
74
75
76
77
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 69

def self.find(id, dry_run: false)
  nc = NotionCache.instance
  block_id = Base.block_id id
  if dry_run
    Base.dry_run_script :get, nc.block_path(block_id)
  else
    nc.block block_id
  end
end

.type2class(type, has_children = false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 17

def self.type2class(type, has_children = false)
  @type2class ||= {
    false => {
      bookmark: BookmarkBlock,
      breadcrumb: BreadcrumbBlock,
      bulleted_list_item: BulletedListItemBlock,
      callout: CalloutBlock,
      child_database: ChildDatabaseBlock,
      child_page: ChildPageBlock,
      code: CodeBlock,
      column: ColumnBlock,
      column_list: ColumnListBlock,
      divider: DividerBlock,
      embed: EmbedBlock,
      equation: EquationBlock,
      file: FileBlock,
      heading_1: Heading1Block,
      heading_2: Heading2Block,
      heading_3: Heading3Block,
      image: ImageBlock,
      link_preview: LinkPreviewBlock,
      link_to_page: LinkToPageBlock,
      numbered_list_item: NumberedListItemBlock,
      paragraph: ParagraphBlock,
      pdf: PdfBlock,
      quote: QuoteBlock,
      synced_block: SyncedBlock,
      table: TableBlock,
      table_row: TableRowBlock,
      table_of_contents: TableOfContentsBlock,
      template: TemplateBlock,
      to_do: ToDoBlock,
      toggle: ToggleBlock,
      video: VideoBlock,
    },
    true => {
      heading_1: ToggleHeading1Block,
      heading_2: ToggleHeading2Block,
      heading_3: ToggleHeading3Block,
    },
  }
  @klass = @type2class[has_children][type.to_sym] || @type2class[false][type.to_sym] || Block
end

Instance Method Details

#append_after(*blocks, dry_run: false) ⇒ NotionRubyMapping::Block, String

Parameters:

  • dry_run (Boolean) (defaults to: false)

    true if dry_run

  • blocks (Array<Block>)

Returns:



82
83
84
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 82

def append_after(*blocks, dry_run: false)
  parent.append_block_children(*blocks, after: id, dry_run: dry_run)
end

#block_json(not_update: true) ⇒ Hash{String (frozen)->Hash

Returns ].

Parameters:

  • not_update (Boolean) (defaults to: true)

    false when update

Returns:

  • (Hash{String (frozen)->Hash)

    ]



88
89
90
91
92
93
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 88

def block_json(not_update: true)
  ans = {"type" => type}
  ans["object"] = "block"
  ans["archived"] = true if @archived
  ans
end

#children_block_jsonHash{String (frozen)->Array<Hash{String (frozen)->Hash}

Returns }].

Returns:

  • (Hash{String (frozen)->Array<Hash{String (frozen)->Hash})

    }]



96
97
98
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 96

def children_block_json
  {"children" => [block_json]}
end

#decode_block_captionNotionRubyMapping::RichTextArray



101
102
103
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 101

def decode_block_caption
  @caption = RichTextArray.new "caption", json: @json[type]["caption"] if @json[type]["caption"]
end

#decode_block_rich_text_arrayNotionRubyMapping::RichTextArray



111
112
113
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 111

def decode_block_rich_text_array
  @rich_text_array = RichTextArray.new "rich_text", json: @json[type]["rich_text"]
end

#decode_colorString

Returns:

  • (String)


106
107
108
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 106

def decode_color
  @color = @json[type]["color"]
end

#destroy(dry_run: false) ⇒ NotionRubyMapping::Base, String

Parameters:

  • dry_run (Boolean) (defaults to: false)

    true if dry_run

Returns:



117
118
119
120
121
122
123
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 117

def destroy(dry_run: false)
  if dry_run
    Base.dry_run_script :delete, @nc.block_path(@id)
  else
    @nc.destroy_block id
  end
end

#update(dry_run: false) ⇒ NotionRubyMapping::Base, String

Parameters:

  • dry_run (Boolean) (defaults to: false)

    true if dry_run

Returns:



127
128
129
130
131
132
133
134
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 127

def update(dry_run: false)
  if dry_run
    dry_run_script :patch, @nc.block_path(@id), :update_block_json
  else
    @json = @nc.update_block_request(@id, update_block_json)
    update_file_object_from_json(@json) if is_a? FileBaseBlock
  end
end

#update_block_jsonHash

Returns:

  • (Hash)


137
138
139
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 137

def update_block_json
  @payload.update_block_json type, block_json(not_update: false)
end