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

#has_children, #id, #in_trash, #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, #data_source?, data_source_id, #database?, database_id, dry_run_script, #get, #icon, #inspect, #json_properties, #last_edited_time, #markdown, #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



63
64
65
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 63

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

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



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

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
60
61
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 17

def self.type2class(type, has_children = false)
  @type2class ||= {
    false => {
      audio: AudioBlock,
      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,
      heading_4: Heading4Block,
      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,
      to_do: ToDoBlock,
      toggle: ToggleBlock,
      video: VideoBlock,
    },
    true => {
      heading_1: ToggleHeading1Block,
      heading_2: ToggleHeading2Block,
      heading_3: ToggleHeading3Block,
      heading_4: ToggleHeading4Block,
    },
  }
  @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



84
85
86
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 84

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

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



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

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

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



98
99
100
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 98

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

#decode_block_captionNotionRubyMapping::RichTextArray



103
104
105
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 103

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

#decode_block_rich_text_arrayNotionRubyMapping::RichTextArray



113
114
115
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 113

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

#decode_colorString



108
109
110
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 108

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

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



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

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



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

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



139
140
141
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 139

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