notion_ruby_mapping 0.3.3 → 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 +33 -1228
  3. data/env.yml.sample +0 -13
  4. data/lib/notion_ruby_mapping/{base.rb → blocks/base.rb} +155 -88
  5. data/lib/notion_ruby_mapping/blocks/block.rb +547 -0
  6. data/lib/notion_ruby_mapping/{database.rb → blocks/database.rb} +34 -7
  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,9 +13,9 @@ 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
@@ -26,6 +26,37 @@ module NotionRubyMapping
26
26
  end
27
27
  attr_reader :json, :id
28
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
+
29
60
  # @param [String] key
30
61
  # @return [NotionRubyMapping::PropertyCache, Hash] obtained Page value or PropertyCache
31
62
  def [](key)
@@ -42,9 +73,92 @@ module NotionRubyMapping
42
73
  end
43
74
  end
44
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
+
45
159
  # @return [NotionRubyMapping::CreatedTimeProperty]
46
160
  def created_time
47
- @created_time ||= CreatedTimeProperty.new("__timestamp__")
161
+ @created_time ||= CreatedTimeProperty.new("__timestamp__", json: self["created_time"])
48
162
  end
49
163
 
50
164
  # @return [TrueClass, FalseClass] true if Database object
@@ -53,16 +167,23 @@ module NotionRubyMapping
53
167
  end
54
168
 
55
169
  # @return [Hash, nil] obtained Hash
170
+ # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#e13d526bd709451e9b06fd32e8d07fcd
56
171
  def icon
57
172
  self["icon"]
58
173
  end
59
174
 
175
+ # @return [Hash] json properties
176
+ def json_properties
177
+ @json && @json["properties"]
178
+ end
179
+
60
180
  # @return [NotionRubyMapping::LastEditedTimeProperty]
61
181
  def last_edited_time
62
- @last_edited_time ||= LastEditedTimeProperty.new("__timestamp__")
182
+ @last_edited_time ||= LastEditedTimeProperty.new("__timestamp__", json: self["last_edited_time"])
63
183
  end
64
184
 
65
185
  # @return [Boolean] true if new record
186
+ # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#307af6e40d3840c59f8a82513a572d51
66
187
  def new_record?
67
188
  @new_record
68
189
  end
@@ -73,6 +194,7 @@ module NotionRubyMapping
73
194
  end
74
195
 
75
196
  # @return [NotionRubyMapping::PropertyCache] get or created PropertyCache object
197
+ # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#8f0b28e09dd74e2a9ff06126c48d64d4
76
198
  def properties
77
199
  unless @property_cache
78
200
  unless @json
@@ -85,70 +207,6 @@ module NotionRubyMapping
85
207
  @property_cache
86
208
  end
87
209
 
88
- # @return [NotionRubyMapping::Base, String]
89
- def save(dry_run: false)
90
- if dry_run
91
- @new_record ? create(dry_run: true) : update(dry_run: true)
92
- else
93
- @new_record ? create : update
94
- @property_cache.clear_will_update
95
- @payload.clear
96
- self
97
- end
98
- end
99
-
100
- # @param [String] emoji
101
- # @param [String] url
102
- # @return [NotionRubyMapping::Base]
103
- def set_icon(emoji: nil, url: nil)
104
- @payload.set_icon(emoji: emoji, url: url) if page? || database?
105
- self
106
- end
107
-
108
- # @return [String] title
109
- def title
110
- properties.select { |p| p.is_a? TitleProperty }.map(&:full_text).join ""
111
- end
112
-
113
- ### Not public announced methods
114
-
115
- # @param [Hash, Notion::Messages] json
116
- # @return [NotionRubyMapping::Base]
117
- def self.create_from_json(json)
118
- case json["object"]
119
- when "page"
120
- Page.new json: json
121
- when "database"
122
- Database.new json: json
123
- when "list"
124
- List.new json: json
125
- when "block"
126
- Block.new json: json
127
- else
128
- raise StandardError, json.inspect
129
- end
130
- end
131
-
132
- # @param [NotionRubyMapping::Property] klass
133
- # @param [String] title
134
- # @return [NotionRubyMapping::Property] generated property
135
- def assign_property(klass, title)
136
- @property_cache ||= PropertyCache.new({}, base_type: database? ? :database : :page)
137
-
138
- property = if database?
139
- klass.new(title, will_update: new_record?, base_type: :database)
140
- else
141
- klass.new(title, will_update: true, base_type: :page)
142
- end
143
- @property_cache.add_property property
144
- property
145
- end
146
-
147
- # @return [NotionRubyMapping::List]
148
- def children
149
- @children ||= @nc.block_children(id)
150
- end
151
-
152
210
  # @return [Hash] created json for update page
153
211
  def property_values_json
154
212
  @payload.property_values_json @property_cache
@@ -173,6 +231,33 @@ module NotionRubyMapping
173
231
  self
174
232
  end
175
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
+
176
261
  # @param [Hash] json
177
262
  # @return [NotionRubyMapping::Base]
178
263
  def update_json(json)
@@ -184,25 +269,7 @@ module NotionRubyMapping
184
269
  self
185
270
  end
186
271
 
187
- # protected
188
- # @return [Hash] json properties
189
- def json_properties
190
- @json && @json["properties"]
191
- end
192
-
193
- # @param [Object] method
194
- # @param [Object] path
195
- # @param [nil] json
196
- def self.dry_run_script(method, path, json = nil)
197
- shell = [
198
- "#!/bin/sh\ncurl #{method == :get ? "" : "-X #{method.to_s.upcase}"} 'https://api.notion.com/#{path}'",
199
- " -H 'Notion-Version: 2022-02-22'",
200
- " -H 'Authorization: Bearer '\"$NOTION_API_KEY\"''",
201
- ]
202
- shell << " -H 'Content-Type: application/json'" unless path == :get
203
- shell << " --data '#{JSON.generate json}'" if json
204
- shell.join(" \\\n")
205
- end
272
+ ### Not public announced methods
206
273
 
207
274
  protected
208
275