<aside> 👉

Links (Block base class)

Page

AudioBlock

Block

CalloutBlock

DividerBlock

Heading1Block

ImageBlock

ParagraphBlock

TableOfContentsBlock

ToggleHeading2Block

Database

BookmarkBlock

CodeBlock

EmbedBlock

Heading2Block

LinkPreviewBlock

PdfBlock

ToDoBlock

ToggleHeading3Block

DataSource 🆕

BreadcrumbBlock

ColumnBlock

EquationBlock

Heading3Block

LinkToPageBlock

QuoteBlock

ToggleBlock

ToggleHeading4Block

List

BulletedListItemBlock

ColumnListBlock

FileBlock

Heading4Block

NumberedListItemBlock

SyncedBlock

ToggleHeading1Block

VideoBlock

Notion Ruby Mapping Public API Reference

</aside>

1. Class methods

self.new(text_info = nil, caption: [], language: "shell") → CodeBlock

self.new creates a CodeBlock. text_info is optional; when omitted, the Block has empty rich text. caption is optional and defaults to an empty RichTextArray. When a RichTextArray is supplied for text_info or caption, its contents are copied using the key required by CodeBlock. A CodeBlock cannot have children blocks.

b = CodeBlock.new "Ruby code", language: "ruby", caption: "sample caption"
b.block_json
# => {"type"=>"code", "object"=>"block", "code"=>{"rich_text"=>[{"type"=>"text", "text"=>{"content"=>"Ruby code", "link"=>nil}, "plain_text"=>"Ruby code", "href"=>nil}], "caption"=>[{"type"=>"text", "text"=>{"content"=>"sample caption", "link"=>nil}, "plain_text"=>"sample caption", "href"=>nil}], "language"=>"ruby"}}

2. Instance methods

caption → RichTextArray

caption returns the caption of CodeBlock as a RichTextArray. Modify the returned object to update the caption; the change is included in the next Block update.

b = CodeBlock.new "Ruby code", language: "ruby", caption: "sample caption"
b.caption
# => #<NotionRubyMapping::RichTextArray:...>

b.caption.rich_text_objects = "updated caption"
b.update_block_json
# => {"code"=>{"caption"=>[{"type"=>"text", "text"=>{"content"=>"updated caption", "link"=>nil}, "plain_text"=>"updated caption", "href"=>nil}]}}

language → String

language returns the language identifier of CodeBlock.

b = CodeBlock.new "Ruby code", language: "ruby", caption: "sample caption"
b.language
# => "ruby"

language=(new_language)

language= changes the language identifier of CodeBlock and marks language for the next Block update. The value is not validated by NotionRubyMapping; specify an identifier supported by the Notion API.

b = CodeBlock.new "Ruby code", language: "ruby", caption: "sample caption"
b.language = "shell"
b.update_block_json
# => {"code"=>{"language"=>"shell"}}

save(dry_run: false)

$\fbox{implemented in Block}$

save updates the Block object with update block API. The updated object has block information generated from the JSON response.

block = Block.find "899e342cec84415f9ff86225704cbb75" # Notion API call
block.url = "<https://www.apple.com/>"
block.save
# => #<NotionRubyMapping::Block:...> # updated Block object

Block.find(id, dry_run: true) creates a shell script using Retrieve a block API for verification.

block = Block.find "899e342cec84415f9ff86225704cbb75" # Notion API call
block.url = "<https://www.apple.com/>"
block.save dry_run: true
# => 
# curl -X PATCH '<https://api.notion.com/v1/blocks/899e342cec84415f9ff86225704cbb75>' \
#   -H 'Notion-Version: 2022-02-22' \
#   -H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
#   -H 'Content-Type: application/json' \
#   --data '{"bookmark":{"url":"<https://www.apple.com/>"}}'

rich_text_array → RichTextArray

rich_text_array returns the rich_text value of CodeBlock as a RichTextArray. Modify the returned object to update the code text.

b = CodeBlock.new "Ruby code", language: "ruby", caption: "sample caption"
b.rich_text_array
# => #<NotionRubyMapping::RichTextArray:...>