Class: NotionRubyMapping::Base
- Inherits:
-
Object
- Object
- NotionRubyMapping::Base
- Defined in:
- lib/notion_ruby_mapping/blocks/base.rb
Overview
Notion Base object (Parent of Page / Database / List) The Public API method has a link to the API references.
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#json ⇒ Object
readonly
Returns the value of attribute json.
Class Method Summary collapse
- .create_from_json(json) ⇒ NotionRubyMapping::Base
- .dry_run_script(method, path, json = nil) ⇒ Object
Instance Method Summary collapse
-
#[](key) ⇒ NotionRubyMapping::PropertyCache, Hash
Obtained Page value or PropertyCache.
- #append_block_children(*blocks, dry_run: false) ⇒ NotionRubyMapping::Block, String
- #assert_parent_children_pair(block) ⇒ Object
-
#assign_property(klass, title) ⇒ NotionRubyMapping::Property
Generated property.
-
#block? ⇒ TrueClass, FalseClass
True if Block object.
- #children(query = Query.new, dry_run: false) ⇒ NotionRubyMapping::List, String
- #create_child_breadcrumb ⇒ NotionRubyMapping::Block
- #created_time ⇒ NotionRubyMapping::CreatedTimeProperty
-
#database? ⇒ TrueClass, FalseClass
True if Database object.
- #description ⇒ Object
-
#icon ⇒ Hash?
Obtained Hash.
-
#initialize(json: nil, id: nil, assign: [], parent: nil) ⇒ Base
constructor
A new instance of Base.
-
#json_properties ⇒ Hash
Json properties.
- #last_edited_time ⇒ NotionRubyMapping::LastEditedTimeProperty
-
#new_record? ⇒ Boolean
True if new record.
-
#page? ⇒ TrueClass, FalseClass
True if Page object.
-
#properties ⇒ NotionRubyMapping::PropertyCache
Get or created PropertyCache object.
-
#property_values_json ⇒ Hash
Created json for update page.
-
#reload ⇒ NotionRubyMapping::Base
Reloaded self.
- #restore_from_json ⇒ NotionRubyMapping::Base
- #save(dry_run: false) ⇒ NotionRubyMapping::Base, String
- #set_icon(emoji: nil, url: nil) ⇒ NotionRubyMapping::Base
- #update_json(json) ⇒ NotionRubyMapping::Base
Constructor Details
#initialize(json: nil, id: nil, assign: [], parent: nil) ⇒ Base
Returns a new instance of Base.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 11 def initialize(json: nil, id: nil, assign: [], parent: nil) @nc = NotionCache.instance @json = json @id = @nc.hex_id(id || json && @json["id"]) @new_record = true unless parent.nil? raise StandardError, "Unknown id" if !is_a?(List) && !is_a?(Block) && @id.nil? && parent.nil? @payload = Payload.new(!is_a?(Block) && parent && {"parent" => parent}) @property_cache = nil @created_time = nil @last_edited_time = nil return if assign.empty? assign.each_slice(2) { |(klass, key)| assign_property(klass, key) } @json ||= {} end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
27 28 29 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 27 def id @id end |
#json ⇒ Object (readonly)
Returns the value of attribute json.
27 28 29 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 27 def json @json end |
Class Method Details
.create_from_json(json) ⇒ NotionRubyMapping::Base
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 31 def self.create_from_json(json) case json["object"] when "page" Page.new json: json when "database" Database.new json: json when "list" List.new json: json when "block" Block.new(json: json).decode_block else raise StandardError, json.inspect end end |
.dry_run_script(method, path, json = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 49 def self.dry_run_script(method, path, json = nil) shell = [ "#!/bin/sh\ncurl #{method == :get ? "" : "-X #{method.to_s.upcase}"} 'https://api.notion.com/#{path}'", " -H 'Notion-Version: 2022-02-22'", " -H 'Authorization: Bearer '\"$NOTION_API_KEY\"''", ] shell << " -H 'Content-Type: application/json'" unless path == :get shell << " --data '#{JSON.generate json}'" if json shell.join(" \\\n") end |
Instance Method Details
#[](key) ⇒ NotionRubyMapping::PropertyCache, Hash
Returns obtained Page value or PropertyCache.
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 62 def [](key) unless @json raise StandardError, "Unknown id" if @id.nil? reload end case key when "properties" properties else @json[key] end end |
#append_block_children(*blocks, dry_run: false) ⇒ NotionRubyMapping::Block, String
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 81 def append_block_children(*blocks, dry_run: false) raise StandardError, "This block can have no children." unless page? || (block? && can_have_children) only_one = blocks.length == 1 json = { "children" => Array(blocks).map do |block| assert_parent_children_pair block block.block_json end } if dry_run path = @nc.append_block_children_page_path(id) self.class.dry_run_script :patch, path, json else response = @nc.append_block_children_request @id, json raise StandardError, response unless response["results"] answers = response["results"].map { |sub_json| Block.new json: sub_json } only_one ? answers.first : answers end end |
#assert_parent_children_pair(block) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 104 def assert_parent_children_pair(block) raise StandardError, "the argument is not a block." unless block.block? raise StandardError, "#{type} cannot have children." if block? && !@can_have_children return if block.can_append bt = block.type raise StandardError, "Internal file block can not append." if bt == "file" raise StandardError, "Column block can only append column_list block" unless bt == "column" && block? && self.type == "columu_list" end |
#assign_property(klass, title) ⇒ NotionRubyMapping::Property
Returns generated property.
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 121 def assign_property(klass, title) @property_cache ||= PropertyCache.new({}, base_type: database? ? :database : :page) property = if database? klass.new(title, will_update: new_record?, base_type: :database) else klass.new(title, will_update: true, base_type: :page) end @property_cache.add_property property property end |
#block? ⇒ TrueClass, FalseClass
Returns true if Block object.
133 134 135 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 133 def block? is_a? Block end |
#children(query = Query.new, dry_run: false) ⇒ NotionRubyMapping::List, String
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 140 def children(query = Query.new, dry_run: false) if dry_run path = @nc.block_children_page_path(id) + query.query_string self.class.dry_run_script :get, path elsif @children @children else response = @nc.block_children_request @id, query.query_string @children = List.new json: response, parent: self, query: query end end |
#create_child_breadcrumb ⇒ NotionRubyMapping::Block
153 154 155 156 157 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 153 def raise StandardError unless page? || (block? && can_have_children) Block.new(parent: self). end |
#created_time ⇒ NotionRubyMapping::CreatedTimeProperty
160 161 162 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 160 def created_time @created_time ||= CreatedTimeProperty.new("__timestamp__", json: self["created_time"]) end |
#database? ⇒ TrueClass, FalseClass
Returns true if Database object.
165 166 167 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 165 def database? is_a? Database end |
#description ⇒ Object
257 258 259 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 257 def description "#{self.class.name}-#{@id}" end |
#icon ⇒ Hash?
Returns obtained Hash.
171 172 173 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 171 def icon self["icon"] end |
#json_properties ⇒ Hash
Returns json properties.
176 177 178 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 176 def json_properties @json && @json["properties"] end |
#last_edited_time ⇒ NotionRubyMapping::LastEditedTimeProperty
181 182 183 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 181 def last_edited_time @last_edited_time ||= LastEditedTimeProperty.new("__timestamp__", json: self["last_edited_time"]) end |
#new_record? ⇒ Boolean
Returns true if new record.
187 188 189 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 187 def new_record? @new_record end |
#page? ⇒ TrueClass, FalseClass
Returns true if Page object.
192 193 194 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 192 def page? is_a? Page end |
#properties ⇒ NotionRubyMapping::PropertyCache
Returns get or created PropertyCache object.
198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 198 def properties unless @property_cache unless @json raise StandardError, "Unknown id" if @id.nil? reload end @property_cache = PropertyCache.new json_properties, base_type: database? ? :database : :page end @property_cache end |
#property_values_json ⇒ Hash
Returns created json for update page.
211 212 213 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 211 def property_values_json @payload.property_values_json @property_cache end |
#reload ⇒ NotionRubyMapping::Base
Returns reloaded self.
216 217 218 219 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 216 def reload update_json reload_json self end |
#restore_from_json ⇒ NotionRubyMapping::Base
222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 222 def restore_from_json return if (ps = @json["properties"]).nil? properties.json = json_properties return unless is_a?(Page) || is_a?(Database) ps.each do |key, json| properties[key].update_from_json json end self end |
#save(dry_run: false) ⇒ NotionRubyMapping::Base, String
236 237 238 239 240 241 242 243 244 245 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 236 def save(dry_run: false) if dry_run @new_record ? create(dry_run: true) : update(dry_run: true) else @new_record ? create : update @property_cache.clear_will_update @payload.clear self end end |
#set_icon(emoji: nil, url: nil) ⇒ NotionRubyMapping::Base
252 253 254 255 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 252 def set_icon(emoji: nil, url: nil) @payload.set_icon(emoji: emoji, url: url) if page? || database? self end |
#update_json(json) ⇒ NotionRubyMapping::Base
263 264 265 266 267 268 269 270 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 263 def update_json(json) raise StandardError, json.inspect unless json["object"] != "error" && (@json.nil? || @json["type"] == json["type"]) @json = json @id = @nc.hex_id(@json["id"]) restore_from_json self end |