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

#id, #json

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], #append_block_children, #assert_parent_children_pair, #assign_property, #block?, #children, #create_child_breadcrumb, create_from_json, #created_time, #database?, #description, dry_run_script, #icon, #json_properties, #last_edited_time, #new_record?, #page?, #properties, #property_values_json, #reload, #restore_from_json, #save, #set_icon, #update_json

Constructor Details

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

Public announced methods



8
9
10
11
12
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 8

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.



13
14
15
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 13

def can_append
  @can_append
end

#can_have_childrenObject (readonly)

Returns the value of attribute can_have_children.



13
14
15
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 13

def can_have_children
  @can_have_children
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 13

def type
  @type
end

Class Method Details

.find(key) ⇒ NotionRubyMapping::Block



18
19
20
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 18

def self.find(key)
  NotionCache.instance.block key
end

Instance Method Details

#block_jsonHash{String (frozen)->String (frozen) | Array



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 23

def block_json
  ans = {"type" => @type}
  ans["object"] = "block"
  ans["archived"] = true if @archived
  ans["has_children"] = true if @has_children
  case @type
  when "bookmark", "embed"
    @caption.will_update = true
    ans[@type] = @caption.update_property_schema_json
    ans[@type]["url"] = @url
  when "breadcrumb", "divider"
    ans[@type] = {}
  when "callout"
    @rich_text_array.will_update = true
    ans[@type] = @rich_text_array.update_property_schema_json
    ans[@type]["color"] = @color
    ans[@type]["icon"] = @emoji.property_values_json if @emoji
    ans[@type]["icon"] = @file.property_values_json if @file
    ans[@type]["children"] = @sub_blocks.map(&:block_json) if @sub_blocks
  when "child_database", "child_page"
    ans[@type] = {"title" => @title}
  when "code"
    @rich_text_array.will_update = true
    @caption.will_update = true
    ans[@type] = @rich_text_array.update_property_schema_json.merge @caption.update_property_schema_json
    ans[@type]["language"] = @language
  when "column"
    ans[@type] = {}
    ans[@type]["children"] = @sub_blocks.map(&:block_json) if @sub_blocks
    @can_append = false
  when "column_list"
    ans[@type] = {}
    ans[@type]["children"] = @columns.map(&:block_json) if @columns
  when "paragraph", "bulleted_list_item", "heading_1", "heading_2", "heading_3", "numbered_list_item", "quote",
    "toggle"
    @rich_text_array.will_update = true
    ans[@type] = @rich_text_array.update_property_schema_json
    ans[@type]["color"] = @color
    ans[@type]["children"] = @sub_blocks.map(&:block_json) if @sub_blocks
  when "equation"
    ans[@type] = {"expression" => @equation_object.expression}
  when "file", "image", "pdf", "video"
    @caption.will_update = true if @caption
    ans[@type] = @file_object.property_values_json
    ans[@type].merge! @caption.update_property_schema_json if @caption
  when "link_preview"
    ans[@type] = {"url" => @url}
  when "link_to_page"
    ans[@type] = @page_id ?
                   {"type" => "page_id", "page_id" => @page_id} :
                   {"type" => "database_id", "database_id" => @database_id}
  when "synced_block"
    ans[@type] = {"synced_from" => @block_id ? {"type" => "block_id", "block_id" => @block_id} : nil}
    ans[@type]["children"] = @sub_blocks.map(&:block_json) if @sub_blocks
  when "table"
    ans[@type] = {
      "has_column_header" => @has_column_header,
      "has_row_header" => @has_row_header,
      "table_width" => @table_width,
    }
    if @table_rows
      ans[@type]["children"] = @table_rows.map(&:block_json)
    end
  when "table_of_contents"
    ans[@type] = {"color" => @color}
  when "table_row"
    ans[@type] = {"cells" => @cells.map { |cell| Array(cell).map { |to| to.property_values_json } } }
  when "template"
    @rich_text_array.will_update = true
    ans[@type] = @rich_text_array.update_property_schema_json
    ans[@type]["children"] = @sub_blocks.map(&:block_json) if @sub_blocks
  when "to_do"
    @rich_text_array.will_update = true
    ans[@type] = @rich_text_array.update_property_schema_json
    ans[@type]["checked"] = @checked
    ans[@type]["color"] = @color
    ans[@type]["children"] = @sub_blocks.map(&:block_json) if @sub_blocks
  else
    print "not yet implemented"
  end
  ans
end

#bookmark(url, caption: []) ⇒ NotionRubyMapping::Block



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

def bookmark(url, caption: [])
  @type = __method__.to_s
  @url = url
  @caption = RichTextArray.rich_text_array "caption", caption
  self
end


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

def breadcrumb
  @type = __method__.to_s
  self
end

#bulleted_list_item(text_info, sub_blocks: nil, color: "default") ⇒ NotionRubyMapping::Block



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

def bulleted_list_item(text_info, sub_blocks: nil, color: "default")
  @type = __method__.to_s
  rich_text_array_and_color "rich_text", text_info, color
  add_sub_blocks sub_blocks
  self
end

#callout(text_info, emoji: nil, file_url: nil, sub_blocks: nil, color: "default") ⇒ NotionRubyMapping::Block



143
144
145
146
147
148
149
150
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 143

def callout(text_info, emoji: nil, file_url: nil, sub_blocks: nil, color: "default")
  @type = __method__.to_s
  rich_text_array_and_color "rich_text", text_info, color
  @emoji = EmojiObject.emoji_object emoji if emoji
  @file = FileObject.file_object file_url if file_url
  add_sub_blocks sub_blocks
  self
end

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



153
154
155
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 153

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

#code(text_info, caption: [], language: "shell") ⇒ NotionRubyMapping::Block



162
163
164
165
166
167
168
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 162

def code(text_info, caption: [], language: "shell")
  @type = __method__.to_s
  rich_text_array_and_color "rich_text", text_info
  @caption = RichTextArray.rich_text_array "caption", caption
  @language = language
  self
end

#column(sub_blocks) ⇒ NotionRubyMapping::Block

Note:

created by column_list only



173
174
175
176
177
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 173

def column(sub_blocks)
  @type = __method__.to_s
  add_sub_blocks sub_blocks
  self
end

#column_list(array_of_sub_blocks) ⇒ NotionRubyMapping::Block



182
183
184
185
186
187
188
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 182

def column_list(array_of_sub_blocks)
  @type = __method__.to_s
  raise StandardError, "The column_list must have at least 2 columns." if array_of_sub_blocks.count < 2

  @columns = array_of_sub_blocks.map { |sub_blocks| Block.new.column(sub_blocks) }
  self
end

#decode_blockNotionRubyMapping::Block



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 191

def decode_block
  @type = @json["type"]
  sub_json = @json[@type]
  case @type
  when "bookmark", "embed"
    @url = sub_json["url"]
    decode_block_caption
  when "bulleted_list_item", "paragraph", "numbered_list_item", "toggle", "heading_1", "heading_2", "heading_3"
    decode_block_rich_text_array
    decode_color
    @can_have_children = true
  when "quote"
    decode_block_rich_text_array
    decode_color
    @can_have_children = true
  when "callout"
    decode_block_rich_text_array
    decode_color
    @can_have_children = true
  when "child_database", "child_page"
    @title = sub_json["title"]
    @can_append = false
  when "code"
    decode_block_rich_text_array
    decode_block_caption
    @language = sub_json["language"] || "shell"
  when "equation"
    @equation_object = EquationObject.equation_object sub_json["expression"]
  when "file", "image", "pdf", "video"
    @file_object = FileObject.new json: sub_json
    decode_block_caption
    @can_append = @file_object.external?
  when "link_preview"
    @url = sub_json["url"]
    @can_append = false
  when "link_to_page"
    @page_id, @database_id = sub_json.values_at(*%w[page_id database_id])
  when "synced_block"
    @block_id = sub_json["synced_from"] && sub_json["synced_from"]["block_id"]
    @can_have_children = @block_id.nil?
  when "table"
    @has_column_header = sub_json["has_column_header"]
    @has_row_header = sub_json["has_row_header"]
    @table_width = sub_json["table_width"]
    @can_have_children = true
  when "table_of_contents"
    decode_color
  when "table_row"
    print sub_json["cells"]
    @cells = sub_json["cells"].map { |cell| cell.map { |to| RichTextObject.create_from_json to } }
  when "template"
    decode_block_rich_text_array
    @can_have_children = true
  when "to_do"
    decode_block_rich_text_array
    decode_color
    @checked = sub_json["checked"]
    @can_have_children = true
  else
    print "not yet implemented"
  end
  @has_children = @json["has_children"] == "true"
  @archived = @json["archived"] == "true"
  self
end

#decode_block_captionNotionRubyMapping::RichTextArray



258
259
260
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 258

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

#decode_block_rich_text_arrayNotionRubyMapping::RichTextArray



268
269
270
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 268

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

#decode_colorString



263
264
265
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 263

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

#dividerNotionRubyMapping::Block



274
275
276
277
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 274

def divider
  @type = __method__.to_s
  self
end

#embed(url, caption: []) ⇒ Object



282
283
284
285
286
287
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 282

def embed(url, caption: [])
  @type = __method__.to_s
  @url = url
  @caption = RichTextArray.rich_text_array "caption", caption
  self
end

#equation(expression) ⇒ NotionRubyMapping::Block



292
293
294
295
296
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 292

def equation(expression)
  @type = __method__.to_s
  @equation_object = EquationObject.equation_object expression
  self
end

#file(url, caption: []) ⇒ Object



301
302
303
304
305
306
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 301

def file(url, caption: [])
  @type = __method__.to_s
  @file_object = FileObject.file_object url
  @caption = RichTextArray.rich_text_array "caption", caption
  self
end

#heading_1(text_info, color: "default") ⇒ NotionRubyMapping::Block



311
312
313
314
315
316
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 311

def heading_1(text_info, color: "default")
  @type = __method__.to_s
  rich_text_array_and_color "rich_text", text_info, color
  @can_have_children = true
  self
end

#heading_2(text_info, color: "default") ⇒ NotionRubyMapping::Block



321
322
323
324
325
326
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 321

def heading_2(text_info, color: "default")
  @type = __method__.to_s
  rich_text_array_and_color "rich_text", text_info, color
  @can_have_children = true
  self
end

#heading_3(text_info, color: "default") ⇒ NotionRubyMapping::Block



331
332
333
334
335
336
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 331

def heading_3(text_info, color: "default")
  @type = __method__.to_s
  rich_text_array_and_color "rich_text", text_info, color
  @can_have_children = true
  self
end

#image(url, caption: []) ⇒ Object



341
342
343
344
345
346
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 341

def image(url, caption: [])
  @type = __method__.to_s
  @file_object = FileObject.file_object url
  @caption = RichTextArray.rich_text_array "caption", caption
  self
end


352
353
354
355
356
357
358
359
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 352

def link_to_page(page_id: nil, database_id: nil)
  raise StandardError, "page_id or database_id is required." if page_id.nil? && database_id.nil?

  @type = __method__.to_s
  @page_id = page_id
  @database_id = database_id
  self
end

#numbered_list_item(text_info, sub_blocks: nil, color: "default") ⇒ NotionRubyMapping::Block



366
367
368
369
370
371
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 366

def numbered_list_item(text_info, sub_blocks: nil, color: "default")
  @type = __method__.to_s
  rich_text_array_and_color "rich_text", text_info, color
  add_sub_blocks sub_blocks
  self
end

#paragraph(text_info, sub_blocks: nil, color: "default") ⇒ NotionRubyMapping::Block



378
379
380
381
382
383
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 378

def paragraph(text_info, sub_blocks: nil, color: "default")
  @type = __method__.to_s
  rich_text_array_and_color "rich_text", text_info, color
  add_sub_blocks sub_blocks
  self
end

#pdf(url) ⇒ Object



387
388
389
390
391
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 387

def pdf(url)
  @type = __method__.to_s
  @file_object = FileObject.file_object url
  self
end

#quote(text_info, sub_blocks: nil, color: "default") ⇒ NotionRubyMapping::Block



398
399
400
401
402
403
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 398

def quote(text_info, sub_blocks: nil, color: "default")
  @type = __method__.to_s
  rich_text_array_and_color "rich_text", text_info, color
  add_sub_blocks sub_blocks
  self
end

#synced_block(sub_blocks: nil, block_id: nil) ⇒ Object



408
409
410
411
412
413
414
415
416
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 408

def synced_block(sub_blocks: nil, block_id: nil)
  raise StandardError, "blocks or block_id cannot set at the same time." if !sub_blocks.nil? && !block_id.nil?

  @type = __method__.to_s
  @sub_blocks = Array(sub_blocks) if sub_blocks
  @block_id = block_id
  @can_have_children = @block_id.nil?
  self
end

#table(table_width:, has_column_header: false, has_row_header: false, table_rows: nil) ⇒ Object



418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 418

def table(table_width:, has_column_header: false, has_row_header: false, table_rows: nil)
  @type = __method__.to_s
  @table_width = table_width
  @has_column_header = has_column_header
  @has_row_header = has_row_header
  if table_rows
    @table_rows = table_rows.map do |table_row|
      Block.new.table_row table_row, @table_width
    end
  end
  self
end

#table_of_contents(color: "default") ⇒ NotionRubyMapping::Block



434
435
436
437
438
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 434

def table_of_contents(color: "default")
  @type = __method__.to_s
  @color = color
  self
end

#table_row(array_array_of_text_objects, table_width) ⇒ NotionRubyMapping::Block

Raises:

  • (StandardError)


442
443
444
445
446
447
448
449
450
451
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 442

def table_row(array_array_of_text_objects, table_width)
  @type = __method__.to_s
  cc = array_array_of_text_objects.count
  raise StandardError, "table width must be #{table_width} (given array size is #{cc}" unless table_width == cc

  @cells = array_array_of_text_objects.map do |cell|
    Array(cell).map { |text_info| TextObject.text_object text_info }
  end
  self
end

#template(text_info, sub_blocks: nil) ⇒ NotionRubyMapping::Block



457
458
459
460
461
462
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 457

def template(text_info, sub_blocks: nil)
  @type = __method__.to_s
  rich_text_array_and_color "rich_text", text_info, nil
  add_sub_blocks sub_blocks
  self
end

#to_do(text_info, checked: false, sub_blocks: nil, color: "default") ⇒ NotionRubyMapping::Block



470
471
472
473
474
475
476
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 470

def to_do(text_info, checked: false, sub_blocks: nil, color: "default")
  @type = __method__.to_s
  rich_text_array_and_color "rich_text", text_info, color
  add_sub_blocks sub_blocks
  @checked = checked
  self
end

#toggle(text_info, checked: false, sub_blocks: nil, color: "default") ⇒ NotionRubyMapping::Block



483
484
485
486
487
488
489
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 483

def toggle(text_info, checked: false, sub_blocks: nil, color: "default")
  @type = __method__.to_s
  rich_text_array_and_color "rich_text", text_info, color
  @checked = checked
  add_sub_blocks sub_blocks
  self
end

#toggle_heading_1(text_info, sub_blocks:, color: "default") ⇒ NotionRubyMapping::Block

See Also:



496
497
498
499
500
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 496

def toggle_heading_1(text_info, sub_blocks:, color: "default")
  heading_1 text_info, color: color
  add_sub_blocks sub_blocks
  self
end

#toggle_heading_2(text_info, sub_blocks:, color: "default") ⇒ NotionRubyMapping::Block



506
507
508
509
510
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 506

def toggle_heading_2(text_info, sub_blocks:, color: "default")
  heading_2 text_info, color: color
  add_sub_blocks sub_blocks
  self
end

#toggle_heading_3(text_info, sub_blocks:, color: "default") ⇒ NotionRubyMapping::Block



516
517
518
519
520
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 516

def toggle_heading_3(text_info, sub_blocks:, color: "default")
  heading_3 text_info, color: color
  add_sub_blocks sub_blocks
  self
end

#video(url) ⇒ Object



525
526
527
528
529
# File 'lib/notion_ruby_mapping/blocks/block.rb', line 525

def video(url)
  @type = __method__.to_s
  @file_object = FileObject.file_object url
  self
end