notion_ruby_mapping 0.3.2 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -1224
- data/env.yml.sample +0 -13
- data/lib/notion_ruby_mapping/{base.rb → blocks/base.rb} +166 -90
- data/lib/notion_ruby_mapping/blocks/block.rb +565 -0
- data/lib/notion_ruby_mapping/{database.rb → blocks/database.rb} +40 -9
- data/lib/notion_ruby_mapping/{list.rb → blocks/list.rb} +35 -3
- data/lib/notion_ruby_mapping/blocks/page.rb +71 -0
- data/lib/notion_ruby_mapping/{notion_cache.rb → controllers/notion_cache.rb} +134 -98
- data/lib/notion_ruby_mapping/{payload.rb → controllers/payload.rb} +0 -0
- data/lib/notion_ruby_mapping/{property_cache.rb → controllers/property_cache.rb} +21 -20
- data/lib/notion_ruby_mapping/{query.rb → controllers/query.rb} +26 -18
- data/lib/notion_ruby_mapping/{rich_text_array.rb → controllers/rich_text_array.rb} +26 -15
- data/lib/notion_ruby_mapping/objects/emoji_object.rb +40 -0
- data/lib/notion_ruby_mapping/objects/equation_object.rb +43 -0
- data/lib/notion_ruby_mapping/objects/file_object.rb +60 -0
- data/lib/notion_ruby_mapping/{mention_object.rb → objects/mention_object.rb} +11 -0
- data/lib/notion_ruby_mapping/{rich_text_object.rb → objects/rich_text_object.rb} +11 -0
- data/lib/notion_ruby_mapping/{text_object.rb → objects/text_object.rb} +0 -1
- data/lib/notion_ruby_mapping/{user_object.rb → objects/user_object.rb} +9 -4
- data/lib/notion_ruby_mapping/{checkbox_property.rb → properties/checkbox_property.rb} +3 -0
- data/lib/notion_ruby_mapping/{created_by_property.rb → properties/created_by_property.rb} +0 -0
- data/lib/notion_ruby_mapping/{created_time_property.rb → properties/created_time_property.rb} +1 -0
- data/lib/notion_ruby_mapping/properties/date_base_property.rb +122 -0
- data/lib/notion_ruby_mapping/{date_property.rb → properties/date_property.rb} +13 -0
- data/lib/notion_ruby_mapping/{email_property.rb → properties/email_property.rb} +1 -0
- data/lib/notion_ruby_mapping/{files_property.rb → properties/files_property.rb} +26 -13
- data/lib/notion_ruby_mapping/{formula_property.rb → properties/formula_property.rb} +4 -0
- data/lib/notion_ruby_mapping/{last_edited_by_property.rb → properties/last_edited_by_property.rb} +1 -0
- data/lib/notion_ruby_mapping/{last_edited_time_property.rb → properties/last_edited_time_property.rb} +1 -0
- data/lib/notion_ruby_mapping/{multi_property.rb → properties/multi_property.rb} +0 -0
- data/lib/notion_ruby_mapping/{multi_select_property.rb → properties/multi_select_property.rb} +2 -3
- data/lib/notion_ruby_mapping/{number_property.rb → properties/number_property.rb} +4 -0
- data/lib/notion_ruby_mapping/{people_property.rb → properties/people_property.rb} +8 -3
- data/lib/notion_ruby_mapping/{phone_number_property.rb → properties/phone_number_property.rb} +4 -0
- data/lib/notion_ruby_mapping/{property.rb → properties/property.rb} +33 -0
- data/lib/notion_ruby_mapping/{relation_property.rb → properties/relation_property.rb} +30 -10
- data/lib/notion_ruby_mapping/{rich_text_property.rb → properties/rich_text_property.rb} +0 -0
- data/lib/notion_ruby_mapping/{rollup_property.rb → properties/rollup_property.rb} +7 -0
- data/lib/notion_ruby_mapping/{select_property.rb → properties/select_property.rb} +6 -1
- data/lib/notion_ruby_mapping/{text_property.rb → properties/text_property.rb} +0 -0
- data/lib/notion_ruby_mapping/{title_property.rb → properties/title_property.rb} +0 -0
- data/lib/notion_ruby_mapping/{url_property.rb → properties/url_property.rb} +5 -0
- data/lib/notion_ruby_mapping/version.rb +1 -1
- data/lib/notion_ruby_mapping.rb +14 -7
- metadata +43 -40
- data/lib/notion_ruby_mapping/block.rb +0 -10
- data/lib/notion_ruby_mapping/date_base_property.rb +0 -75
- data/lib/notion_ruby_mapping/page.rb +0 -50
data/env.yml.sample
CHANGED
@@ -1,15 +1,2 @@
|
|
1
1
|
---
|
2
2
|
notion_token: "secret_XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
3
|
-
top_page: "c01166c613ae45cbb96818b4ef2f5a77"
|
4
|
-
unpermitted_page: "4a6eb31244204fecb488da11f3caf871"
|
5
|
-
database: "c37a2c66e3aa4a0da44773de3b80c253"
|
6
|
-
unpermitted_database: "668d797c76fa49349b05ad288df2d136"
|
7
|
-
h1block: "0250fb6d600142eca4c74efb8794fc6b"
|
8
|
-
unpermitted_block: "0c940186ab704351bb342d16f0635d49"
|
9
|
-
db_first_page: "dcdc805c85fa4155a55c20fc28771af7"
|
10
|
-
db_second_page: "6601e719a39a460c908e8909467fcccf"
|
11
|
-
db_update_page: "206ffaa277744a99baf593e28730240c"
|
12
|
-
user_hkob: "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
|
13
|
-
parent1_page: "860753bb6d1f48de96211fa6e0e31f82"
|
14
|
-
parent_database: "1d6b1040a9fb48d99a3d041429816e9f"
|
15
|
-
created_database: "c7697137d49f49c2bbcdd6a665c4f921"
|
@@ -2,9 +2,8 @@
|
|
2
2
|
|
3
3
|
module NotionRubyMapping
|
4
4
|
# Notion Base object (Parent of Page / Database / List)
|
5
|
+
# The Public API method has a link to the API references.
|
5
6
|
class Base
|
6
|
-
#### Public announced methods
|
7
|
-
|
8
7
|
# @param [Hash, nil] json
|
9
8
|
# @param [String, nil] id
|
10
9
|
# @param [Array<Property, Class, String>] assign
|
@@ -13,16 +12,50 @@ module NotionRubyMapping
|
|
13
12
|
@json = json
|
14
13
|
@id = @nc.hex_id(id || json && @json["id"])
|
15
14
|
@new_record = true unless parent.nil?
|
16
|
-
raise StandardError, "Unknown id" if !is_a?(List) && @id.nil? && parent.nil?
|
15
|
+
raise StandardError, "Unknown id" if !is_a?(List) && !is_a?(Block) && @id.nil? && parent.nil?
|
17
16
|
|
18
|
-
@payload = Payload.new(parent && {"parent" => parent})
|
17
|
+
@payload = Payload.new(!is_a?(Block) && parent && {"parent" => parent})
|
19
18
|
@property_cache = nil
|
20
19
|
@created_time = nil
|
21
20
|
@last_edited_time = nil
|
21
|
+
return if assign.empty?
|
22
|
+
|
22
23
|
assign.each_slice(2) { |(klass, key)| assign_property(klass, key) }
|
24
|
+
@json ||= {}
|
23
25
|
end
|
24
26
|
attr_reader :json, :id
|
25
27
|
|
28
|
+
# @param [Hash, Notion::Messages] json
|
29
|
+
# @return [NotionRubyMapping::Base]
|
30
|
+
def self.create_from_json(json)
|
31
|
+
case json["object"]
|
32
|
+
when "page"
|
33
|
+
Page.new json: json
|
34
|
+
when "database"
|
35
|
+
Database.new json: json
|
36
|
+
when "list"
|
37
|
+
List.new json: json
|
38
|
+
when "block"
|
39
|
+
Block.new(json: json).decode_block
|
40
|
+
else
|
41
|
+
raise StandardError, json.inspect
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# @param [Object] method
|
46
|
+
# @param [Object] path
|
47
|
+
# @param [nil] json
|
48
|
+
def self.dry_run_script(method, path, json = nil)
|
49
|
+
shell = [
|
50
|
+
"#!/bin/sh\ncurl #{method == :get ? "" : "-X #{method.to_s.upcase}"} 'https://api.notion.com/#{path}'",
|
51
|
+
" -H 'Notion-Version: 2022-02-22'",
|
52
|
+
" -H 'Authorization: Bearer '\"$NOTION_API_KEY\"''",
|
53
|
+
]
|
54
|
+
shell << " -H 'Content-Type: application/json'" if %i[post patch].include?(method)
|
55
|
+
shell << " --data '#{JSON.generate json}'" if json
|
56
|
+
shell.join(" \\\n")
|
57
|
+
end
|
58
|
+
|
26
59
|
# @param [String] key
|
27
60
|
# @return [NotionRubyMapping::PropertyCache, Hash] obtained Page value or PropertyCache
|
28
61
|
def [](key)
|
@@ -39,9 +72,92 @@ module NotionRubyMapping
|
|
39
72
|
end
|
40
73
|
end
|
41
74
|
|
75
|
+
# @param [Array<Block>] blocks
|
76
|
+
# @param [Boolean] dry_run
|
77
|
+
# @return [NotionRubyMapping::Block, String]
|
78
|
+
# @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#44bbf83d852c419485c5efe9fe1558fb
|
79
|
+
# @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#2c47f7fedae543cf8566389ba1677132
|
80
|
+
def append_block_children(*blocks, dry_run: false)
|
81
|
+
raise StandardError, "This block can have no children." unless page? || (block? && can_have_children)
|
82
|
+
|
83
|
+
only_one = blocks.length == 1
|
84
|
+
json = {
|
85
|
+
"children" => Array(blocks).map do |block|
|
86
|
+
assert_parent_children_pair block
|
87
|
+
block.block_json
|
88
|
+
end,
|
89
|
+
}
|
90
|
+
if dry_run
|
91
|
+
path = @nc.append_block_children_page_path(id)
|
92
|
+
self.class.dry_run_script :patch, path, json
|
93
|
+
else
|
94
|
+
response = @nc.append_block_children_request @id, json
|
95
|
+
raise StandardError, response unless response["results"]
|
96
|
+
|
97
|
+
answers = response["results"].map { |sub_json| Block.new json: sub_json }
|
98
|
+
only_one ? answers.first : answers
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# @param [NotionRubyMapping::Block] block
|
103
|
+
def assert_parent_children_pair(block)
|
104
|
+
raise StandardError, "the argument is not a block." unless block.block?
|
105
|
+
|
106
|
+
raise StandardError, "#{type} cannot have children." if block? && !@can_have_children
|
107
|
+
|
108
|
+
return if block.can_append
|
109
|
+
|
110
|
+
bt = block.type
|
111
|
+
raise StandardError, "Internal file block can not append." if bt == "file"
|
112
|
+
|
113
|
+
raise StandardError, "Column block can only append column_list block" unless bt == "column" &&
|
114
|
+
block? && type == "columu_list"
|
115
|
+
end
|
116
|
+
|
117
|
+
# @param [NotionRubyMapping::Property] klass
|
118
|
+
# @param [String] title
|
119
|
+
# @return [NotionRubyMapping::Property] generated property
|
120
|
+
def assign_property(klass, title)
|
121
|
+
@property_cache ||= PropertyCache.new({}, base_type: database? ? :database : :page)
|
122
|
+
property = if database?
|
123
|
+
klass.new(title, will_update: new_record?, base_type: :database)
|
124
|
+
else
|
125
|
+
klass.new(title, will_update: true, base_type: :page)
|
126
|
+
end
|
127
|
+
@property_cache.add_property property
|
128
|
+
property
|
129
|
+
end
|
130
|
+
|
131
|
+
# @return [TrueClass, FalseClass] true if Block object
|
132
|
+
def block?
|
133
|
+
is_a? Block
|
134
|
+
end
|
135
|
+
|
136
|
+
# @param [NotionRubyMapping::Query] query
|
137
|
+
# @param [Boolean] dry_run
|
138
|
+
# @return [NotionRubyMapping::List, String]
|
139
|
+
def children(query = Query.new, dry_run: false)
|
140
|
+
if dry_run
|
141
|
+
path = @nc.block_children_page_path(id) + query.query_string
|
142
|
+
self.class.dry_run_script :get, path
|
143
|
+
elsif @children
|
144
|
+
@children
|
145
|
+
else
|
146
|
+
response = @nc.block_children_request @id, query.query_string
|
147
|
+
@children = List.new json: response, parent: self, query: query
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
# @return [NotionRubyMapping::Block]
|
152
|
+
def create_child_breadcrumb
|
153
|
+
raise StandardError unless page? || (block? && can_have_children)
|
154
|
+
|
155
|
+
Block.new(parent: self).breadcrumb
|
156
|
+
end
|
157
|
+
|
42
158
|
# @return [NotionRubyMapping::CreatedTimeProperty]
|
43
159
|
def created_time
|
44
|
-
@created_time ||= CreatedTimeProperty.new("__timestamp__")
|
160
|
+
@created_time ||= CreatedTimeProperty.new("__timestamp__", json: self["created_time"])
|
45
161
|
end
|
46
162
|
|
47
163
|
# @return [TrueClass, FalseClass] true if Database object
|
@@ -50,16 +166,28 @@ module NotionRubyMapping
|
|
50
166
|
end
|
51
167
|
|
52
168
|
# @return [Hash, nil] obtained Hash
|
169
|
+
# @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#e13d526bd709451e9b06fd32e8d07fcd
|
53
170
|
def icon
|
54
171
|
self["icon"]
|
55
172
|
end
|
56
173
|
|
174
|
+
# @return [String (frozen)]
|
175
|
+
def inspect
|
176
|
+
"#{self.class.name}-#{@id}"
|
177
|
+
end
|
178
|
+
|
179
|
+
# @return [Hash] json properties
|
180
|
+
def json_properties
|
181
|
+
@json && @json["properties"]
|
182
|
+
end
|
183
|
+
|
57
184
|
# @return [NotionRubyMapping::LastEditedTimeProperty]
|
58
185
|
def last_edited_time
|
59
|
-
@last_edited_time ||= LastEditedTimeProperty.new("__timestamp__")
|
186
|
+
@last_edited_time ||= LastEditedTimeProperty.new("__timestamp__", json: self["last_edited_time"])
|
60
187
|
end
|
61
188
|
|
62
189
|
# @return [Boolean] true if new record
|
190
|
+
# @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#307af6e40d3840c59f8a82513a572d51
|
63
191
|
def new_record?
|
64
192
|
@new_record
|
65
193
|
end
|
@@ -70,6 +198,7 @@ module NotionRubyMapping
|
|
70
198
|
end
|
71
199
|
|
72
200
|
# @return [NotionRubyMapping::PropertyCache] get or created PropertyCache object
|
201
|
+
# @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#8f0b28e09dd74e2a9ff06126c48d64d4
|
73
202
|
def properties
|
74
203
|
unless @property_cache
|
75
204
|
unless @json
|
@@ -82,70 +211,6 @@ module NotionRubyMapping
|
|
82
211
|
@property_cache
|
83
212
|
end
|
84
213
|
|
85
|
-
# @return [NotionRubyMapping::Base, String]
|
86
|
-
def save(dry_run: false)
|
87
|
-
if dry_run
|
88
|
-
@new_record ? create(dry_run: true) : update(dry_run: true)
|
89
|
-
else
|
90
|
-
@new_record ? create : update
|
91
|
-
@property_cache.clear_will_update
|
92
|
-
@payload.clear
|
93
|
-
self
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
# @param [String] emoji
|
98
|
-
# @param [String] url
|
99
|
-
# @return [NotionRubyMapping::Base]
|
100
|
-
def set_icon(emoji: nil, url: nil)
|
101
|
-
@payload.set_icon(emoji: emoji, url: url) if page? || database?
|
102
|
-
self
|
103
|
-
end
|
104
|
-
|
105
|
-
# @return [String] title
|
106
|
-
def title
|
107
|
-
properties.select { |p| p.is_a? TitleProperty }.map(&:full_text).join ""
|
108
|
-
end
|
109
|
-
|
110
|
-
### Not public announced methods
|
111
|
-
|
112
|
-
# @param [Hash, Notion::Messages] json
|
113
|
-
# @return [NotionRubyMapping::Base]
|
114
|
-
def self.create_from_json(json)
|
115
|
-
case json["object"]
|
116
|
-
when "page"
|
117
|
-
Page.new json: json
|
118
|
-
when "database"
|
119
|
-
Database.new json: json
|
120
|
-
when "list"
|
121
|
-
List.new json: json
|
122
|
-
when "block"
|
123
|
-
Block.new json: json
|
124
|
-
else
|
125
|
-
raise StandardError, json.inspect
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
# @param [NotionRubyMapping::Property] klass
|
130
|
-
# @param [String] title
|
131
|
-
# @return [NotionRubyMapping::Property] generated property
|
132
|
-
def assign_property(klass, title)
|
133
|
-
@property_cache ||= PropertyCache.new({}, base_type: database? ? :database : :page)
|
134
|
-
|
135
|
-
property = if database?
|
136
|
-
klass.new(title, will_update: new_record?, base_type: :database)
|
137
|
-
else
|
138
|
-
klass.new(title, will_update: true, base_type: :page)
|
139
|
-
end
|
140
|
-
@property_cache.add_property property
|
141
|
-
property
|
142
|
-
end
|
143
|
-
|
144
|
-
# @return [NotionRubyMapping::List]
|
145
|
-
def children
|
146
|
-
@children ||= @nc.block_children(id)
|
147
|
-
end
|
148
|
-
|
149
214
|
# @return [Hash] created json for update page
|
150
215
|
def property_values_json
|
151
216
|
@payload.property_values_json @property_cache
|
@@ -170,10 +235,39 @@ module NotionRubyMapping
|
|
170
235
|
self
|
171
236
|
end
|
172
237
|
|
238
|
+
# @param [Boolean] dry_run
|
239
|
+
# @return [NotionRubyMapping::Base, String]
|
240
|
+
# @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#277085c8439841c798a4b94eae9a7326
|
241
|
+
def save(dry_run: false)
|
242
|
+
if dry_run
|
243
|
+
@new_record ? create(dry_run: true) : update(dry_run: true)
|
244
|
+
else
|
245
|
+
@new_record ? create : update
|
246
|
+
@property_cache.clear_will_update
|
247
|
+
@payload.clear
|
248
|
+
self
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
# @param [String] emoji
|
253
|
+
# @param [String] url
|
254
|
+
# @return [NotionRubyMapping::Base]
|
255
|
+
# @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#62eea67af7824496820c6bb903503540
|
256
|
+
# @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#e13d526bd709451e9b06fd32e8d07fcd
|
257
|
+
def set_icon(emoji: nil, url: nil)
|
258
|
+
@payload.set_icon(emoji: emoji, url: url) if page? || database?
|
259
|
+
self
|
260
|
+
end
|
261
|
+
|
262
|
+
# @return [FalseClass] return false except block
|
263
|
+
def synced_block_original?
|
264
|
+
false
|
265
|
+
end
|
266
|
+
|
173
267
|
# @param [Hash] json
|
174
268
|
# @return [NotionRubyMapping::Base]
|
175
269
|
def update_json(json)
|
176
|
-
raise StandardError, json.inspect unless json["object"] != "error" && @json.nil? || @json["type"] == json["type"]
|
270
|
+
raise StandardError, json.inspect unless json["object"] != "error" && (@json.nil? || @json["type"] == json["type"])
|
177
271
|
|
178
272
|
@json = json
|
179
273
|
@id = @nc.hex_id(@json["id"])
|
@@ -181,25 +275,7 @@ module NotionRubyMapping
|
|
181
275
|
self
|
182
276
|
end
|
183
277
|
|
184
|
-
|
185
|
-
# @return [Hash] json properties
|
186
|
-
def json_properties
|
187
|
-
@json && @json["properties"]
|
188
|
-
end
|
189
|
-
|
190
|
-
# @param [Object] method
|
191
|
-
# @param [Object] path
|
192
|
-
# @param [nil] json
|
193
|
-
def self.dry_run_script(method, path, json = nil)
|
194
|
-
shell = [
|
195
|
-
"#!/bin/sh\ncurl #{method == :get ? "" : "-X #{method.to_s.upcase}"} 'https://api.notion.com/#{path}'",
|
196
|
-
" -H 'Notion-Version: 2022-02-22'",
|
197
|
-
" -H 'Authorization: Bearer '\"$NOTION_API_KEY\"''",
|
198
|
-
]
|
199
|
-
shell << " -H 'Content-Type: application/json'" unless path == :get
|
200
|
-
shell << " --data '#{JSON.generate json}'" if json
|
201
|
-
shell.join(" \\\n")
|
202
|
-
end
|
278
|
+
### Not public announced methods
|
203
279
|
|
204
280
|
protected
|
205
281
|
|