notion_ruby_mapping 0.6.0 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: edf72d3f2ccbc741e909ce3c8eb1a8604da6404730862751a9fb2bea1af9865c
4
- data.tar.gz: de74bbb3f8821f93ec806f5e6854d54fa42fc5e3b915d33975b3ae8b2f4d4a28
3
+ metadata.gz: b295b0ba6b110a2f9e2f559e251490005a446abd71d2b7cc67903f9b0e2e504a
4
+ data.tar.gz: 8b6d1ed3f4c53973c8391981cef7044628ba22894dd42f8b23364f9a995326d9
5
5
  SHA512:
6
- metadata.gz: 410633c4128a8b41182c40527fde6df1752d199ef6921e8f3b9416537979f3b3bf42eb40434a6c4ecf022c7da919c5d6431b327dfdf3def302bb645c0e2d458c
7
- data.tar.gz: c5a6d6211363c59a2e8c14d9f27526a73b3537ab588ba4d81b7307a852cb376f209beac3ef49cb9d7028e0f59a193ce3aadd9d37a2a3433443959ba4ea6ba55e
6
+ metadata.gz: 5d3e718b49fceea38ee34be30161496d9023158631b24a633408af4b9eb5f5507ae85484c74a4f0094a7e1d8aaa47c851cc2d47af128e45814d98a5c1f5a21f4
7
+ data.tar.gz: 95b5374a44768b1454d550abcad6343c06d24333d787045d5317ba91ef0a1f0a1ddb273879ca28e928e2808180b7acbd144a4e8f19c52cb7208d565083d19906
data/README.md CHANGED
@@ -83,6 +83,11 @@ NotionCache.instance.create_client ENV["NOTION_API_TOKEN"] # from environment
83
83
  1. [Set icon to all icon unsettled pages](examples/set_icon_to_all_icon_unsettled_pages.md)
84
84
  1. [Renumbering pages](examples/renumbering_pages.md)
85
85
  1. [Change title](examples/change_title.md)
86
+ 1. [Create erDiagram from Notion database](tools/createErDiagram.rb)
87
+ ```shell
88
+ Usage:
89
+ ruby tools/createErDiagram.rb database_id code_block_id
90
+ ```
86
91
 
87
92
  ### 2.5 API reference
88
93
 
@@ -90,6 +95,9 @@ NotionCache.instance.create_client ENV["NOTION_API_TOKEN"] # from environment
90
95
 
91
96
  ## 3. ChangeLog
92
97
 
98
+ - 2022/8/9 [v0.6.3] update createErDiagram.rb (Fixed a bug with non-ASCII database titles)
99
+ - 2022/8/7 [v0.6.2] add comment_object support
100
+ - 2022/7/28 [v0.6.1] added createErDiagram.rb
93
101
  - 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
102
  - 2022/6/24 [v0.5.5] add file_names= to FileProperty
95
103
  - 2022/6/23 [v0.5.4] add update 'is_inline' and 'description' for database object
@@ -58,6 +58,24 @@ module NotionRubyMapping
58
58
  shell.join(" \\\n")
59
59
  end
60
60
 
61
+ def comments(query = nil, dry_run: false)
62
+ if page? || block?
63
+ if dry_run
64
+ self.class.dry_run_script :get, @nc.retrieve_comments_path(@id)
65
+ else
66
+ ans = {}
67
+ List.new(comment_parent: self,
68
+ json: @nc.retrieve_comments_request(@id, query),
69
+ query: query).each do |comment|
70
+ dt_id = comment.discussion_id
71
+ dt = ans[dt_id] ||= DiscussionThread.new dt_id
72
+ dt.comments << comment
73
+ end
74
+ ans
75
+ end
76
+ end
77
+ end
78
+
61
79
  # @param [String] key
62
80
  # @return [NotionRubyMapping::PropertyCache, Hash] obtained Page value or PropertyCache
63
81
  def get(key)
@@ -264,11 +282,7 @@ module NotionRubyMapping
264
282
  @new_record ? create(dry_run: true) : update(dry_run: true)
265
283
  else
266
284
  @new_record ? create : update
267
- if block?
268
-
269
- else
270
- @property_cache.clear_will_update
271
- end
285
+ @property_cache.clear_will_update if page?
272
286
  @payload.clear
273
287
  self
274
288
  end
@@ -5,13 +5,14 @@ module NotionRubyMapping
5
5
  class List < Base
6
6
  include Enumerable
7
7
 
8
- def initialize(json: nil, id: nil, database: nil, parent: nil, property: nil, query: nil)
8
+ def initialize(json: nil, id: nil, database: nil, parent: nil, property: nil, comment_parent: 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
14
  @property = property
15
+ @comment_parent = comment_parent
15
16
  @query = query
16
17
  @index = 0
17
18
  @has_content = true
@@ -20,105 +21,86 @@ module NotionRubyMapping
20
21
 
21
22
  # @return [NotionRubyMapping::List, Enumerator]
22
23
  # @see https://www.notion.so/hkob/List-9a0b32335e0d48849a785ce5e162c760#12e1c261a0944a4095776b7515bef4a1
23
- def each
24
- return enum_for(:each) unless block_given?
25
-
24
+ def each(&block)
25
+ return enum_for(:each) if block.nil?
26
26
  if @parent
27
- unless @has_content
28
- unless @load_all_contents
29
- @query.start_cursor = nil
30
- @json = @parent.children @query
31
- @has_more = @json["has_more"]
32
- end
33
- @index = 0
34
- @has_content = true
35
- end
36
-
37
- while @has_content
38
- if @index < results.length
39
- object = Base.create_from_json(results[@index])
40
- @index += 1
41
- yield object
42
- elsif @has_more
43
- if @parent
44
- @query.start_cursor = @json["next_cursor"]
45
- @json = @parent.children @query
46
- @index = 0
47
- @has_more = @json["has_more"]
48
- else
49
- @has_content = false
50
- end
51
- else
52
- @has_content = false
53
- end
54
- end
27
+ each_sub base: @page,
28
+ query: -> { @parent.children @query },
29
+ create_object: ->(json) { Base.create_from_json json },
30
+ &block
55
31
  elsif @database
56
- unless @has_content # re-exec
57
- unless @load_all_contents
58
- @query.start_cursor = nil
59
- @json = @nc.database_query_request @database.id, @query
60
- @has_more = @json["has_more"]
61
- end
62
- @index = 0
63
- @has_content = true
64
- end
32
+ each_sub base: @database,
33
+ query: -> { @nc.database_query_request @database.id, @query },
34
+ create_object: ->(json) { Base.create_from_json json },
35
+ &block
36
+ elsif @property
37
+ each_sub base: @property,
38
+ query: -> do
39
+ @nc.page_property_request @property.property_cache.page_id,
40
+ @property.property_id,
41
+ @query.query_json
42
+ end,
43
+ create_object: ->(json) do
44
+ case json["type"]
45
+ when "people"
46
+ UserObject.new json: json["people"]
47
+ when "relation"
48
+ json["relation"]["id"]
49
+ when "rich_text"
50
+ RichTextObject.create_from_json json["rich_text"]
51
+ when "title"
52
+ RichTextObject.create_from_json json["title"]
53
+ else
54
+ json
55
+ end
56
+ end,
57
+ &block
58
+ elsif @comment_parent
59
+ each_sub base: @comment_parent,
60
+ query: -> { @nc.retrieve_comments_request @comment_parent.id, @query },
61
+ create_object: ->(json) { CommentObject.new json: json },
62
+ &block
63
+ end
64
+ self
65
+ end
65
66
 
66
- while @has_content
67
- if @index < results.length
68
- object = Base.create_from_json(results[@index])
69
- @index += 1
70
- yield object
71
- elsif @has_more
72
- if @database
73
- @query.start_cursor = @json["next_cursor"]
74
- @json = @nc.database_query_request @database.id, @query
75
- @index = 0
76
- @has_more = @json["has_more"]
77
- else
78
- @has_content = false
79
- end
80
- else
81
- @has_content = false
82
- end
67
+ private
68
+
69
+ # @param [NotionRubyMapping::Page, NotionRubyMapping::Block] base page or block
70
+ # @param [Proc] query
71
+ # @param [Proc] create_object
72
+ # @param [Proc] block
73
+ def each_sub(base:, query:, create_object:, &block)
74
+ unless @has_content
75
+ unless @load_all_contents
76
+ @query.start_cursor = nil
77
+ @json = query.call
78
+ @has_more = @json["has_more"]
83
79
  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
80
+ @index = 0
81
+ @has_content = true
82
+ end
83
+
84
+ while @has_content
85
+ if @index < results.length
86
+ object = create_object.call results[@index]
87
+ @index += 1
88
+ block.call object
89
+ elsif @has_more
90
+ if base
91
+ @query.start_cursor = @json["next_cursor"]
92
+ @json = query.call
93
+ @index = 0
94
+ @has_more = @json["has_more"]
112
95
  else
113
96
  @has_content = false
114
97
  end
98
+ else
99
+ @has_content = false
115
100
  end
116
101
  end
117
- self
118
102
  end
119
103
 
120
- private
121
-
122
104
  # @return [Hash]
123
105
  def results
124
106
  @json["results"]
@@ -23,6 +23,16 @@ module NotionRubyMapping
23
23
  get key
24
24
  end
25
25
 
26
+ def append_comment(text_objects, dry_run: false)
27
+ rto = RichTextArray.new "rich_text", text_objects: text_objects, will_update: true
28
+ json = rto.property_schema_json.merge({"parent" => {"page_id" => @id}})
29
+ if dry_run
30
+ self.class.dry_run_script :post, @nc.comments_path, json
31
+ else
32
+ CommentObject.new json: (@nc.append_comment_request json)
33
+ end
34
+ end
35
+
26
36
  # @param [String] title
27
37
  # @param [Array<String, Property>] assigns
28
38
  # @return [NotionRubyMapping::Database]
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NotionRubyMapping
4
+ # DiscussionThread
5
+ class DiscussionThread
6
+ # @param [String] discussion_id
7
+ def initialize(discussion_id)
8
+ @discussion_id = discussion_id
9
+ @comments = []
10
+ end
11
+ attr_reader :discussion_id, :comments
12
+
13
+ # @param [String] text_objects
14
+ # @param [Boolean] dry_run
15
+ # @return [String, NotionRubyMapping::CommentObject]
16
+ def append_comment(text_objects, dry_run: false)
17
+ rto = RichTextArray.new "rich_text", text_objects: text_objects, will_update: true
18
+ nc = NotionCache.instance
19
+ json = rto.property_schema_json.merge({"discussion_id" => @discussion_id})
20
+ if dry_run
21
+ Base.dry_run_script :post, nc.comments_path, json
22
+ else
23
+ CommentObject.new json: (nc.append_comment_request json)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -45,6 +45,10 @@ module NotionRubyMapping
45
45
  request :patch, append_block_children_block_path(id), payload
46
46
  end
47
47
 
48
+ def append_comment_request(json)
49
+ request :post, comments_path, json
50
+ end
51
+
48
52
  # @param [String] id block_id (with or without "-")
49
53
  # @return [NotionRubyMapping::Base] Block object or nil
50
54
  def block(id)
@@ -81,6 +85,11 @@ module NotionRubyMapping
81
85
  @object_hash = {}
82
86
  end
83
87
 
88
+ # @return [String (frozen)]
89
+ def comments_path
90
+ "v1/comments"
91
+ end
92
+
84
93
  # @param [String] notion_token
85
94
  # @return [NotionRubyMapping::NotionCache] self (NotionCache.instance)
86
95
  def create_client(notion_token, wait: 0.3333, debug: false)
@@ -214,6 +223,17 @@ module NotionRubyMapping
214
223
  "v1/databases/#{database_id}/query"
215
224
  end
216
225
 
226
+ # @param [String] block_id
227
+ def retrieve_comments_path(block_id)
228
+ "v1/comments?block_id=#{block_id}"
229
+ end
230
+
231
+ # @param [String] block_id
232
+ # @param [NotionRubyMapping::Query, NilClass] query
233
+ def retrieve_comments_request(block_id, query)
234
+ request :get, retrieve_comments_path(block_id), (query&.query_json || {})
235
+ end
236
+
217
237
  # @param [Symbol] method
218
238
  # @param [String] path
219
239
  # @param [Hash] options
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NotionRubyMapping
4
+ # CommentObject
5
+ class CommentObject
6
+ # @param [String] text_objects
7
+ def initialize(text_objects: nil, json: {})
8
+ if text_objects
9
+ @text_objects = RichTextArray.new "rich_text", text_objects: text_objects
10
+ @json = {}
11
+ elsif json
12
+ @json = json
13
+ @text_objects = RichTextArray.new "rich_text", json: json["rich_text"]
14
+ else
15
+ raise StandardError, "Either text_objects or json is required CommentObject"
16
+ end
17
+ @will_update = false
18
+ end
19
+ attr_reader :will_update, :text_objects
20
+
21
+ def discussion_id
22
+ NotionCache.instance.hex_id @json["discussion_id"]
23
+ end
24
+
25
+ # @return [String] full_text
26
+ def full_text
27
+ @text_objects.full_text
28
+ end
29
+ end
30
+ end
@@ -107,6 +107,7 @@ module NotionRubyMapping
107
107
  "people" => PeopleProperty,
108
108
  "phone_number" => PhoneNumberProperty,
109
109
  "select" => SelectProperty,
110
+ "status" => StatusProperty,
110
111
  "title" => TitleProperty,
111
112
  "rich_text" => RichTextProperty,
112
113
  "url" => UrlProperty,
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NotionRubyMapping
4
+ # Status property
5
+ class StatusProperty < Property
6
+ TYPE = "status"
7
+
8
+ ### Public announced methods
9
+
10
+ ## Common methods
11
+
12
+ def status
13
+ @json
14
+ end
15
+
16
+ ### Not public announced methods
17
+
18
+ ## Common methods
19
+
20
+ # @param [String] name Property name
21
+ # @param [Boolean, Hash] json
22
+ def initialize(name, will_update: false, base_type: :page, property_cache: nil, json: {})
23
+ super name, will_update: will_update, base_type: base_type, property_cache: property_cache
24
+ @json = json
25
+ end
26
+
27
+ ## Page property only methods
28
+
29
+ # @return [Hash]
30
+ def property_values_json
31
+ assert_page_property __method__
32
+ {}
33
+ #{@name => {"status" => @json, "type" => "status"}}
34
+ end
35
+ end
36
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
- VERSION = "0.6.0"
4
+ VERSION = "0.6.3"
5
5
  NOTION_VERSION = "2022-06-28"
6
6
  end
@@ -12,13 +12,14 @@ require_relative "notion_ruby_mapping/version"
12
12
  link_preview_block link_to_page_block numbered_list_item_block paragraph_block pdf_block quote_block
13
13
  synced_block table_block table_row_block table_of_contents_block template_block to_do_block
14
14
  toggle_block video_block],
15
- controllers: %w[notion_cache payload property_cache query rich_text_array],
16
- objects: %w[rich_text_object emoji_object equation_object file_object mention_object text_object user_object],
15
+ controllers: %w[notion_cache payload property_cache query rich_text_array discussion_thread],
16
+ objects: %w[rich_text_object emoji_object equation_object file_object mention_object text_object user_object
17
+ comment_object],
17
18
  properties: %w[property checkbox_property multi_property created_by_property date_base_property created_time_property
18
19
  date_property email_property files_property formula_property last_edited_by_property
19
20
  last_edited_time_property multi_select_property number_property people_property phone_number_property
20
- relation_property text_property rich_text_property rollup_property select_property title_property
21
- url_property],
21
+ relation_property text_property rich_text_property rollup_property select_property status_property
22
+ title_property url_property],
22
23
  }.each do |key, values|
23
24
  values.each do |klass|
24
25
  require_relative "notion_ruby_mapping/#{key}/#{klass}"
@@ -0,0 +1,57 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require "notion_ruby_mapping"
4
+ include NotionRubyMapping
5
+
6
+ def append_database(text, db, db_titles)
7
+ base_title = db_title db
8
+ normalize_db_title(db, db_titles) if db_titles[db].nil?
9
+ text << "#{db_titles[db]} {"
10
+ text << %( Database title "#{base_title}") unless base_title == db_titles[db]
11
+ db.properties.reject { |p| p.is_a? RelationProperty }.each_with_index do |p, i|
12
+ class_name = p.class.name.split("::").last.sub /Property/, ""
13
+ text << %( #{class_name} p#{i} "#{p.name}")
14
+ end
15
+ text << "}\n"
16
+ end
17
+
18
+ def normalize_db_title(db, db_titles)
19
+ base_title = db_title db
20
+ db_titles[db] = base_title.gsub(/[\w\d\-_]+/, "").empty? ? base_title : "d#{db_titles.count}"
21
+ end
22
+
23
+ def db_title(db)
24
+ db.database_title.full_text.gsub " ", "_"
25
+ end
26
+
27
+ if ARGV.length < 2
28
+ print "Usage: createErDiagram.rb top_database_id code_block_id"
29
+ exit
30
+ end
31
+ database_id, code_block_id = ARGV
32
+ NotionCache.instance.create_client ENV["NOTION_API_KEY"]
33
+ block = Block.find code_block_id
34
+ unless block.is_a? CodeBlock
35
+ print "#{code_block_id} is not CodeBlock's id"
36
+ exit
37
+ end
38
+ dbs = [Database.find(database_id)]
39
+ text = %w[erDiagram]
40
+
41
+ finished = {}
42
+ db_titles = {}
43
+ until dbs.empty?
44
+ db = dbs.shift
45
+ finished[db] = true
46
+ append_database(text, db, db_titles)
47
+ db.properties.select { |pp| pp.is_a? RelationProperty }.each_with_index do |pp, i|
48
+ new_db = Database.find pp.relation_database_id
49
+ normalize_db_title(new_db, db_titles) if db_titles[new_db].nil?
50
+ text << "#{db_titles[db]} |o--o{ #{db_titles[new_db]} : r#{i}"
51
+ dbs << new_db unless finished[new_db]
52
+ end
53
+ text << ""
54
+ end
55
+ block.rich_text_array.rich_text_objects = text.join("\n ")
56
+ block.language = "mermaid"
57
+ 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.6.0
4
+ version: 0.6.3
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-07-22 00:00:00.000000000 Z
11
+ date: 2022-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -192,11 +192,13 @@ files:
192
192
  - lib/notion_ruby_mapping/blocks/url_base_block.rb
193
193
  - lib/notion_ruby_mapping/blocks/url_caption_base_block.rb
194
194
  - lib/notion_ruby_mapping/blocks/video_block.rb
195
+ - lib/notion_ruby_mapping/controllers/discussion_thread.rb
195
196
  - lib/notion_ruby_mapping/controllers/notion_cache.rb
196
197
  - lib/notion_ruby_mapping/controllers/payload.rb
197
198
  - lib/notion_ruby_mapping/controllers/property_cache.rb
198
199
  - lib/notion_ruby_mapping/controllers/query.rb
199
200
  - lib/notion_ruby_mapping/controllers/rich_text_array.rb
201
+ - lib/notion_ruby_mapping/objects/comment_object.rb
200
202
  - lib/notion_ruby_mapping/objects/emoji_object.rb
201
203
  - lib/notion_ruby_mapping/objects/equation_object.rb
202
204
  - lib/notion_ruby_mapping/objects/file_object.rb
@@ -224,6 +226,7 @@ files:
224
226
  - lib/notion_ruby_mapping/properties/rich_text_property.rb
225
227
  - lib/notion_ruby_mapping/properties/rollup_property.rb
226
228
  - lib/notion_ruby_mapping/properties/select_property.rb
229
+ - lib/notion_ruby_mapping/properties/status_property.rb
227
230
  - lib/notion_ruby_mapping/properties/text_property.rb
228
231
  - lib/notion_ruby_mapping/properties/title_property.rb
229
232
  - lib/notion_ruby_mapping/properties/url_property.rb
@@ -231,6 +234,7 @@ files:
231
234
  - notion_ruby_mapping.gemspec
232
235
  - sig/notion_ruby_mapping.rbs
233
236
  - tools/an
237
+ - tools/createErDiagram.rb
234
238
  homepage: https://github.com/hkob/notion_ruby_mapping.git
235
239
  licenses:
236
240
  - MIT