notion_ruby_mapping 0.5.4 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +19 -3
  3. data/lib/notion_ruby_mapping/blocks/base.rb +37 -7
  4. data/lib/notion_ruby_mapping/blocks/list.rb +34 -1
  5. data/lib/notion_ruby_mapping/blocks/page.rb +8 -7
  6. data/lib/notion_ruby_mapping/controllers/notion_cache.rb +14 -0
  7. data/lib/notion_ruby_mapping/controllers/payload.rb +1 -0
  8. data/lib/notion_ruby_mapping/controllers/property_cache.rb +5 -2
  9. data/lib/notion_ruby_mapping/controllers/rich_text_array.rb +7 -0
  10. data/lib/notion_ruby_mapping/objects/rich_text_object.rb +5 -0
  11. data/lib/notion_ruby_mapping/properties/checkbox_property.rb +2 -2
  12. data/lib/notion_ruby_mapping/properties/created_by_property.rb +9 -2
  13. data/lib/notion_ruby_mapping/properties/created_time_property.rb +9 -2
  14. data/lib/notion_ruby_mapping/properties/date_property.rb +2 -2
  15. data/lib/notion_ruby_mapping/properties/email_property.rb +2 -2
  16. data/lib/notion_ruby_mapping/properties/files_property.rb +11 -3
  17. data/lib/notion_ruby_mapping/properties/formula_property.rb +9 -2
  18. data/lib/notion_ruby_mapping/properties/last_edited_by_property.rb +9 -2
  19. data/lib/notion_ruby_mapping/properties/last_edited_time_property.rb +9 -2
  20. data/lib/notion_ruby_mapping/properties/multi_select_property.rb +2 -2
  21. data/lib/notion_ruby_mapping/properties/number_property.rb +2 -2
  22. data/lib/notion_ruby_mapping/properties/people_property.rb +8 -4
  23. data/lib/notion_ruby_mapping/properties/phone_number_property.rb +2 -3
  24. data/lib/notion_ruby_mapping/properties/property.rb +86 -33
  25. data/lib/notion_ruby_mapping/properties/relation_property.rb +4 -3
  26. data/lib/notion_ruby_mapping/properties/rollup_property.rb +9 -2
  27. data/lib/notion_ruby_mapping/properties/select_property.rb +2 -2
  28. data/lib/notion_ruby_mapping/properties/text_property.rb +9 -2
  29. data/lib/notion_ruby_mapping/properties/title_property.rb +13 -1
  30. data/lib/notion_ruby_mapping/properties/url_property.rb +2 -2
  31. data/lib/notion_ruby_mapping/version.rb +2 -2
  32. data/tools/createErDiagram.rb +46 -0
  33. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eebf655e3f67cf1b8efd0b2e143550004b3d6277bb5bc9071d496c2814df2538
4
- data.tar.gz: e940233fed8804f92318434feaa99160f7ce3288b2e68e45eb09e3f4763f6efe
3
+ metadata.gz: 5d2f6819154bd2ae6fb448cd36d06311d20e6ba29a0e2e4fda6584d49ca58732
4
+ data.tar.gz: 042e30bda77b5c21a3226a37579254e52eb19d537abaa94a5233238846ba6a76
5
5
  SHA512:
6
- metadata.gz: aa4de2b82ccb55272ae93e36900036a666b5197671aedc7b54fe141d4a9bdfaaa7acd180765bf2e3948bf8dd2030f0e05aa4adfa4bd9539eb152c7e64f42ccb8
7
- data.tar.gz: 8f91372152624213a0fdde007c8d8a6fe3a0a9b76a9d17b0c8b123020aac498ab0a1f5e6a5187170c7666a87b21ddb2f2f0c68911074941b0918a2320a545d19
6
+ metadata.gz: 69419656c3c728d5472aade37de759a2a9b58124cd6856df7ecfa9b0838f5755af84f80a9936434a82991a7418a9f33de6188c87b20afff32573565c64ea5099
7
+ data.tar.gz: '093e56922c2f2aef5b6efd45043472e165b8bc3d04cce3613b9d1f363d779adca495982c818091e067dc9413107afbd26371ba73676d4def61b8abc1e1ea83b8'
data/README.md CHANGED
@@ -8,7 +8,9 @@ Development note is here. → [Idea note of "notion_ruby_mapping"](https://www.n
8
8
 
9
9
  - [notion_ruby_mapping](#notion_ruby_mapping)
10
10
  - [Table of Contents](#table-of-contents)
11
- - [0. Changes in v0.5.0](#0-changes-in-v050)
11
+ - [0. Changes](#0-changes)
12
+ - [0.1 Changes in v0.6.0](#01-changes-in-v060)
13
+ - [0.2 Changes in v0.5.0](#02-changes-in-v050)
12
14
  - [1. Installation](#1-installation)
13
15
  - [2. How to use](#2-how-to-use)
14
16
  - [2.1 Create a New Integration](#21-create-a-new-integration)
@@ -21,9 +23,18 @@ Development note is here. → [Idea note of "notion_ruby_mapping"](https://www.n
21
23
  - [5. License](#5-license)
22
24
  - [6. Code of Conduct](#6-code-of-conduct)
23
25
 
24
- ## 0. Changes in v0.5.0
26
+ ## 0. Changes
25
27
 
26
- NotionRubyMapping v.0.5.0 now supports block updates.
28
+ ### 0.1 Changes in v0.6.0
29
+
30
+ NotionRubyMapping v0.6.0 now supports Notion-Version 2022-06-28.
31
+ In 2022-06-28, property values are no longer returned when retrieving pages.
32
+ NotionRubyMapping temporarily creates a Property Object and calls the retrieve a property item API when a value is needed.
33
+ Therefore, users do not need to be aware of any differences, and existing scripts should work as they are.
34
+
35
+ ### 0.2 Changes in v0.5.0
36
+
37
+ NotionRubyMapping v0.5.0 now supports block updates.
27
38
  For efficiency, subclasses are provided under Block class. As a result, they are no longer compatible with the scripts used in v0.4.0.
28
39
 
29
40
  ## 1. Installation
@@ -79,6 +90,11 @@ NotionCache.instance.create_client ENV["NOTION_API_TOKEN"] # from environment
79
90
 
80
91
  ## 3. ChangeLog
81
92
 
93
+ - 2022/7/22 [v0.6.0] updates for Notion-Version 2022-06-28 (lazy retrieve property values, retrieve page/database/block parent, single_property/dual_property for RelationProperty)
94
+ - 2022/6/24 [v0.5.5] add file_names= to FileProperty
95
+ - 2022/6/23 [v0.5.4] add update 'is_inline' and 'description' for database object
96
+ - 2022/6/14 [v0.5.3] add time zone for query database by Date (before, after, on_or_before, on_or_after)
97
+ - 2022/6/8 [v0.5.2] Change query database filter for date with time zone
82
98
  - 2022/6/5 [v0.5.1] bug fix for append_block_children. added synced_block_original to SyncedBlock
83
99
  - 2022/6/4 [v0.5.0] added subclasses of the block class and update_block API support
84
100
  - 2022/5/19 [v0.4.1] added delete_block
@@ -50,7 +50,7 @@ module NotionRubyMapping
50
50
  def self.dry_run_script(method, path, json = nil)
51
51
  shell = [
52
52
  "#!/bin/sh\ncurl #{method == :get ? "" : "-X #{method.to_s.upcase}"} 'https://api.notion.com/#{path}'",
53
- " -H 'Notion-Version: 2022-02-22'",
53
+ " -H 'Notion-Version: 2022-06-28'",
54
54
  " -H 'Authorization: Bearer '\"$NOTION_API_KEY\"''",
55
55
  ]
56
56
  shell << " -H 'Content-Type: application/json'" if %i[post patch].include?(method)
@@ -120,11 +120,13 @@ module NotionRubyMapping
120
120
  # @param [String] title
121
121
  # @return [NotionRubyMapping::Property] generated property
122
122
  def assign_property(klass, title)
123
- @property_cache ||= PropertyCache.new({}, base_type: database? ? :database : :page)
123
+ @property_cache ||= PropertyCache.new({},
124
+ base_type: database? ? :database : :page,
125
+ page_id: page? ? @id : nil)
124
126
  property = if database?
125
127
  klass.new(title, will_update: new_record?, base_type: :database)
126
128
  else
127
- klass.new(title, will_update: true, base_type: :page)
129
+ klass.new(title, will_update: true, base_type: :page, property_cache: @property_cache)
128
130
  end
129
131
  @property_cache.add_property property
130
132
  property
@@ -192,6 +194,24 @@ module NotionRubyMapping
192
194
  is_a? Page
193
195
  end
194
196
 
197
+ def parent(dry_run: false)
198
+ parent_json = @json && @json["parent"]
199
+ raise StandardError, "Unknown parent" if parent_json.nil?
200
+
201
+ type = parent_json["type"]
202
+ klass = case type
203
+ when "database_id"
204
+ Database
205
+ when "page_id"
206
+ Page
207
+ when "block_id"
208
+ Block
209
+ else
210
+ raise StandardError, "List does not have any parent"
211
+ end
212
+ klass.find parent_json[type], dry_run: dry_run
213
+ end
214
+
195
215
  # @return [NotionRubyMapping::PropertyCache] get or created PropertyCache object
196
216
  # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#8f0b28e09dd74e2a9ff06126c48d64d4
197
217
  def properties
@@ -201,7 +221,9 @@ module NotionRubyMapping
201
221
 
202
222
  reload
203
223
  end
204
- @property_cache = PropertyCache.new json_properties, base_type: database? ? :database : :page
224
+ @property_cache = PropertyCache.new json_properties,
225
+ base_type: database? ? :database : :page,
226
+ page_id: page? ? @id : nil
205
227
  end
206
228
  @property_cache
207
229
  end
@@ -225,20 +247,28 @@ module NotionRubyMapping
225
247
  return unless is_a?(Page) || is_a?(Database)
226
248
 
227
249
  ps.each do |key, json|
228
- properties[key].update_from_json json
250
+ if json["type"]
251
+ properties[key].update_from_json json
252
+ else
253
+ properties[key]&.clear_will_update
254
+ end
229
255
  end
230
256
  self
231
257
  end
232
258
 
233
259
  # @param [Boolean] dry_run
234
- # @return [NotionRubyMapping::Base, String]
260
+ # @return [NotionRubyMapping::Base, NotionRubyMapping::Database, String]
235
261
  # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#277085c8439841c798a4b94eae9a7326
236
262
  def save(dry_run: false)
237
263
  if dry_run
238
264
  @new_record ? create(dry_run: true) : update(dry_run: true)
239
265
  else
240
266
  @new_record ? create : update
241
- @property_cache.clear_will_update unless block?
267
+ if block?
268
+
269
+ else
270
+ @property_cache.clear_will_update
271
+ end
242
272
  @payload.clear
243
273
  self
244
274
  end
@@ -5,12 +5,13 @@ module NotionRubyMapping
5
5
  class List < Base
6
6
  include Enumerable
7
7
 
8
- def initialize(json: nil, id: nil, database: nil, parent: nil, query: nil)
8
+ def initialize(json: nil, id: nil, database: nil, parent: nil, property: nil, query: nil)
9
9
  super(json: json, id: id)
10
10
  @has_more = @json["has_more"]
11
11
  @load_all_contents = !@has_more
12
12
  @database = database
13
13
  @parent = parent
14
+ @property = property
14
15
  @query = query
15
16
  @index = 0
16
17
  @has_content = true
@@ -80,6 +81,38 @@ module NotionRubyMapping
80
81
  @has_content = false
81
82
  end
82
83
  end
84
+ elsif @property
85
+ while @has_content
86
+ if @index < results.length
87
+ json = results[@index]
88
+ object = case json["type"]
89
+ when "people"
90
+ UserObject.new json: json["people"]
91
+ when "relation"
92
+ json["relation"]["id"]
93
+ when "rich_text"
94
+ RichTextObject.create_from_json json["rich_text"]
95
+ when "title"
96
+ RichTextObject.create_from_json json["title"]
97
+ else
98
+ json
99
+ end
100
+ @index += 1
101
+ yield object
102
+ elsif @has_more
103
+ if @property
104
+ @query ||= Query.new
105
+ @query.start_cursor = @json["next_cursor"]
106
+ @json = NotionCache.instance.page_property_request @property.property_cache.page_id, @property.property_id, @query.query_json
107
+ @index = 0
108
+ @has_more = @json["has_more"]
109
+ else
110
+ @has_content = false
111
+ end
112
+ else
113
+ @has_content = false
114
+ end
115
+ end
83
116
  end
84
117
  self
85
118
  end
@@ -26,7 +26,7 @@ module NotionRubyMapping
26
26
  # @param [String] title
27
27
  # @param [Array<String, Property>] assigns
28
28
  # @return [NotionRubyMapping::Database]
29
- # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#2e8ca5408afb4f83a92b7b84c0dc9903
29
+ # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#71f680d59b874930bf9f488a7cd6a49e
30
30
  def build_child_database(title, *assigns)
31
31
  db = Database.new json: {"title" => [TextObject.new(title).property_values_json]},
32
32
  assign: assigns, parent: {"type" => "page_id", "page_id" => @id}
@@ -34,18 +34,19 @@ module NotionRubyMapping
34
34
  db
35
35
  end
36
36
 
37
+ # @param [String] title
38
+ # @param [Array<String, Property>] assigns
39
+ # @return [NotionRubyMapping::Database]
40
+ # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#e3f1d21e0f724f589e48431468772eed
37
41
  def create_child_database(title, *assigns, dry_run: false)
38
- build_child_database title, *assigns
39
- db = Database.new json: {"title" => [TextObject.new(title).property_values_json]},
40
- assign: assigns, parent: {"type" => "page_id", "page_id" => @id}
41
- yield db, db.properties
42
- db.save dry_run: dry_run
42
+ build_child_database(title, *assigns).save dry_run: dry_run
43
43
  end
44
44
 
45
45
  # @return [String] title
46
46
  # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#2ff7209055f346fbbda454cdbb40b1c8
47
47
  def title
48
- properties.select { |p| p.is_a? TitleProperty }.map(&:full_text).join ""
48
+ tp = properties.select { |p| (p.is_a?(TitleProperty)) || (p.is_a?(Property) && p.property_id == "title") }
49
+ tp.map(&:full_text).join ""
49
50
  end
50
51
 
51
52
  protected
@@ -183,6 +183,20 @@ module NotionRubyMapping
183
183
  "v1/pages/#{page_id}"
184
184
  end
185
185
 
186
+ # @param [String] page_id
187
+ # @param [String] property_id
188
+ # @return [String (frozen)] page_property_path
189
+ def page_property_path(page_id, property_id)
190
+ [page_path(page_id), "properties/#{property_id}"].join "/"
191
+ end
192
+
193
+ # @param [String] page_id
194
+ # @param [String] property_id
195
+ # @return [Hash] response
196
+ def page_property_request(page_id, property_id, query = {})
197
+ request :get, page_property_path(page_id, property_id), query
198
+ end
199
+
186
200
  # @param [String] page_id
187
201
  # @return [Hash] response
188
202
  def page_request(page_id)
@@ -15,6 +15,7 @@ module NotionRubyMapping
15
15
 
16
16
  # @return [Hash] {}
17
17
  def clear
18
+ @update_block_key = []
18
19
  @json = {}
19
20
  end
20
21
 
@@ -4,23 +4,26 @@ module NotionRubyMapping
4
4
  # PropertyCache class
5
5
  class PropertyCache
6
6
  include Enumerable
7
- def initialize(json = {}, base_type: :page)
7
+ def initialize(json = {}, base_type: :page, page_id: nil)
8
8
  @properties = {}
9
9
  @json = json
10
10
  @base_type = base_type
11
+ @page_id = page_id
11
12
  end
12
13
  attr_writer :json
14
+ attr_reader :page_id
13
15
 
14
16
  # @param [String] key
15
17
  # @return [Property] Property for key
16
18
  # @see https://www.notion.so/hkob/PropertyCache-2451fa64a814432db4809831cc77ba25#9709e2b2a7a0479f9951291a501f65c8
17
19
  def [](key)
18
- @properties[key] ||= Property.create_from_json key, @json[key], @base_type
20
+ @properties[key] ||= Property.create_from_json key, @json[key], @base_type, self
19
21
  end
20
22
 
21
23
  # @param [Property] property added Property
22
24
  def add_property(property)
23
25
  @properties[property.name] = property
26
+ property.property_cache = self
24
27
  self
25
28
  end
26
29
 
@@ -46,6 +46,13 @@ module NotionRubyMapping
46
46
  @rich_text_objects[index]
47
47
  end
48
48
 
49
+ # @return [FalseClass]
50
+ def clear_will_update
51
+ @will_update = false
52
+ @rich_text_objects.each(&:clear_will_update)
53
+ false
54
+ end
55
+
49
56
  # @param [Array] json
50
57
  # @return [Array] RichTextArray
51
58
  def create_from_json(json = [])
@@ -73,6 +73,11 @@ module NotionRubyMapping
73
73
  }.merge annotations_json
74
74
  end
75
75
 
76
+ # @return [FalseClass]
77
+ def clear_will_update
78
+ @will_update = false
79
+ end
80
+
76
81
  # @param [String] url
77
82
  # @return [String] input text
78
83
  def href=(url)
@@ -33,8 +33,8 @@ module NotionRubyMapping
33
33
 
34
34
  # @param [String] name Property name
35
35
  # @param [Boolean, Hash] json
36
- def initialize(name, will_update: false, base_type: :page, json: nil)
37
- super name, will_update: will_update, base_type: base_type
36
+ def initialize(name, will_update: false, base_type: :page, property_cache: nil, json: nil)
37
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache
38
38
  @json = if database?
39
39
  json || {}
40
40
  else
@@ -22,8 +22,8 @@ module NotionRubyMapping
22
22
  # @param [String] name Property name
23
23
  # @param [String] user_id user_id (optional)
24
24
  # @param [Hash] json json (optional)
25
- def initialize(name, will_update: false, base_type: :page, json: nil, user_id: nil)
26
- super name, will_update: will_update, base_type: base_type
25
+ def initialize(name, will_update: false, base_type: :page, json: nil, user_id: nil, property_cache: nil)
26
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache
27
27
  @json = if database?
28
28
  json || {}
29
29
  else
@@ -37,5 +37,12 @@ module NotionRubyMapping
37
37
  cb = json["created_by"]
38
38
  @json = database? ? cb : UserObject.new(json: cb)
39
39
  end
40
+
41
+ ## Page property only methods
42
+ # @return [Hash]
43
+ def property_values_json
44
+ assert_page_property __method__
45
+ {}
46
+ end
40
47
  end
41
48
  end
@@ -21,10 +21,17 @@ module NotionRubyMapping
21
21
 
22
22
  # @param [String] name Property name
23
23
  # @param [String] json created_time value (optional)
24
- def initialize(name, will_update: false, base_type: :page, json: nil)
25
- super name, will_update: will_update, base_type: base_type
24
+ def initialize(name, will_update: false, base_type: :page, json: nil, property_cache: nil)
25
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache
26
26
  @json = json
27
27
  @json ||= {} if database?
28
28
  end
29
+
30
+ ## Page property only methods
31
+ # @return [Hash]
32
+ def property_values_json
33
+ assert_page_property __method__
34
+ {}
35
+ end
29
36
  end
30
37
  end
@@ -75,8 +75,8 @@ module NotionRubyMapping
75
75
  # @param [Date, Time, DateTime, String, nil] start_date
76
76
  # @param [Date, Time, DateTime, String, nil] end_date
77
77
  # @param [String, nil] time_zone
78
- def initialize(name, will_update: false, base_type: :page, json: nil, start_date: nil, end_date: nil, time_zone: nil)
79
- super name, will_update: will_update, base_type: base_type
78
+ def initialize(name, will_update: false, base_type: :page, json: nil, start_date: nil, end_date: nil, time_zone: nil, property_cache: nil)
79
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache
80
80
  @json = json || {}
81
81
  return if database?
82
82
 
@@ -32,8 +32,8 @@ module NotionRubyMapping
32
32
  ## Common methods
33
33
 
34
34
  # @param [String] name Property name
35
- def initialize(name, will_update: false, base_type: :page, json: nil)
36
- super name, will_update: will_update, base_type: base_type
35
+ def initialize(name, will_update: false, base_type: :page, json: nil, property_cache: nil)
36
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache
37
37
  @json = json || {}
38
38
  end
39
39
 
@@ -19,14 +19,22 @@ module NotionRubyMapping
19
19
  @file_names = Array(files)
20
20
  end
21
21
 
22
+ def file_names=(file_names = [])
23
+ array_file_names = Array(file_names)
24
+ raise StandardError, "files and file_names must be the same sizes." unless @files.length == array_file_names.length
25
+
26
+ @will_update = true
27
+ @file_names = array_file_names
28
+ end
29
+
22
30
  ### Not public announced methods
23
31
 
24
32
  ## Common methods
25
33
 
26
34
  # @param [String] name Property name
27
35
  # @param [String] files files value (optional)
28
- def initialize(name, will_update: false, base_type: :page, json: nil, files: [])
29
- super name, will_update: will_update, base_type: base_type
36
+ def initialize(name, will_update: false, base_type: :page, json: nil, files: [], property_cache: nil)
37
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache
30
38
  if database?
31
39
  @files = json || {}
32
40
  elsif json
@@ -58,7 +66,7 @@ module NotionRubyMapping
58
66
  @files = json["files"].map { |sub_json| FileObject.new json: sub_json }
59
67
  @file_names = json["files"].map { |sub_json| sub_json["name"] }
60
68
  @will_update = false
61
- p self
69
+ self
62
70
  end
63
71
 
64
72
  protected
@@ -41,8 +41,8 @@ module NotionRubyMapping
41
41
 
42
42
  # @param [String] name
43
43
  # @param [Hash] json
44
- def initialize(name, will_update: false, base_type: :page, json: nil, formula: nil)
45
- super name, will_update: will_update, base_type: base_type
44
+ def initialize(name, will_update: false, base_type: :page, json: nil, formula: nil, property_cache: nil)
45
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache
46
46
  @json = json || {}
47
47
  return unless database?
48
48
 
@@ -62,6 +62,13 @@ module NotionRubyMapping
62
62
  ans
63
63
  end
64
64
 
65
+ ## Page property only methods
66
+ # @return [Hash]
67
+ def property_values_json
68
+ assert_page_property __method__
69
+ {}
70
+ end
71
+
65
72
  protected
66
73
 
67
74
  ## Database property only methods
@@ -22,8 +22,8 @@ module NotionRubyMapping
22
22
  # @param [String] name Property name
23
23
  # @param [String] user_id user_id (optional)
24
24
  # @param [Hash] json json (optional)
25
- def initialize(name, will_update: false, base_type: :page, json: nil, user_id: nil)
26
- super name, will_update: will_update, base_type: base_type
25
+ def initialize(name, will_update: false, base_type: :page, json: nil, user_id: nil, property_cache: nil)
26
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache
27
27
  @json = if database?
28
28
  json || {}
29
29
  else
@@ -38,5 +38,12 @@ module NotionRubyMapping
38
38
  leb = json["last_edited_by"]
39
39
  @json = database? ? leb : UserObject.new(json: leb)
40
40
  end
41
+
42
+ ## Page property only methods
43
+ # @return [Hash]
44
+ def property_values_json
45
+ assert_page_property __method__
46
+ {}
47
+ end
41
48
  end
42
49
  end
@@ -21,10 +21,17 @@ module NotionRubyMapping
21
21
 
22
22
  # @param [String] name Property name
23
23
  # @param [String] json last_edited_time value (optional)
24
- def initialize(name, will_update: false, base_type: :page, json: nil)
25
- super name, will_update: will_update, base_type: base_type
24
+ def initialize(name, will_update: false, base_type: :page, json: nil, property_cache: nil)
25
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache
26
26
  @json = json
27
27
  @json ||= {} if database?
28
28
  end
29
+
30
+ ## Page property only methods
31
+ # @return [Hash]
32
+ def property_values_json
33
+ assert_page_property __method__
34
+ {}
35
+ end
29
36
  end
30
37
  end
@@ -61,8 +61,8 @@ module NotionRubyMapping
61
61
  # @param [String] name
62
62
  # @param [Hash] json
63
63
  # @param [Array<String>, String] multi_select
64
- def initialize(name, will_update: false, base_type: :page, json: nil, multi_select: nil)
65
- super name, will_update: will_update, base_type: base_type
64
+ def initialize(name, will_update: false, base_type: :page, json: nil, multi_select: nil, property_cache: nil)
65
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache
66
66
  if database?
67
67
  @json = json || {"options" => []}
68
68
  else
@@ -53,8 +53,8 @@ module NotionRubyMapping
53
53
 
54
54
  # @param [String] name Property name
55
55
  # @param [Float, Integer, Hash] json Number value or format Hash
56
- def initialize(name, will_update: false, base_type: :page, json: nil, format: nil)
57
- super name, will_update: will_update, base_type: base_type
56
+ def initialize(name, will_update: false, base_type: :page, json: nil, format: nil, property_cache: nil)
57
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache
58
58
  @json = json
59
59
  @json ||= {"format" => (format || "number")} if database?
60
60
  end
@@ -39,17 +39,21 @@ module NotionRubyMapping
39
39
 
40
40
  ## Common methods
41
41
 
42
+ def self.people_from_json(json)
43
+ List.new(json: json, property: self).select { true }
44
+ end
45
+
42
46
  # @param [String] name
43
47
  # @param [Hash] json
44
48
  # @param [Array] people ids for people
45
- def initialize(name, will_update: false, base_type: :page, json: nil, people: nil)
46
- super name, will_update: will_update, base_type: base_type
49
+ def initialize(name, will_update: false, base_type: :page, json: nil, people: nil, property_cache: nil, query: nil)
50
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache, query: query
47
51
  @json = if database?
48
52
  {}
49
53
  elsif people
50
54
  Array(people).map { |uo| UserObject.user_object(uo) }
51
55
  elsif json
52
- json.map { |p| UserObject.new json: p }
56
+ PeopleProperty.people_from_json json
53
57
  else
54
58
  []
55
59
  end
@@ -72,7 +76,7 @@ module NotionRubyMapping
72
76
  # @return [Hash, Array]
73
77
  def update_from_json(json)
74
78
  @will_update = false
75
- @json = database? ? {} : json["people"].map { |p_json| UserObject.new json: p_json }
79
+ @json = database? ? {} : PeopleProperty.people_from_json(json)
76
80
  end
77
81
  end
78
82
  end
@@ -34,13 +34,12 @@ module NotionRubyMapping
34
34
  ## Common methods
35
35
 
36
36
  # @param [String] name Property name
37
- def initialize(name, will_update: false, base_type: :page, json: nil)
38
- super name, will_update: will_update, base_type: base_type
37
+ def initialize(name, will_update: false, base_type: :page, json: nil, property_cache: nil)
38
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache
39
39
  @json = database? ? {} : json
40
40
  end
41
41
 
42
42
  ## Page property only methods
43
-
44
43
  # @return [Hash]
45
44
  def property_values_json
46
45
  assert_page_property __method__
@@ -1,13 +1,30 @@
1
1
  # frozen_string_literal: true
2
+ #
3
+ require "forwardable"
2
4
 
3
5
  module NotionRubyMapping
4
6
  # abstract class for property
5
7
  class Property
8
+ extend Forwardable
9
+
6
10
  ### Public announced methods
7
11
 
8
12
  ## Common methods
9
13
 
10
- attr_reader :name, :will_update
14
+ attr_reader :name, :will_update, :property_id
15
+ attr_accessor :property_cache
16
+
17
+ def_delegators :retrieve_page_property, :<<, :[], :add_person, :add_relation, :checkbox, :checkbox=, :created_by,
18
+ :created_time, :date, :each, :email, :email=, :end_date, :end_date=, :files, :files=, :filter_after,
19
+ :filter_before, :filter_contains, :filter_does_not_contain, :filter_does_not_equal,
20
+ :filter_ends_with, :filter_equals, :filter_greater_than, :filter_greater_than_or_equal_to,
21
+ :filter_is_empty, :filter_is_not_empty, :filter_less_than, :filter_less_than_or_equal_to,
22
+ :filter_next_month, :filter_next_week, :filter_next_year, :filter_on_or_after, :filter_on_or_before,
23
+ :filter_past_month, :filter_past_week, :filter_past_year, :filter_starts_with, :formula, :full_text,
24
+ :last_edited_by, :last_edited_time, :multi_select, :multi_select=, :multi_select_names, :number,
25
+ :number=, :people, :people=, :phone_number, :phone_number=, :property_values_json, :relation=,
26
+ :rollup, :start_date, :start_date=, :time_zone, :time_zone=, :select, :select=, :select_name, :url,
27
+ :url=
11
28
 
12
29
  ## Database property only methods
13
30
 
@@ -32,7 +49,7 @@ module NotionRubyMapping
32
49
 
33
50
  # @param [String] name Property name
34
51
  # @return [Property] generated Property object
35
- def initialize(name, will_update: false, base_type: :page)
52
+ def initialize(name, will_update: false, base_type: :page, property_id: nil, property_cache: nil, query: nil)
36
53
  @name = name
37
54
  @will_update = will_update
38
55
  @base_type = base_type
@@ -40,39 +57,64 @@ module NotionRubyMapping
40
57
  @remove = false
41
58
  @new_name = nil
42
59
  @json = nil
60
+ @property_id = property_id
61
+ @property_cache = property_cache
62
+ @query = query
43
63
  end
44
64
 
45
65
  # @param [String] name
46
66
  # @param [Hash] input_json
47
67
  # @return [NotionRubyMapping::Property, nil] generated Property object
48
- def self.create_from_json(name, input_json, base_type = :page)
68
+ # @param [Symbol] base_type :page or :database
69
+ # @param [String, nil] page_id
70
+ def self.create_from_json(name, input_json, base_type = :page, property_cache = nil, query = nil)
49
71
  raise StandardError, "Property not found: #{name}:#{input_json}" if input_json.nil?
50
72
 
51
73
  type = input_json["type"]
52
- klass = {
53
- "checkbox" => CheckboxProperty,
54
- "created_time" => CreatedTimeProperty,
55
- "date" => DateProperty,
56
- "formula" => FormulaProperty,
57
- "last_edited_time" => LastEditedTimeProperty,
58
- "rollup" => RollupProperty,
59
- "email" => EmailProperty,
60
- "files" => FilesProperty,
61
- "created_by" => CreatedByProperty,
62
- "last_edited_by" => LastEditedByProperty,
63
- "multi_select" => MultiSelectProperty,
64
- "people" => PeopleProperty,
65
- "relation" => RelationProperty,
66
- "number" => NumberProperty,
67
- "phone_number" => PhoneNumberProperty,
68
- "select" => SelectProperty,
69
- "title" => TitleProperty,
70
- "rich_text" => RichTextProperty,
71
- "url" => UrlProperty,
72
- }[type]
73
- raise StandardError, "Irregular property type: #{type}" unless klass
74
-
75
- klass.new name, json: input_json[type], base_type: base_type
74
+ if type.nil?
75
+ new name, property_id: input_json["id"], base_type: base_type, property_cache: property_cache, query: query
76
+ elsif type == "property_item"
77
+ tmp = new name, property_id: input_json["property_item"]["id"], base_type: base_type,
78
+ property_cache: property_cache, query: query
79
+ objects = List.new(json: input_json, property: tmp, query: query).select { true }
80
+ case input_json["property_item"]["type"]
81
+ when "people"
82
+ PeopleProperty.new name, people: objects, base_type: base_type, property_cache: property_cache, query: query
83
+ when "relation"
84
+ RelationProperty.new name, relation: objects, base_type: base_type, property_cache: property_cache, query: query
85
+ when "rich_text"
86
+ RichTextProperty.new name, text_objects: objects, base_type: base_type, property_cache: property_cache, query: query
87
+ when "rollup"
88
+ RollupProperty.new name, json: objects, base_type: base_type, property_cache: property_cache, query: query
89
+ when "title"
90
+ TitleProperty.new name, text_objects: objects, base_type: base_type, property_cache: property_cache, query: query
91
+ end
92
+ else
93
+ klass = {
94
+ "checkbox" => CheckboxProperty,
95
+ "created_time" => CreatedTimeProperty,
96
+ "date" => DateProperty,
97
+ "formula" => FormulaProperty,
98
+ "last_edited_time" => LastEditedTimeProperty,
99
+ "rollup" => RollupProperty,
100
+ "email" => EmailProperty,
101
+ "files" => FilesProperty,
102
+ "created_by" => CreatedByProperty,
103
+ "last_edited_by" => LastEditedByProperty,
104
+ "multi_select" => MultiSelectProperty,
105
+ "relation" => RelationProperty,
106
+ "number" => NumberProperty,
107
+ "people" => PeopleProperty,
108
+ "phone_number" => PhoneNumberProperty,
109
+ "select" => SelectProperty,
110
+ "title" => TitleProperty,
111
+ "rich_text" => RichTextProperty,
112
+ "url" => UrlProperty,
113
+ }[type]
114
+ raise StandardError, "Irregular property type: #{type}" unless klass
115
+
116
+ klass.new name, json: input_json[type], base_type: base_type, property_cache: property_cache
117
+ end
76
118
  end
77
119
 
78
120
  # @return [FalseClass]
@@ -85,6 +127,11 @@ module NotionRubyMapping
85
127
  @base_type == :database
86
128
  end
87
129
 
130
+ # @return [TrueClass, FalseClass] true if it has Property contents
131
+ def contents?
132
+ !instance_of? Property
133
+ end
134
+
88
135
  # @param [String] key query parameter
89
136
  # @param [Object] value query value
90
137
  # @return [NotionRubyMapping::Query] generated Query object
@@ -108,7 +155,9 @@ module NotionRubyMapping
108
155
  # @param [Hash] json
109
156
  def update_from_json(json)
110
157
  @will_update = false
111
- @json = json[type]
158
+ return unless contents?
159
+
160
+ @json = json[type] if json[type] && json[type] != "property_item"
112
161
  end
113
162
 
114
163
  # @return [Symbol] property type
@@ -148,12 +197,16 @@ module NotionRubyMapping
148
197
  raise StandardError, "#{method} can execute only Page property." unless @base_type == :page
149
198
  end
150
199
 
151
- ## Page property only methods
152
-
153
- # @return [Hash] {} created_time cannot be updated
154
- def property_values_json
200
+ # @return [NotionRubyMapping::Property, Array<UserObject>, nil]
201
+ def retrieve_page_property
155
202
  assert_page_property __method__
156
- {}
203
+ raise StandardError, "property_cache.page_id is empty" if @property_cache.page_id.nil?
204
+
205
+ json = NotionCache.instance.page_property_request @property_cache.page_id, @property_id,
206
+ (@query&.query_json || {})
207
+ new_property = self.class.create_from_json @name, json, :page, @property_cache, @query
208
+ @property_cache.add_property new_property
209
+ new_property
157
210
  end
158
211
 
159
212
  protected
@@ -54,10 +54,11 @@ module NotionRubyMapping
54
54
  # @param [String] database_id
55
55
  # @param [String] synced_property_name
56
56
  # @see https://www.notion.so/hkob/RelationProperty-f608ab41a1f0476b98456620346fba03#7f5029fb7f6e4c009f22888b233e6f64
57
- def replace_relation_database(database_id: nil, synced_property_name: nil)
57
+ def replace_relation_database(database_id: nil, type: "dual_property", synced_property_name: nil)
58
58
  assert_database_property __method__
59
59
  @will_update = true
60
60
  @json["database_id"] = database_id if database_id
61
+ @json["type"] = type
61
62
  @json["synced_property_name"] = synced_property_name if synced_property_name
62
63
  @json
63
64
  end
@@ -69,8 +70,8 @@ module NotionRubyMapping
69
70
  # @param [String] name
70
71
  # @param [Hash, Array] json
71
72
  # @param [String, Array] relation
72
- def initialize(name, will_update: false, json: nil, relation: nil, base_type: :page)
73
- super name, will_update: will_update, base_type: base_type
73
+ def initialize(name, will_update: false, json: nil, relation: nil, base_type: :page, property_cache: nil, query: nil)
74
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache, query: query
74
75
  @json = if database?
75
76
  json || {}
76
77
  elsif relation
@@ -71,8 +71,8 @@ module NotionRubyMapping
71
71
 
72
72
  # @param [String] name
73
73
  # @param [Hash] json
74
- def initialize(name, will_update: false, json: nil, base_type: :page)
75
- super name, will_update: will_update, base_type: base_type
74
+ def initialize(name, will_update: false, json: nil, base_type: :page, property_cache: nil, query: nil)
75
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache, query: query
76
76
  @json = json || {}
77
77
  end
78
78
 
@@ -92,6 +92,13 @@ module NotionRubyMapping
92
92
  ans
93
93
  end
94
94
 
95
+ ## Page property only methods
96
+
97
+ def property_values_json
98
+ assert_page_property __method__
99
+ {}
100
+ end
101
+
95
102
  protected
96
103
 
97
104
  ## Database property only methods
@@ -74,8 +74,8 @@ module NotionRubyMapping
74
74
  # @param [String] name Property name
75
75
  # @param [Hash] json
76
76
  # @param [String] select String value (optional)
77
- def initialize(name, will_update: false, base_type: :page, json: nil, select: nil)
78
- super name, will_update: will_update, base_type: base_type
77
+ def initialize(name, will_update: false, base_type: :page, json: nil, select: nil, property_cache: nil)
78
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache
79
79
  @json = if database?
80
80
  json || {"options" => []}
81
81
  else
@@ -27,10 +27,10 @@ module NotionRubyMapping
27
27
  # @param [String] name
28
28
  # @param [Hash, Array] json
29
29
  # @param [Array<RichTextObject>] text_objects
30
- def initialize(name, will_update: false, base_type: :page, json: nil, text_objects: nil)
30
+ def initialize(name, will_update: false, base_type: :page, json: nil, text_objects: nil, property_cache: nil, query: nil)
31
31
  raise StandardError, "TextObject is abstract class. Please use RichTextProperty." if instance_of? TextProperty
32
32
 
33
- super name, will_update: will_update, base_type: base_type
33
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache, query: query
34
34
  @text_objects = if database?
35
35
  json || {}
36
36
  else
@@ -38,6 +38,13 @@ module NotionRubyMapping
38
38
  end
39
39
  end
40
40
 
41
+ # @return [FalseClass]
42
+ def clear_will_update
43
+ super
44
+ @text_objects.clear_will_update
45
+ false
46
+ end
47
+
41
48
  # @return [TrueClass, FalseClass] will update?
42
49
  def will_update
43
50
  @will_update || page? && @text_objects.will_update
@@ -9,18 +9,30 @@ module NotionRubyMapping
9
9
 
10
10
  ## Common methods
11
11
 
12
+ def self.rich_text_array_from_json(json)
13
+ rich_text_objects = List.new(json: json, property: self).select { true }
14
+ RichTextArray.rich_text_array "title", rich_text_objects
15
+ end
16
+
12
17
  # @param [Hash] json
13
18
  def update_from_json(json)
14
19
  @will_update = false
15
20
  if database?
16
21
  @json = json || {}
17
22
  else
18
- @text_objects = RichTextArray.new "rich_text", json: json["title"]
23
+ @text_objects = TitleProperty.rich_text_array_from_json json
19
24
  end
20
25
  end
21
26
 
22
27
  ## Page property only methods
23
28
 
29
+ # @return [FalseClass]
30
+ def clear_will_update
31
+ super
32
+ @text_objects.clear_will_update
33
+ false
34
+ end
35
+
24
36
  # @return [Hash] created json
25
37
  def property_values_json
26
38
  assert_page_property __method__
@@ -34,8 +34,8 @@ module NotionRubyMapping
34
34
  ## Common methods
35
35
 
36
36
  # @param [String] name Property name
37
- def initialize(name, will_update: false, base_type: :page, json: nil)
38
- super name, will_update: will_update, base_type: base_type
37
+ def initialize(name, will_update: false, base_type: :page, json: nil, property_cache: nil)
38
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache
39
39
  @json = json || (database? ? {} : nil)
40
40
  end
41
41
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
- VERSION = "0.5.4"
5
- NOTION_VERSION = "2022-02-22"
4
+ VERSION = "0.6.1"
5
+ NOTION_VERSION = "2022-06-28"
6
6
  end
@@ -0,0 +1,46 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require "notion_ruby_mapping"
4
+ include NotionRubyMapping
5
+
6
+ def append_database(text, db)
7
+ text << "#{db_title db} {"
8
+ db.properties.reject { |p| p.is_a? RelationProperty }.each_with_index do |p, i|
9
+ class_name = p.class.name.split("::").last.sub /Property/, ""
10
+ text << %Q[ #{class_name} p#{i} "#{p.name}"]
11
+ end
12
+ text << "}\n"
13
+ end
14
+
15
+ def db_title(db)
16
+ db.database_title.full_text.gsub " ", "_"
17
+ end
18
+
19
+ if ARGV.length < 2
20
+ print "Usage: createErDiagram.rb top_database_id code_block_id"
21
+ exit
22
+ end
23
+ database_id, code_block_id = ARGV
24
+ NotionCache.instance.create_client ENV["NOTION_API_KEY"]
25
+ block = Block.find code_block_id
26
+ unless block.is_a? CodeBlock
27
+ print "#{code_block_id} is not CodeBlock's id"
28
+ end
29
+ dbs = [Database.find(database_id)]
30
+ text = %w[erDiagram]
31
+
32
+ finished = {}
33
+ until dbs.empty?
34
+ db = dbs.shift
35
+ finished[db] = true
36
+ append_database(text, db)
37
+ db.properties.select { |p| p.is_a? RelationProperty }.each_with_index do |p, i|
38
+ new_db = Database.find p.relation_database_id
39
+ text << "#{db_title db} ||--o{ #{db_title new_db} : r#{i}"
40
+ dbs << new_db unless finished[new_db]
41
+ end
42
+ text << ""
43
+ end
44
+ block.rich_text_array.rich_text_objects = text.join("\n ")
45
+ block.language = "mermaid"
46
+ block.save
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notion_ruby_mapping
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroyuki KOBAYASHI
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-23 00:00:00.000000000 Z
11
+ date: 2022-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -231,6 +231,7 @@ files:
231
231
  - notion_ruby_mapping.gemspec
232
232
  - sig/notion_ruby_mapping.rbs
233
233
  - tools/an
234
+ - tools/createErDiagram.rb
234
235
  homepage: https://github.com/hkob/notion_ruby_mapping.git
235
236
  licenses:
236
237
  - MIT