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, TemplateBlock, 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
#archived, #has_children, #id, #json
Class Method Summary collapse
- .decode_block(json) ⇒ Object
- .find(id, dry_run: false) ⇒ 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, 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_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
62 63 64 |
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 62 def self.decode_block(json) type2class(json[:type], json[:has_children]).new json: json end |
.find(id, dry_run: false) ⇒ NotionRubyMapping::Block
70 71 72 73 74 75 76 77 78 |
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 70 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 |
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 17 def self.type2class(type, has_children = false) type_sym = type.to_sym @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_sym] || @type2class[false][type_sym] || Block end |
Instance Method Details
#append_after(*blocks, dry_run: false) ⇒ NotionRubyMapping::Block, String
83 84 85 |
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 83 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
89 90 91 92 93 94 |
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 89 def block_json(not_update: true) ans = {type: type.to_s} ans[:object] = "block" ans[:archived] = true if @archived ans end |
#children_block_json ⇒ Hash{String (frozen)->Array<Hash{String (frozen)->Hash}
97 98 99 |
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 97 def children_block_json {children: [block_json]} end |
#decode_block_caption ⇒ NotionRubyMapping::RichTextArray
102 103 104 |
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 102 def decode_block_caption @caption = RichTextArray.new :caption, json: @json[type][:caption] end |
#decode_block_rich_text_array ⇒ NotionRubyMapping::RichTextArray
112 113 114 |
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 112 def decode_block_rich_text_array @rich_text_array = RichTextArray.new :rich_text, json: @json[type][:rich_text] end |
#decode_color ⇒ String
107 108 109 |
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 107 def decode_color @color = @json[type][:color] end |
#destroy(dry_run: false) ⇒ NotionRubyMapping::Base, String
118 119 120 121 122 123 124 |
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 118 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
128 129 130 131 132 133 134 |
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 128 def update(dry_run: false) if dry_run dry_run_script :patch, @nc.block_path(@id), :update_block_json else update_json @nc.update_block_request(@id, update_block_json) end end |
#update_block_json ⇒ 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 |