notion_ruby_mapping 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +35 -1224
  3. data/env.yml.sample +0 -13
  4. data/lib/notion_ruby_mapping/{base.rb → blocks/base.rb} +159 -89
  5. data/lib/notion_ruby_mapping/blocks/block.rb +547 -0
  6. data/lib/notion_ruby_mapping/{database.rb → blocks/database.rb} +40 -8
  7. data/lib/notion_ruby_mapping/{list.rb → blocks/list.rb} +35 -3
  8. data/lib/notion_ruby_mapping/blocks/page.rb +71 -0
  9. data/lib/notion_ruby_mapping/{notion_cache.rb → controllers/notion_cache.rb} +128 -102
  10. data/lib/notion_ruby_mapping/{payload.rb → controllers/payload.rb} +0 -0
  11. data/lib/notion_ruby_mapping/{property_cache.rb → controllers/property_cache.rb} +21 -20
  12. data/lib/notion_ruby_mapping/{query.rb → controllers/query.rb} +26 -18
  13. data/lib/notion_ruby_mapping/{rich_text_array.rb → controllers/rich_text_array.rb} +26 -15
  14. data/lib/notion_ruby_mapping/objects/emoji_object.rb +40 -0
  15. data/lib/notion_ruby_mapping/objects/equation_object.rb +43 -0
  16. data/lib/notion_ruby_mapping/objects/file_object.rb +60 -0
  17. data/lib/notion_ruby_mapping/{mention_object.rb → objects/mention_object.rb} +11 -0
  18. data/lib/notion_ruby_mapping/{rich_text_object.rb → objects/rich_text_object.rb} +11 -0
  19. data/lib/notion_ruby_mapping/{text_object.rb → objects/text_object.rb} +0 -1
  20. data/lib/notion_ruby_mapping/{user_object.rb → objects/user_object.rb} +9 -4
  21. data/lib/notion_ruby_mapping/{checkbox_property.rb → properties/checkbox_property.rb} +3 -0
  22. data/lib/notion_ruby_mapping/{created_by_property.rb → properties/created_by_property.rb} +0 -0
  23. data/lib/notion_ruby_mapping/{created_time_property.rb → properties/created_time_property.rb} +1 -0
  24. data/lib/notion_ruby_mapping/properties/date_base_property.rb +122 -0
  25. data/lib/notion_ruby_mapping/{date_property.rb → properties/date_property.rb} +13 -0
  26. data/lib/notion_ruby_mapping/{email_property.rb → properties/email_property.rb} +1 -0
  27. data/lib/notion_ruby_mapping/{files_property.rb → properties/files_property.rb} +26 -13
  28. data/lib/notion_ruby_mapping/{formula_property.rb → properties/formula_property.rb} +4 -0
  29. data/lib/notion_ruby_mapping/{last_edited_by_property.rb → properties/last_edited_by_property.rb} +1 -0
  30. data/lib/notion_ruby_mapping/{last_edited_time_property.rb → properties/last_edited_time_property.rb} +1 -0
  31. data/lib/notion_ruby_mapping/{multi_property.rb → properties/multi_property.rb} +0 -0
  32. data/lib/notion_ruby_mapping/{multi_select_property.rb → properties/multi_select_property.rb} +2 -3
  33. data/lib/notion_ruby_mapping/{number_property.rb → properties/number_property.rb} +4 -0
  34. data/lib/notion_ruby_mapping/{people_property.rb → properties/people_property.rb} +8 -3
  35. data/lib/notion_ruby_mapping/{phone_number_property.rb → properties/phone_number_property.rb} +4 -0
  36. data/lib/notion_ruby_mapping/{property.rb → properties/property.rb} +33 -0
  37. data/lib/notion_ruby_mapping/{relation_property.rb → properties/relation_property.rb} +30 -10
  38. data/lib/notion_ruby_mapping/{rich_text_property.rb → properties/rich_text_property.rb} +0 -0
  39. data/lib/notion_ruby_mapping/{rollup_property.rb → properties/rollup_property.rb} +7 -0
  40. data/lib/notion_ruby_mapping/{select_property.rb → properties/select_property.rb} +6 -1
  41. data/lib/notion_ruby_mapping/{text_property.rb → properties/text_property.rb} +0 -0
  42. data/lib/notion_ruby_mapping/{title_property.rb → properties/title_property.rb} +0 -0
  43. data/lib/notion_ruby_mapping/{url_property.rb → properties/url_property.rb} +5 -0
  44. data/lib/notion_ruby_mapping/version.rb +1 -1
  45. data/lib/notion_ruby_mapping.rb +14 -7
  46. metadata +43 -40
  47. data/lib/notion_ruby_mapping/block.rb +0 -10
  48. data/lib/notion_ruby_mapping/date_base_property.rb +0 -75
  49. 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,8 +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
7
 
8
8
  # @param [Hash, nil] json
9
9
  # @param [String, nil] id
@@ -13,16 +13,50 @@ module NotionRubyMapping
13
13
  @json = json
14
14
  @id = @nc.hex_id(id || json && @json["id"])
15
15
  @new_record = true unless parent.nil?
16
- raise StandardError, "Unknown id" if !is_a?(List) && @id.nil? && parent.nil?
16
+ raise StandardError, "Unknown id" if !is_a?(List) && !is_a?(Block) && @id.nil? && parent.nil?
17
17
 
18
- @payload = Payload.new(parent && {"parent" => parent})
18
+ @payload = Payload.new(!is_a?(Block) && parent && {"parent" => parent})
19
19
  @property_cache = nil
20
20
  @created_time = nil
21
21
  @last_edited_time = nil
22
+ return if assign.empty?
23
+
22
24
  assign.each_slice(2) { |(klass, key)| assign_property(klass, key) }
25
+ @json ||= {}
23
26
  end
24
27
  attr_reader :json, :id
25
28
 
29
+ # @param [Hash, Notion::Messages] json
30
+ # @return [NotionRubyMapping::Base]
31
+ def self.create_from_json(json)
32
+ case json["object"]
33
+ when "page"
34
+ Page.new json: json
35
+ when "database"
36
+ Database.new json: json
37
+ when "list"
38
+ List.new json: json
39
+ when "block"
40
+ Block.new(json: json).decode_block
41
+ else
42
+ raise StandardError, json.inspect
43
+ end
44
+ end
45
+
46
+ # @param [Object] method
47
+ # @param [Object] path
48
+ # @param [nil] json
49
+ def self.dry_run_script(method, path, json = nil)
50
+ shell = [
51
+ "#!/bin/sh\ncurl #{method == :get ? "" : "-X #{method.to_s.upcase}"} 'https://api.notion.com/#{path}'",
52
+ " -H 'Notion-Version: 2022-02-22'",
53
+ " -H 'Authorization: Bearer '\"$NOTION_API_KEY\"''",
54
+ ]
55
+ shell << " -H 'Content-Type: application/json'" unless path == :get
56
+ shell << " --data '#{JSON.generate json}'" if json
57
+ shell.join(" \\\n")
58
+ end
59
+
26
60
  # @param [String] key
27
61
  # @return [NotionRubyMapping::PropertyCache, Hash] obtained Page value or PropertyCache
28
62
  def [](key)
@@ -39,9 +73,92 @@ module NotionRubyMapping
39
73
  end
40
74
  end
41
75
 
76
+ # @param [String] url
77
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] caption
78
+ # @return [NotionRubyMapping::Block, String]
79
+ # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#44bbf83d852c419485c5efe9fe1558fb
80
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#2c47f7fedae543cf8566389ba1677132
81
+ def append_block_children(*blocks, dry_run: false)
82
+ raise StandardError, "This block can have no children." unless page? || (block? && can_have_children)
83
+
84
+ only_one = blocks.length == 1
85
+ json = {
86
+ "children" => Array(blocks).map do |block|
87
+ assert_parent_children_pair block
88
+ block.block_json
89
+ end
90
+ }
91
+ if dry_run
92
+ path = @nc.append_block_children_page_path(id)
93
+ self.class.dry_run_script :patch, path, json
94
+ else
95
+ response = @nc.append_block_children_request @id, json
96
+ raise StandardError, response unless response["results"]
97
+
98
+ answers = response["results"].map { |sub_json| Block.new json: sub_json }
99
+ only_one ? answers.first : answers
100
+ end
101
+ end
102
+
103
+ # @param [NotionRubyMapping::Block] block
104
+ def assert_parent_children_pair(block)
105
+ raise StandardError, "the argument is not a block." unless block.block?
106
+
107
+ raise StandardError, "#{type} cannot have children." if block? && !@can_have_children
108
+
109
+ return if block.can_append
110
+
111
+ bt = block.type
112
+ raise StandardError, "Internal file block can not append." if bt == "file"
113
+
114
+ raise StandardError, "Column block can only append column_list block" unless bt == "column" &&
115
+ block? && self.type == "columu_list"
116
+ end
117
+
118
+ # @param [NotionRubyMapping::Property] klass
119
+ # @param [String] title
120
+ # @return [NotionRubyMapping::Property] generated property
121
+ def assign_property(klass, title)
122
+ @property_cache ||= PropertyCache.new({}, base_type: database? ? :database : :page)
123
+ property = if database?
124
+ klass.new(title, will_update: new_record?, base_type: :database)
125
+ else
126
+ klass.new(title, will_update: true, base_type: :page)
127
+ end
128
+ @property_cache.add_property property
129
+ property
130
+ end
131
+
132
+ # @return [TrueClass, FalseClass] true if Block object
133
+ def block?
134
+ is_a? Block
135
+ end
136
+
137
+ # @param [NotionRubyMapping::Query] query
138
+ # @param [Boolean] dry_run
139
+ # @return [NotionRubyMapping::List, String]
140
+ def children(query = Query.new, dry_run: false)
141
+ if dry_run
142
+ path = @nc.block_children_page_path(id) + query.query_string
143
+ self.class.dry_run_script :get, path
144
+ elsif @children
145
+ @children
146
+ else
147
+ response = @nc.block_children_request @id, query.query_string
148
+ @children = List.new json: response, parent: self, query: query
149
+ end
150
+ end
151
+
152
+ # @return [NotionRubyMapping::Block]
153
+ def create_child_breadcrumb
154
+ raise StandardError unless page? || (block? && can_have_children)
155
+
156
+ Block.new(parent: self).breadcrumb
157
+ end
158
+
42
159
  # @return [NotionRubyMapping::CreatedTimeProperty]
43
160
  def created_time
44
- @created_time ||= CreatedTimeProperty.new("__timestamp__")
161
+ @created_time ||= CreatedTimeProperty.new("__timestamp__", json: self["created_time"])
45
162
  end
46
163
 
47
164
  # @return [TrueClass, FalseClass] true if Database object
@@ -50,16 +167,23 @@ module NotionRubyMapping
50
167
  end
51
168
 
52
169
  # @return [Hash, nil] obtained Hash
170
+ # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#e13d526bd709451e9b06fd32e8d07fcd
53
171
  def icon
54
172
  self["icon"]
55
173
  end
56
174
 
175
+ # @return [Hash] json properties
176
+ def json_properties
177
+ @json && @json["properties"]
178
+ end
179
+
57
180
  # @return [NotionRubyMapping::LastEditedTimeProperty]
58
181
  def last_edited_time
59
- @last_edited_time ||= LastEditedTimeProperty.new("__timestamp__")
182
+ @last_edited_time ||= LastEditedTimeProperty.new("__timestamp__", json: self["last_edited_time"])
60
183
  end
61
184
 
62
185
  # @return [Boolean] true if new record
186
+ # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#307af6e40d3840c59f8a82513a572d51
63
187
  def new_record?
64
188
  @new_record
65
189
  end
@@ -70,6 +194,7 @@ module NotionRubyMapping
70
194
  end
71
195
 
72
196
  # @return [NotionRubyMapping::PropertyCache] get or created PropertyCache object
197
+ # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#8f0b28e09dd74e2a9ff06126c48d64d4
73
198
  def properties
74
199
  unless @property_cache
75
200
  unless @json
@@ -82,70 +207,6 @@ module NotionRubyMapping
82
207
  @property_cache
83
208
  end
84
209
 
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
210
  # @return [Hash] created json for update page
150
211
  def property_values_json
151
212
  @payload.property_values_json @property_cache
@@ -170,10 +231,37 @@ module NotionRubyMapping
170
231
  self
171
232
  end
172
233
 
234
+ # @return [NotionRubyMapping::Base, String]
235
+ # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#277085c8439841c798a4b94eae9a7326
236
+ def save(dry_run: false)
237
+ if dry_run
238
+ @new_record ? create(dry_run: true) : update(dry_run: true)
239
+ else
240
+ @new_record ? create : update
241
+ @property_cache.clear_will_update
242
+ @payload.clear
243
+ self
244
+ end
245
+ end
246
+
247
+ # @param [String] emoji
248
+ # @param [String] url
249
+ # @return [NotionRubyMapping::Base]
250
+ # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#62eea67af7824496820c6bb903503540
251
+ # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#e13d526bd709451e9b06fd32e8d07fcd
252
+ def set_icon(emoji: nil, url: nil)
253
+ @payload.set_icon(emoji: emoji, url: url) if page? || database?
254
+ self
255
+ end
256
+
257
+ def description
258
+ "#{self.class.name}-#{@id}"
259
+ end
260
+
173
261
  # @param [Hash] json
174
262
  # @return [NotionRubyMapping::Base]
175
263
  def update_json(json)
176
- raise StandardError, json.inspect unless json["object"] != "error" && @json.nil? || @json["type"] == json["type"]
264
+ raise StandardError, json.inspect unless json["object"] != "error" && (@json.nil? || @json["type"] == json["type"])
177
265
 
178
266
  @json = json
179
267
  @id = @nc.hex_id(@json["id"])
@@ -181,25 +269,7 @@ module NotionRubyMapping
181
269
  self
182
270
  end
183
271
 
184
- # protected
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
272
+ ### Not public announced methods
203
273
 
204
274
  protected
205
275