Class: NotionRubyMapping::Page

Inherits:
Base
  • Object
show all
Defined in:
lib/notion_ruby_mapping/blocks/page.rb

Overview

Notion page object

Instance Attribute Summary

Attributes inherited from Base

#has_children, #id, #in_trash, #json

Class Method Summary collapse

Instance Method Summary collapse

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, #initialize, #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

This class inherits a constructor from NotionRubyMapping::Base

Class Method Details

.find(id, dry_run: false) ⇒ NotionRubyMapping::Page, String

Parameters:

  • id (String)
  • dry_run (Boolean) (defaults to: false)

    true if dry_run

Returns:

See Also:



12
13
14
15
16
17
18
19
20
# File 'lib/notion_ruby_mapping/blocks/page.rb', line 12

def self.find(id, dry_run: false)
  nc = NotionCache.instance
  page_id = Base.page_id id
  if dry_run
    Base.dry_run_script :get, nc.page_path(page_id)
  else
    nc.page page_id
  end
end

Instance Method Details

#[](key) ⇒ NotionRubyMapping::PropertyCache, Hash

Returns obtained Page value or PropertyCache.

Parameters:

  • key (String)

Returns:



24
25
26
# File 'lib/notion_ruby_mapping/blocks/page.rb', line 24

def [](key)
  get key
end

#append_comment(text_objects, dry_run: false) ⇒ Object

Parameters:

  • dry_run (Boolean) (defaults to: false)

    true if dry_run



29
30
31
32
33
34
35
36
37
# File 'lib/notion_ruby_mapping/blocks/page.rb', line 29

def append_comment(text_objects, dry_run: false)
  rto = RichTextArray.new "rich_text", text_objects: text_objects, will_update: true
  json = rto.property_schema_json.merge({"parent" => {"page_id" => @id}})
  if dry_run
    self.class.dry_run_script :post, @nc.comments_path, json
  else
    CommentObject.new json: (@nc.append_comment_request json)
  end
end

#build_child_database(title, *assigns) {|db, db.properties| ... } ⇒ NotionRubyMapping::Database

Parameters:

  • title (String)
  • assigns (Array<String, Property>)

Yields:

Returns:

See Also:



43
44
45
46
47
48
# File 'lib/notion_ruby_mapping/blocks/page.rb', line 43

def build_child_database(title, *assigns)
  db = Database.new json: {"title" => [TextObject.new(title).property_values_json]},
                    assign: assigns, parent: {"type" => "page_id", "page_id" => @id}
  yield db, db.properties if block_given?
  db
end

#build_child_page(template_page: nil, position: nil, markdown: nil) {|page, pp| ... } ⇒ NotionRubyMapping::Base

Parameters:

  • template_page (String, NilClass) (defaults to: nil)
  • position (String, NilClass) (defaults to: nil)
  • markdown (String, NilClass) (defaults to: nil)

Yields:

  • (page, pp)

Returns:



66
67
68
69
70
71
72
73
# File 'lib/notion_ruby_mapping/blocks/page.rb', line 66

def build_child_page(template_page: nil, position: nil, markdown: nil)
  page = Page.new assign: [TitleProperty, "title"], parent: {"type" => "page_id", "page_id" => @id},
                  template_page: template_page, position: position, markdown: markdown
  pp = page.properties
  pp.clear_will_update
  yield page, pp if block_given?
  page
end

#create_child_database(title, *assigns, dry_run: false) {|db, db.properties| ... } ⇒ NotionRubyMapping::Database, String

Parameters:

  • title (String)
  • assigns (Array<String, Property>)
  • dry_run (Boolean) (defaults to: false)

    true if dry_run

Yields:

Returns:

See Also:



55
56
57
58
59
60
# File 'lib/notion_ruby_mapping/blocks/page.rb', line 55

def create_child_database(title, *assigns, dry_run: false)
  db = Database.new json: {"title" => [TextObject.new(title).property_values_json]},
                    assign: assigns, parent: {"type" => "page_id", "page_id" => @id}
  yield db, db.properties if block_given?
  db.save dry_run: dry_run
end

#create_child_page(template_page: nil, position: nil, markdown: nil, dry_run: false) {|page, pp| ... } ⇒ NotionRubyMapping::Base

Parameters:

  • dry_run (Boolean) (defaults to: false)

    true if dry_run

  • template_page (String, NilClass) (defaults to: nil)
  • position (String, NilClass) (defaults to: nil)
  • markdown (String, NilClass) (defaults to: nil)

Yields:

  • (page, pp)

Returns:



80
81
82
83
84
85
86
87
# File 'lib/notion_ruby_mapping/blocks/page.rb', line 80

def create_child_page(template_page: nil, position: nil, markdown: nil, dry_run: false)
  page = Page.new assign: [TitleProperty, "title"], parent: {"type" => "page_id", "page_id" => @id},
                  template_page: template_page, position: position, markdown: markdown
  pp = page.properties
  pp.clear_will_update
  yield page, pp if block_given?
  page.save dry_run: dry_run
end

#insert_markdown(markdown, after: nil, dry_run: false) ⇒ String, NotionRubyMapping::Base

Parameters:

  • markdown (String)
  • after (String, NilClass) (defaults to: nil)
  • dry_run (Boolean) (defaults to: false)

Returns:



93
94
95
96
97
98
99
100
101
# File 'lib/notion_ruby_mapping/blocks/page.rb', line 93

def insert_markdown(markdown, after: nil, dry_run: false)
  json = {"type" => "insert_content", "insert_content" => {"content" => markdown}}
  json["insert_content"]["after"] = after if after
  if dry_run
    self.class.dry_run_script :patch, @nc.markdown_page_path(@id), json
  else
    update_json @nc.markdown_page_request(@id, json)
  end
end

#move_to(page_or_data_source, dry_run: false) ⇒ NotionRubyMapping::Page, String

Parameters:

  • page_or_data_source (Page, DataSource)
  • dry_run (Boolean) (defaults to: false)

    true if dry_run

Returns:



106
107
108
109
110
111
112
113
114
# File 'lib/notion_ruby_mapping/blocks/page.rb', line 106

def move_to(page_or_data_source, dry_run: false)
  key = page_or_data_source.is_a?(Page) ? "page_id" : "data_source_id"
  json = {"parent" => {"type" => key, key => page_or_data_source.id}}
  if dry_run
    self.class.dry_run_script :post, @nc.move_page_path(@id), json
  else
    update_json @nc.move_page_request(@id, json)
  end
end

#public_urlString

Returns 公開URL.

Returns:

  • (String)

    公開URL



117
118
119
# File 'lib/notion_ruby_mapping/blocks/page.rb', line 117

def public_url
  @json["public_url"]
end

#replace_markdown(replace, replace_range, allow_deleting_content: nil, dry_run: false) ⇒ String, NotionRubyMapping::Base

Parameters:

  • replace (String)
  • replace_range (String)
  • dry_run (Boolean) (defaults to: false)

Returns:



125
126
127
128
129
130
131
132
133
134
# File 'lib/notion_ruby_mapping/blocks/page.rb', line 125

def replace_markdown(replace, replace_range, allow_deleting_content: nil, dry_run: false)
  json = {"type" => "replace_content_range",
          "replace_content_range" => {"content" => replace, "content_range" => replace_range}}
  json["replace_content_range"]["allow_deleting_content"] = true if allow_deleting_content
  if dry_run
    self.class.dry_run_script :patch, @nc.markdown_page_path(@id), json
  else
    update_json @nc.markdown_page_request(@id, json)
  end
end

#titleString

Returns title.



138
139
140
141
# File 'lib/notion_ruby_mapping/blocks/page.rb', line 138

def title
  tp = properties.select { |p| p.is_a?(TitleProperty) || (p.is_a?(Property) && p.property_id == :title) }
  tp.map(&:full_text).join ""
end

#urlString

Returns URL.

Returns:

  • (String)

    URL



144
145
146
# File 'lib/notion_ruby_mapping/blocks/page.rb', line 144

def url
  @json["url"]
end