Class: NotionRubyMapping::Base
- Inherits:
-
Object
- Object
- NotionRubyMapping::Base
show all
- 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
Class Method Summary
collapse
Instance Method Summary
collapse
-
#append_block_children(*blocks, dry_run: false) ⇒ NotionRubyMapping::Block, String
-
#assert_parent_children_pair(block) ⇒ Object
-
#assign_property(klass, title) ⇒ NotionRubyMapping::Property
-
#block? ⇒ TrueClass, FalseClass
-
#children(query = Query.new, dry_run: false) ⇒ NotionRubyMapping::List, String
-
#comments(query = nil, dry_run: false) ⇒ Object
-
#created_time ⇒ NotionRubyMapping::CreatedTimeProperty
-
#database? ⇒ TrueClass, FalseClass
-
#get(key) ⇒ NotionRubyMapping::PropertyCache, Hash
Obtained Page value or PropertyCache.
-
#icon ⇒ Hash?
-
#initialize(json: nil, id: nil, assign: [], parent: nil) ⇒ Base
constructor
-
#inspect ⇒ String (frozen)
-
#json_properties ⇒ Hash
-
#last_edited_time ⇒ NotionRubyMapping::LastEditedTimeProperty
-
#new_record? ⇒ Boolean
-
#page? ⇒ TrueClass, FalseClass
-
#parent(dry_run: false) ⇒ Object
-
#properties ⇒ NotionRubyMapping::PropertyCache
Get or created PropertyCache object.
-
#property_values_json ⇒ Hash
Created json for update page.
-
#reload ⇒ NotionRubyMapping::Base
-
#restore_from_json ⇒ NotionRubyMapping::Base
-
#save(dry_run: false) ⇒ NotionRubyMapping::Base, ...
-
#set_icon(emoji: nil, url: 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) ⇒ Base
Returns a new instance of Base.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 10
def initialize(json: nil, id: nil, assign: [], parent: nil)
@nc = NotionCache.instance
@json = json
@id = @nc.hex_id(id || @json && @json["id"])
@archived = @json && @json["archived"]
@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 = 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
#archived ⇒ Object
Returns the value of attribute archived.
28
29
30
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 28
def archived
@archived
end
|
#has_children ⇒ Object
Returns the value of attribute has_children.
28
29
30
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 28
def has_children
@has_children
end
|
#id ⇒ Object
Returns the value of attribute id.
28
29
30
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 28
def id
@id
end
|
#json ⇒ Object
Returns the value of attribute json.
28
29
30
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 28
def json
@json
end
|
Class Method Details
.block_id(str) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 47
def self.block_id(str)
if /^http/.match str
/#([\da-f]{32})/.match(str)[1]
else
NotionCache.instance.hex_id str
end
end
|
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 32
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.decode_block json
else
raise StandardError, json.inspect
end
end
|
.database_id(str) ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 55
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
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 66
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-06-28'",
" -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
77
78
79
80
81
82
83
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 77
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, dry_run: false) ⇒ NotionRubyMapping::Block, String
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 125
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.create_from_json sub_json }
only_one ? answers.first : answers
end
end
|
#assert_parent_children_pair(block) ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 148
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
|
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 165
def assign_property(klass, title)
@property_cache ||= PropertyCache.new({},
base_type: database? ? :database : :page,
page_id: page? ? @id : nil)
property = if database?
klass.new(title, will_update: new_record?, base_type: :database)
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
179
180
181
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 179
def block?
is_a? Block
end
|
#children(query = Query.new, dry_run: false) ⇒ NotionRubyMapping::List, String
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 186
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
|
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 86
def (query = nil, dry_run: false)
return unless page? || block?
if dry_run
self.class.dry_run_script :get, @nc.(@id)
else
ans = {}
List.new(type: :comment_parent, value: self,
json: @nc.(@id, query),
query: query).each do ||
dt_id = .discussion_id
dt = ans[dt_id] ||= DiscussionThread.new dt_id
dt. <<
end
ans
end
end
|
199
200
201
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 199
def created_time
@created_time ||= CreatedTimeProperty.new("__timestamp__", json: self["created_time"])
end
|
#database? ⇒ TrueClass, FalseClass
204
205
206
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 204
def database?
is_a? Database
end
|
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 106
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?
210
211
212
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 210
def icon
self["icon"]
end
|
#inspect ⇒ String (frozen)
215
216
217
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 215
def inspect
"#{self.class.name}-#{@id}"
end
|
#json_properties ⇒ Hash
220
221
222
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 220
def json_properties
@json && @json["properties"]
end
|
225
226
227
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 225
def last_edited_time
@last_edited_time ||= LastEditedTimeProperty.new("__timestamp__", json: self["last_edited_time"])
end
|
#new_record? ⇒ Boolean
Returns true if new record.
231
232
233
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 231
def new_record?
@new_record
end
|
#page? ⇒ TrueClass, FalseClass
236
237
238
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 236
def page?
is_a? Page
end
|
#parent(dry_run: false) ⇒ Object
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 241
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 "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
|
Returns get or created PropertyCache object.
261
262
263
264
265
266
267
268
269
270
271
272
273
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 261
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,
page_id: page? ? @id : nil
end
@property_cache
end
|
#property_values_json ⇒ Hash
276
277
278
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 276
def property_values_json
@payload.property_values_json @property_cache
end
|
281
282
283
284
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 281
def reload
update_json reload_json
self
end
|
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 287
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
|
306
307
308
309
310
311
312
313
314
315
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 306
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
|
322
323
324
325
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 322
def set_icon(emoji: nil, url: nil)
@payload.set_icon(emoji: emoji, url: url) if page? || database?
self
end
|
#synced_block_original? ⇒ FalseClass
328
329
330
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 328
def synced_block_original?
false
end
|
334
335
336
337
338
339
340
341
342
343
344
|
# File 'lib/notion_ruby_mapping/blocks/base.rb', line 334
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
|