Class: NotionRails::BaseBlock

Inherits:
Object
  • Object
show all
Includes:
Renderers
Defined in:
lib/notion_rails/base_block.rb

Constant Summary collapse

BLOCK_TYPES =
%w[
  paragraph
  heading_1
  heading_2
  heading_3
  bulleted_list_item
  numbered_list_item
  quote
  callout
  code
  image
  video
  table_of_contents
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Renderers

#annotation_to_css_class, #render_bulleted_list_item, #render_callout, #render_code, #render_date, #render_heading_1, #render_heading_2, #render_heading_3, #render_image, #render_list_items, #render_numbered_list_item, #render_paragraph, #render_quote, #render_title, #render_video, #text_renderer

Constructor Details

#initialize(data) ⇒ BaseBlock

Returns a new instance of BaseBlock.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/notion_rails/base_block.rb', line 39

def initialize(data)
  @id = data['id']
  @created_time = data['created_time']
  @last_edited_time = data['last_edited_time']
  # TODO: handle user object
  @created_by = data['created_by']
  @last_edited_by = data['last_edited_by']
  # TODO: handle page_id type
  @parent = data['parent']
  @archived = data['archived']
  @has_children = data['has_children']
  @children = []
  @siblings = []
  @type = data['type']
  @properties = data[@type]
end

Instance Attribute Details

#archivedObject (readonly)

TODO: validate object type is block



9
10
11
# File 'lib/notion_rails/base_block.rb', line 9

def archived
  @archived
end

#childrenObject

TODO: validate object type is block



9
10
11
# File 'lib/notion_rails/base_block.rb', line 9

def children
  @children
end

#created_byObject (readonly)

TODO: validate object type is block



9
10
11
# File 'lib/notion_rails/base_block.rb', line 9

def created_by
  @created_by
end

#created_timeObject (readonly)

TODO: validate object type is block



9
10
11
# File 'lib/notion_rails/base_block.rb', line 9

def created_time
  @created_time
end

#has_childrenObject (readonly)

TODO: validate object type is block



9
10
11
# File 'lib/notion_rails/base_block.rb', line 9

def has_children
  @has_children
end

#idObject (readonly)

TODO: validate object type is block



9
10
11
# File 'lib/notion_rails/base_block.rb', line 9

def id
  @id
end

#last_edited_byObject (readonly)

TODO: validate object type is block



9
10
11
# File 'lib/notion_rails/base_block.rb', line 9

def last_edited_by
  @last_edited_by
end

#last_edited_timeObject (readonly)

TODO: validate object type is block



9
10
11
# File 'lib/notion_rails/base_block.rb', line 9

def last_edited_time
  @last_edited_time
end

#parentObject (readonly)

TODO: validate object type is block



9
10
11
# File 'lib/notion_rails/base_block.rb', line 9

def parent
  @parent
end

#propertiesObject (readonly)

TODO: validate object type is block



9
10
11
# File 'lib/notion_rails/base_block.rb', line 9

def properties
  @properties
end

#siblingsObject

TODO: validate object type is block



9
10
11
# File 'lib/notion_rails/base_block.rb', line 9

def siblings
  @siblings
end

#typeObject (readonly)

TODO: validate object type is block



9
10
11
# File 'lib/notion_rails/base_block.rb', line 9

def type
  @type
end

Instance Method Details

#iconObject



81
82
83
84
# File 'lib/notion_rails/base_block.rb', line 81

def icon
  icon = @properties['icon']
  @properties['icon'][icon['type']] || []
end

#multi_mediaObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/notion_rails/base_block.rb', line 86

def multi_media
  case @properties['type']
  when 'file'
    [@properties.dig('file', 'url'), @properties.dig('file', 'expiry_time'), @properties['caption'], 'file']
  when 'external'
    [@properties.dig('external', 'url'), nil, @properties['caption'], 'external']
  else
    [nil, nil, @properties['caption'], nil]
  end
end

#render(options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/notion_rails/base_block.rb', line 56

def render(options = {})
  case @type
  when 'paragraph' then render_paragraph(rich_text, class: options[:paragraph])
  when 'heading_1' then render_heading_1(rich_text, class: options[:heading_1])
  when 'heading_2' then render_heading_2(rich_text, class: options[:heading_2])
  when 'heading_3' then render_heading_3(rich_text, class: options[:heading_3])
  when 'table_of_contents' then render_table_of_contents
  when 'bulleted_list_item'
    render_bulleted_list_item(rich_text, @siblings, @children, class: options[:bulleted_list_item])
  when 'numbered_list_item'
    render_numbered_list_item(rich_text, @siblings, @children, class: options[:numbered_list_item])
  when 'quote' then render_quote(rich_text, class: options[:quote])
  when 'callout' then render_callout(rich_text, icon, class: options[:callout])
  when 'code' then render_code(rich_text, class: options[:code], language: @properties['language'])
  when 'image' then render_image(*multi_media, class: options[:image])
  when 'video' then render_video(*multi_media, class: options[:video])
  else
    'Error'
  end
end

#rich_textObject



77
78
79
# File 'lib/notion_rails/base_block.rb', line 77

def rich_text
  @properties['rich_text'] || []
end