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.
Direct Known Subclasses
Block, DataSource, Database, List, Page
Instance Attribute Summary collapse
-
#has_children ⇒ Object
readonly
Returns the value of attribute has_children.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#in_trash ⇒ Object
readonly
Returns the value of attribute in_trash.
-
#json ⇒ Object
readonly
Returns the value of attribute json.
Class Method Summary collapse
- .block_id(str) ⇒ Object
- .create_from_json(json) ⇒ NotionRubyMapping::Base
-
.data_source_id(str) ⇒ String
Data_source_id.
-
.database_id(str) ⇒ String
Database_id.
- .dry_run_script(method, path, json = nil) ⇒ Object
- .page_id(str) ⇒ Object
Instance Method Summary collapse
- #append_block_children(*blocks, position: nil, 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
- #comments(query = nil, dry_run: false) ⇒ Object
-
#cover ⇒ Hash?
Obtained Hash.
- #created_time ⇒ NotionRubyMapping::CreatedTimeProperty
-
#data_source? ⇒ TrueClass, FalseClass
True if Database object.
-
#database? ⇒ TrueClass, FalseClass
True if Database object.
-
#get(key) ⇒ NotionRubyMapping::PropertyCache, Hash
Obtained Page value or PropertyCache.
-
#icon ⇒ Hash?
Obtained Hash.
-
#initialize(json: nil, id: nil, assign: [], parent: nil, template_page: nil, position: nil, markdown: nil) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ String (frozen)
-
#json_properties ⇒ Hash
Json properties.
- #last_edited_time ⇒ NotionRubyMapping::LastEditedTimeProperty
- #markdown ⇒ String
-
#new_record? ⇒ Boolean
True if new record.
-
#page? ⇒ TrueClass, FalseClass
True if Page object.
- #parent(dry_run: false) ⇒ Object
- #parent_id ⇒ 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, ...
- #set_cover(url: nil, file_upload_object: nil) ⇒ NotionRubyMapping::Base
- #set_icon(emoji: nil, url: nil, file_upload_object: nil) ⇒ NotionRubyMapping::Base
-
#synced_block_original? ⇒ FalseClass
Return false except block.
- #update_json(json) ⇒ NotionRubyMapping::Base
Constructor Details
#initialize(json: nil, id: nil, assign: [], parent: nil, template_page: nil, position: nil, markdown: nil) ⇒ Base
Returns a new instance of Base.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 14 def initialize(json: nil, id: nil, assign: [], parent: nil, template_page: nil, position: nil, markdown: nil) @nc = NotionCache.instance @json = json @id = @nc.hex_id(id || @json && @json["id"]) @in_trash = @json && @json["in_trash"] @has_children = @json && @json["has_children"] @new_record = true unless parent.nil? raise StandardError, "Unknown id" if !is_a?(List) && !is_a?(Block) && @id.nil? && parent.nil? payload_json = {} payload_json["parent"] = parent if !is_a?(Block) && parent if template_page == "default" payload_json["template"] = {"type" => "default"} elsif template_page payload_json["template"] = {"type" => "template_id", "template_id" => template_page.id} end if %w[page_start page_end].include? position payload_json["position"] = {"type" => position} elsif position payload_json["position"] = {"type" => "after_block", "after_block" => {"id" => position}} end payload_json["markdown"] = markdown if markdown @payload = Payload.new(payload_json) @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
#has_children ⇒ Object (readonly)
Returns the value of attribute has_children.
45 46 47 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 45 def has_children @has_children end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
45 46 47 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 45 def id @id end |
#in_trash ⇒ Object (readonly)
Returns the value of attribute in_trash.
45 46 47 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 45 def in_trash @in_trash end |
#json ⇒ Object (readonly)
Returns the value of attribute json.
45 46 47 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 45 def json @json end |
Class Method Details
.block_id(str) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 66 def self.block_id(str) if /^http/.match str /#([\da-f]{32})/.match(str)[1] else NotionCache.instance.hex_id str end end |
.create_from_json(json) ⇒ NotionRubyMapping::Base
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 49 def self.create_from_json(json) case json["object"] when "page", "page_markdown" Page.new json: json when "data_source" DataSource.new json: json when "database" Database.new json: json when "list" List.new json: json when "block" Block.decode_block json else raise StandardError, json.inspect end end |
.data_source_id(str) ⇒ String
Returns data_source_id.
76 77 78 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 76 def self.data_source_id(str) NotionCache.instance.hex_id str end |
.database_id(str) ⇒ String
Returns database_id.
82 83 84 85 86 87 88 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 82 def self.database_id(str) if /^http/.match str /([\da-f]{32})\?/.match(str)[1] else NotionCache.instance.hex_id str end end |
.dry_run_script(method, path, json = nil) ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 93 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: #{NotionRubyMapping::NOTION_VERSION}'", " -H 'Authorization: Bearer '\"$NOTION_API_KEY\"''", ] shell << " -H 'Content-Type: application/json'" if i[post patch].include?(method) shell << " --data '#{JSON.generate json}'" if json shell.join(" \\\n") end |
.page_id(str) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 104 def self.page_id(str) if /^http/.match str /([\da-f]{32})(|\?.*)$/.match(str)[1] else NotionCache.instance.hex_id str end end |
Instance Method Details
#append_block_children(*blocks, position: nil, dry_run: false) ⇒ NotionRubyMapping::Block, String
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 153 def append_block_children(*blocks, position: nil, 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 %w[start end].include? position json["position"] = {"type" => position} elsif position json["position"] = {"type" => "after_block", "after_block" => {"id" => position}} 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.create_from_json sub_json } only_one ? answers.first : answers end end |
#assert_parent_children_pair(block) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 181 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? && type == :column_list end |
#assign_property(klass, title) ⇒ NotionRubyMapping::Property
Returns generated property.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 198 def assign_property(klass, title) @property_cache ||= PropertyCache.new({}, base_type: if database? "database" else data_source? ? "data_source" : "page" end, page_id: page? ? @id : nil) property = if database? klass.new(title, will_update: new_record?, base_type: "database") elsif data_source? klass.new(title, will_update: new_record?, base_type: "data_source") else klass.new(title, will_update: true, base_type: "page", property_cache: @property_cache) end @property_cache.add_property property property end |
#block? ⇒ TrueClass, FalseClass
Returns true if Block object.
218 219 220 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 218 def block? is_a? Block end |
#children(query = Query.new, dry_run: false) ⇒ NotionRubyMapping::List, String
225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 225 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, type: "parent", value: self, query: query end end |
#comments(query = nil, dry_run: false) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 113 def comments(query = nil, dry_run: false) return unless page? || block? if dry_run self.class.dry_run_script :get, @nc.retrieve_comments_path(@id) else ans = {} List.new(type: "comment_parent", value: self, json: @nc.retrieve_comments_request(@id, query), query: query).each do |comment| dt_id = comment.discussion_id dt = ans[dt_id] ||= DiscussionThread.new(dt_id) dt.comments << comment end ans end end |
#cover ⇒ Hash?
Returns obtained Hash.
238 239 240 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 238 def cover self["cover"] end |
#created_time ⇒ NotionRubyMapping::CreatedTimeProperty
243 244 245 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 243 def created_time @created_time ||= CreatedTimeProperty.new("__timestamp__", json: self["created_time"]) end |
#data_source? ⇒ TrueClass, FalseClass
Returns true if Database object.
253 254 255 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 253 def data_source? is_a? DataSource end |
#database? ⇒ TrueClass, FalseClass
Returns true if Database object.
248 249 250 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 248 def database? is_a? Database end |
#get(key) ⇒ NotionRubyMapping::PropertyCache, Hash
Returns obtained Page value or PropertyCache.
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 133 def get(key) unless @json raise StandardError, "Unknown id" if @id.nil? reload end case key when "properties" properties else @json[key] end end |
#icon ⇒ Hash?
Returns obtained Hash.
259 260 261 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 259 def icon self["icon"] end |
#inspect ⇒ String (frozen)
264 265 266 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 264 def inspect "#{self.class.name}-#{@id}" end |
#json_properties ⇒ Hash
Returns json properties.
269 270 271 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 269 def json_properties @json && @json["properties"] end |
#last_edited_time ⇒ NotionRubyMapping::LastEditedTimeProperty
274 275 276 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 274 def last_edited_time @last_edited_time ||= LastEditedTimeProperty.new("__timestamp__", json: self["last_edited_time"]) end |
#markdown ⇒ String
279 280 281 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 279 def markdown @json && @json["markdown"] end |
#new_record? ⇒ Boolean
Returns true if new record.
285 286 287 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 285 def new_record? @new_record end |
#page? ⇒ TrueClass, FalseClass
Returns true if Page object.
290 291 292 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 290 def page? is_a? Page end |
#parent(dry_run: false) ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 295 def parent(dry_run: false) parent_json = @json && @json["parent"] raise StandardError, "Unknown parent" if parent_json.nil? type = parent_json["type"] klass = case type when "data_source_id" DataSource when "database_id" Database when "page_id" Page when "block_id" Block else raise StandardError, "List does not have any parent" end klass.find parent_json[type], dry_run: dry_run end |
#parent_id ⇒ Object
315 316 317 318 319 320 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 315 def parent_id parent_json = @json && @json["parent"] raise StandardError, "Unknown parent" if parent_json.nil? parent_json[parent_json["type"]] end |
#properties ⇒ NotionRubyMapping::PropertyCache
Returns get or created PropertyCache object.
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 324 def properties unless @property_cache unless @json raise StandardError, "Unknown id" if @id.nil? reload end @property_cache = PropertyCache.new json_properties, base_type: if database? "database" else data_source? ? "data_source" : "page" end, page_id: page? ? @id : nil end @property_cache end |
#property_values_json ⇒ Hash
Returns created json for update page.
343 344 345 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 343 def property_values_json @payload.property_values_json @property_cache end |
#reload ⇒ NotionRubyMapping::Base
Returns reloaded self.
348 349 350 351 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 348 def reload update_json reload_json self end |
#restore_from_json ⇒ NotionRubyMapping::Base
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 354 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| if json["type"] properties[key].update_from_json json else properties[key]&.clear_will_update end end self end |
#save(dry_run: false) ⇒ NotionRubyMapping::Base, ...
373 374 375 376 377 378 379 380 381 382 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 373 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 if page? @payload.clear self end end |
#set_cover(url: nil, file_upload_object: nil) ⇒ NotionRubyMapping::Base
387 388 389 390 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 387 def set_cover(url: nil, file_upload_object: nil) @payload.set_cover(url: url, file_upload_object: file_upload_object) if page? || database? self end |
#set_icon(emoji: nil, url: nil, file_upload_object: nil) ⇒ NotionRubyMapping::Base
398 399 400 401 402 403 404 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 398 def set_icon(emoji: nil, url: nil, file_upload_object: nil) if page? || database? || data_source? @payload.set_icon(emoji: emoji, url: url, file_upload_object: file_upload_object) end self end |
#synced_block_original? ⇒ FalseClass
Return false except block
407 408 409 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 407 def synced_block_original? false end |
#update_json(json) ⇒ NotionRubyMapping::Base
413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 413 def update_json(json) unless json["object"] != "error" && (@json.nil? || @json["type"] == json["type"]) raise StandardError, json.inspect end @json = json @id = @nc.hex_id(@json["id"]) restore_from_json self end |