Class: NotionRubyMapping::Block
- Defined in:
- lib/notion_ruby_mapping/blocks/block.rb
Overview
Notion block
Direct Known Subclasses
BreadcrumbBlock, CalloutBlock, ChildBaseBlock, CodeBlock, ColumnBlock, ColumnListBlock, DividerBlock, EquationBlock, FileBaseBlock, LinkToPageBlock, SyncedBlock, TableBlock, TableOfContentsBlock, TableRowBlock, TextSubBlockColorBaseBlock, UrlBaseBlock, UrlCaptionBaseBlock
Instance Attribute Summary collapse
-
#can_append ⇒ Object
readonly
Returns the value of attribute can_append.
-
#can_have_children ⇒ Object
readonly
Returns the value of attribute can_have_children.
-
#caption ⇒ Object
readonly
Returns the value of attribute caption.
-
#color ⇒ Object
readonly
Returns the value of attribute color.
-
#language ⇒ Object
readonly
Returns the value of attribute language.
-
#rich_text_array ⇒ Object
readonly
Returns the value of attribute rich_text_array.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Attributes inherited from Base
#has_children, #id, #in_trash, #json
Class Method Summary collapse
- .decode_block(json) ⇒ Object
- .find(id, dry_run: false) ⇒ String, NotionRubyMapping::Block
- .type2class(type, has_children = false) ⇒ Object
Instance Method Summary collapse
- #append_after(*blocks, dry_run: false) ⇒ NotionRubyMapping::Block, String
-
#block_json(not_update: true) ⇒ Hash{String (frozen)->Hash
].
-
#children_block_json ⇒ Hash{String (frozen)->Array<Hash{String (frozen)->Hash}
}].
- #decode_block_caption ⇒ NotionRubyMapping::RichTextArray
- #decode_block_rich_text_array ⇒ NotionRubyMapping::RichTextArray
- #decode_color ⇒ String
- #destroy(dry_run: false) ⇒ NotionRubyMapping::Base, String
-
#initialize(json: nil, id: nil, parent: nil) ⇒ Block
constructor
A new instance of Block.
- #update(dry_run: false) ⇒ NotionRubyMapping::Base, String
- #update_block_json ⇒ Hash
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_append ⇒ Object (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_children ⇒ Object (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 |
#caption ⇒ Object (readonly)
Returns the value of attribute caption.
15 16 17 |
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 15 def caption @caption end |
#color ⇒ Object (readonly)
Returns the value of attribute color.
15 16 17 |
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 15 def color @color end |
#language ⇒ Object (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_array ⇒ Object (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 |
#type ⇒ Object (readonly)
Returns the value of attribute type.
15 16 17 |
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 15 def type @type end |
#url ⇒ Object (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_json ⇒ Hash{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_caption ⇒ NotionRubyMapping::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_array ⇒ NotionRubyMapping::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_color ⇒ String
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_json ⇒ Hash
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 |