notion_ruby_mapping 0.2.3 → 0.3.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 (42) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +416 -135
  3. data/env.yml.sample +1 -0
  4. data/examples/change_title.md +26 -0
  5. data/examples/renumbering_pages.md +20 -0
  6. data/examples/set_icon_to_all_icon_unsettled_pages.md +30 -0
  7. data/lib/notion_ruby_mapping/base.rb +124 -71
  8. data/lib/notion_ruby_mapping/checkbox_property.rb +27 -12
  9. data/lib/notion_ruby_mapping/created_by_property.rb +22 -10
  10. data/lib/notion_ruby_mapping/created_time_property.rb +16 -14
  11. data/lib/notion_ruby_mapping/database.rb +74 -10
  12. data/lib/notion_ruby_mapping/date_property.rb +67 -42
  13. data/lib/notion_ruby_mapping/email_property.rb +26 -16
  14. data/lib/notion_ruby_mapping/files_property.rb +40 -24
  15. data/lib/notion_ruby_mapping/formula_property.rb +51 -10
  16. data/lib/notion_ruby_mapping/last_edited_by_property.rb +22 -9
  17. data/lib/notion_ruby_mapping/last_edited_time_property.rb +16 -14
  18. data/lib/notion_ruby_mapping/list.rb +3 -0
  19. data/lib/notion_ruby_mapping/multi_select_property.rb +83 -21
  20. data/lib/notion_ruby_mapping/notion_cache.rb +17 -13
  21. data/lib/notion_ruby_mapping/number_property.rb +71 -10
  22. data/lib/notion_ruby_mapping/page.rb +32 -10
  23. data/lib/notion_ruby_mapping/payload.rb +21 -3
  24. data/lib/notion_ruby_mapping/people_property.rb +47 -19
  25. data/lib/notion_ruby_mapping/phone_number_property.rb +26 -15
  26. data/lib/notion_ruby_mapping/property.rb +140 -49
  27. data/lib/notion_ruby_mapping/property_cache.rb +31 -3
  28. data/lib/notion_ruby_mapping/query.rb +10 -0
  29. data/lib/notion_ruby_mapping/relation_property.rb +74 -21
  30. data/lib/notion_ruby_mapping/rich_text_array.rb +81 -0
  31. data/lib/notion_ruby_mapping/rich_text_object.rb +1 -0
  32. data/lib/notion_ruby_mapping/rich_text_property.rb +12 -1
  33. data/lib/notion_ruby_mapping/rollup_property.rb +82 -10
  34. data/lib/notion_ruby_mapping/select_property.rb +88 -13
  35. data/lib/notion_ruby_mapping/text_object.rb +0 -2
  36. data/lib/notion_ruby_mapping/text_property.rb +19 -45
  37. data/lib/notion_ruby_mapping/title_property.rb +12 -1
  38. data/lib/notion_ruby_mapping/url_property.rb +26 -15
  39. data/lib/notion_ruby_mapping/user_object.rb +4 -4
  40. data/lib/notion_ruby_mapping/version.rb +1 -1
  41. data/lib/notion_ruby_mapping.rb +2 -1
  42. metadata +6 -2
@@ -5,41 +5,94 @@ module NotionRubyMapping
5
5
  class RelationProperty < MultiProperty
6
6
  TYPE = "relation"
7
7
 
8
- # @param [String] name
9
- # @param [Hash] json
10
- # @param [String, Array<String>] relation
11
- def initialize(name, will_update: false, json: nil, relation: nil)
12
- super name, will_update: will_update
13
- @relation = if relation
14
- Array(relation)
15
- elsif json
16
- json.map { |r| r["id"] }
17
- else
18
- []
19
- end
8
+ ### Public announced methods
9
+
10
+ ## Common methods
11
+
12
+ # @return [Hash, Array]
13
+ def relation
14
+ @json
20
15
  end
21
16
 
22
- # @param [String, Array<String>] relation a id or Array of id
23
- # @return [Array<String>] settled relation Array
24
- def relation=(relation)
17
+ ## Database property only methods
18
+
19
+ # @return [String] relation database_id
20
+ def relation_database_id
21
+ assert_database_property __method__
22
+ @json["database_id"]
23
+ end
24
+
25
+ # @param [String] database_id
26
+ # @param [String] synced_property_name
27
+ def replace_relation_database(database_id: nil, synced_property_name: nil)
28
+ assert_database_property __method__
25
29
  @will_update = true
26
- @relation = Array(relation)
30
+ @json["database_id"] = database_id if database_id
31
+ @json["synced_property_name"] = synced_property_name if synced_property_name
32
+ @json
27
33
  end
28
34
 
29
- # @param [Hash] json
30
- def update_from_json(json)
31
- @will_update = false
32
- @relation = json["relation"].map { |r| r["id"] }
35
+ ### Not public announced methods
36
+
37
+ ## Common methods
38
+
39
+ # @param [String] name
40
+ # @param [Hash, Array] json
41
+ # @param [String, Array] relation
42
+ def initialize(name, will_update: false, json: nil, relation: nil, base_type: :page)
43
+ super name, will_update: will_update, base_type: base_type
44
+ @json = if database?
45
+ json || {}
46
+ elsif relation
47
+ Array(relation).map { |r| {"id" => r} }
48
+ elsif json
49
+ json
50
+ else
51
+ []
52
+ end
53
+ end
54
+
55
+ ## Database property only methods
56
+
57
+ # @return [Hash]
58
+ def update_property_schema_json
59
+ assert_database_property __method__
60
+ ans = super
61
+ return ans if ans != {} || !@will_update
62
+
63
+ ans[@name] ||= {}
64
+ ans[@name]["relation"] = @json
65
+ ans
66
+ end
67
+
68
+ ## Page property only methods
69
+
70
+ # @param [String, Array<String>] relation a id or Array of id
71
+ # @return [Array] settled relation Array
72
+ def relation=(page_ids)
73
+ assert_page_property __method__
74
+ @will_update = true
75
+ @json = Array(page_ids).map { |r| {"id" => r} }
33
76
  end
34
77
 
35
78
  # @return [Hash] created json
36
79
  def property_values_json
80
+ assert_page_property __method__
37
81
  {
38
82
  @name => {
39
83
  "type" => "relation",
40
- "relation" => @relation.map { |rid| {"id" => rid} },
84
+ "relation" => @json,
41
85
  },
42
86
  }
43
87
  end
88
+
89
+ protected
90
+
91
+ ## Database property only methods
92
+
93
+ # @return [Hash]
94
+ def property_schema_json_sub
95
+ {"database_id" => relation_database_id}
96
+ end
44
97
  end
45
98
  end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NotionRubyMapping
4
+ # RichTextObject
5
+ class RichTextArray
6
+ include Enumerable
7
+
8
+ # @param [Array] json
9
+ def initialize(key, json: nil, text_objects: nil, will_update: false)
10
+ @key = key
11
+ @rich_text_objects = if json
12
+ create_from_json(json)
13
+ elsif text_objects
14
+ Array(text_objects).map do |to|
15
+ RichTextObject.text_object to
16
+ end
17
+ else
18
+ []
19
+ end
20
+ @will_update = will_update
21
+ end
22
+
23
+ # @param [Array] json
24
+ # @return [Array] RichTextArray
25
+ def create_from_json(json = [])
26
+ json.map { |rt_json| RichTextObject.create_from_json rt_json }
27
+ end
28
+
29
+ # @param [Numeric] index
30
+ # @return [NotionRubyMapping::RichTextObject] removed RichTextObject
31
+ def delete_at(index)
32
+ @will_update = true
33
+ @rich_text_objects.delete_at index
34
+ end
35
+
36
+ # @param [Proc] block
37
+ # @return [Enumerator]
38
+ def each(&block)
39
+ return enum_for(:each) unless block
40
+
41
+ @rich_text_objects.each(&block)
42
+ end
43
+
44
+ # @return [String]
45
+ def full_text
46
+ map(&:text).join ""
47
+ end
48
+
49
+ def property_values_json
50
+ will_update ? @rich_text_objects.map(&:property_values_json) : []
51
+ end
52
+
53
+ def property_schema_json
54
+ will_update ? {@key => @rich_text_objects.map(&:property_values_json)} : {}
55
+ end
56
+
57
+ def update_property_schema_json
58
+ will_update ? {@key => @rich_text_objects.map(&:property_values_json)} : {}
59
+ end
60
+
61
+ # @return [TrueClass, FalseClass] true if it will update
62
+ def will_update
63
+ @will_update || @rich_text_objects.map(&:will_update).any?
64
+ end
65
+
66
+ # @param [String, RichTextObject] to
67
+ # @return [NotionRubyMapping::RichTextObject] added RichTextObject
68
+ def <<(to)
69
+ @will_update = true
70
+ rto = RichTextObject.text_object(to)
71
+ @rich_text_objects << rto
72
+ rto
73
+ end
74
+
75
+ # @param [Numeric] index index number
76
+ # @return [RichTextObject] selected RichTextObject
77
+ def [](index)
78
+ @rich_text_objects[index]
79
+ end
80
+ end
81
+ end
@@ -14,6 +14,7 @@ module NotionRubyMapping
14
14
  @type = type
15
15
  @options = options
16
16
  end
17
+ attr_reader :will_update
17
18
 
18
19
  def self.create_from_json(json)
19
20
  type = json["type"]
@@ -5,14 +5,25 @@ module NotionRubyMapping
5
5
  class RichTextProperty < TextProperty
6
6
  TYPE = "rich_text"
7
7
 
8
+ ### Not public announced methods
9
+
10
+ ## Common methods
11
+
8
12
  # @param [Hash] json
9
13
  def update_from_json(json)
10
14
  @will_update = false
11
- @text_objects = json["rich_text"].map { |to| RichTextObject.create_from_json to }
15
+ if database?
16
+ @json = json["rich_text"] || {}
17
+ else
18
+ @text_objects = RichTextArray.new "rich_text", json: json["rich_text"]
19
+ end
12
20
  end
13
21
 
22
+ ## Page property only methods
23
+
14
24
  # @return [Hash] created json
15
25
  def property_values_json
26
+ assert_page_property __method__
16
27
  text_json = @text_objects.map(&:property_values_json)
17
28
  {
18
29
  @name => {
@@ -8,22 +8,94 @@ module NotionRubyMapping
8
8
  include GreaterThanLessThan
9
9
  TYPE = "rollup"
10
10
 
11
+ ### Public announced methods
12
+
13
+ ## Common methods
14
+
15
+ # @return [Hash]
16
+ def rollup
17
+ @json
18
+ end
19
+
20
+ ## Database property only methods
21
+
22
+ # @return [String] new or settled function
23
+ def function
24
+ assert_database_property __method__
25
+ @json["function"]
26
+ end
27
+
28
+ # @param [String] func
29
+ def function=(func)
30
+ assert_database_property __method__
31
+ @will_update = true
32
+ @json["function"] = func
33
+ end
34
+
35
+ # @return [String] new or settled relation_property_name
36
+ def relation_property_name
37
+ assert_database_property __method__
38
+ @json["relation_property_name"]
39
+ end
40
+
41
+ # @param [String] rpn
42
+ def relation_property_name=(rpn)
43
+ assert_database_property __method__
44
+ @will_update = true
45
+ @json["relation_property_name"] = rpn
46
+ end
47
+
48
+ # @return [String] new or settled rollup_property_name
49
+ def rollup_property_name
50
+ assert_database_property __method__
51
+ @json["rollup_property_name"]
52
+ end
53
+
54
+ # @param [String] rpn
55
+ def rollup_property_name=(rpn)
56
+ assert_database_property __method__
57
+ @will_update = true
58
+ @json["rollup_property_name"] = rpn
59
+ end
60
+
61
+ ### Not public announced methods
62
+
63
+ ## Common methods
64
+
11
65
  # @param [String] name
12
66
  # @param [Hash] json
13
- def initialize(name, json: nil)
14
- super name, will_update: false
15
- @json = json
67
+ def initialize(name, will_update: false, json: nil, base_type: :page)
68
+ super name, will_update: will_update, base_type: base_type
69
+ @json = json || {}
16
70
  end
17
71
 
18
- # @return [Hash] {} created_time cannot be updated
19
- def property_values_json
20
- {}
72
+ ## Database property only methods
73
+
74
+ # @return [Hash]
75
+ def update_property_schema_json
76
+ assert_database_property __method__
77
+ ans = super
78
+ return ans if ans != {} || !@will_update
79
+
80
+ ans[@name] ||= {}
81
+ ans[@name]["rollup"] ||= {}
82
+ ans[@name]["rollup"]["function"] = function
83
+ ans[@name]["rollup"]["relation_property_name"] = relation_property_name
84
+ ans[@name]["rollup"]["rollup_property_name"] = rollup_property_name
85
+ ans
21
86
  end
22
87
 
23
- # @param [Hash] json
24
- def update_from_json(json)
25
- @will_update = false
26
- @json = json["rollup"]
88
+ protected
89
+
90
+ ## Database property only methods
91
+
92
+ # @return [Hash]
93
+ def property_schema_json_sub
94
+ {
95
+ "function" => function,
96
+ "relation_property_name" => relation_property_name,
97
+ "rollup_property_name" => rollup_property_name,
98
+ }
27
99
  end
28
100
  end
29
101
  end
@@ -7,30 +7,105 @@ module NotionRubyMapping
7
7
  include IsEmptyIsNotEmpty
8
8
  TYPE = "select"
9
9
 
10
+ ### Public announced methods
11
+
12
+ ## Common methods
13
+
14
+ # @return [Hash]
15
+ def select
16
+ @json
17
+ end
18
+
19
+ ## Page property only methods
20
+
21
+ # @return [String]
22
+ def select_name
23
+ assert_page_property __method__
24
+ @json["name"]
25
+ end
26
+
27
+ ## Database property only methods
28
+
29
+ # @param [String] name
30
+ # @param [String] color
31
+ # @return [Array] added array
32
+ def add_select_options(name:, color:)
33
+ edit_select_options << {"name" => name, "color" => color}
34
+ end
35
+
36
+ # @return [Array] copyed multi select options
37
+ def edit_select_options
38
+ assert_database_property __method__
39
+ @will_update = true
40
+ @json["options"] ||= []
41
+ end
42
+
43
+ # @return [Array]
44
+ def select_options
45
+ assert_database_property __method__
46
+ @json["options"] || []
47
+ end
48
+
49
+ # @return [String]
50
+ def select_names
51
+ assert_database_property __method__
52
+ @json["options"].map { |s| s["name"] }
53
+ end
54
+
55
+ ## Page property only methods
56
+
57
+ # @param [String] select
58
+ # @return [Hash] settled value
59
+ def select=(select)
60
+ @will_update = true
61
+ @json = {"name" => select}
62
+ end
63
+
64
+ ### Not public announced methods
65
+
66
+ ## Common methods
67
+
10
68
  # @param [String] name Property name
11
69
  # @param [Hash] json
12
70
  # @param [String] select String value (optional)
13
- def initialize(name, will_update: false, json: nil, select: nil)
14
- super name, will_update: will_update
15
- @select = select || json && json["name"]
71
+ def initialize(name, will_update: false, base_type: :page, json: nil, select: nil)
72
+ super name, will_update: will_update, base_type: base_type
73
+ @json = if database?
74
+ json || {"options" => []}
75
+ else
76
+ json || {"name" => select}
77
+ end
16
78
  end
17
79
 
18
- # @param [Hash] json
19
- def update_from_json(json)
20
- @will_update = false
21
- @select = json["select"]["name"]
80
+ ## Database property only methods
81
+
82
+ # @return [Hash]
83
+ def update_property_schema_json
84
+ assert_database_property __method__
85
+ ans = super
86
+ return ans if ans != {} || !@will_update
87
+
88
+ ans[@name] ||= {}
89
+ ans[@name]["select"] ||= {}
90
+ ans[@name]["select"]["options"] = @json["options"]
91
+ ans
22
92
  end
23
93
 
94
+ ## Page property only methods
95
+
24
96
  # @return [Hash]
25
97
  def property_values_json
26
- {@name => {"type" => "select", "select" => (@select ? {"name" => @select} : @json)}}
98
+ assert_page_property __method__
99
+ {@name => {"type" => "select", "select" => @json}}
27
100
  end
28
101
 
29
- # @param [String] select
30
- # @return [String] settled value
31
- def select=(select)
32
- @will_update = true
33
- @select = select
102
+ protected
103
+
104
+ ## Database property only methods
105
+
106
+ # @return [Hash]
107
+ def property_schema_json_sub
108
+ {"options" => edit_select_options}
34
109
  end
35
110
  end
36
111
  end
@@ -14,7 +14,6 @@ module NotionRubyMapping
14
14
  attr_reader :text, :will_update
15
15
 
16
16
  # @param [String, RichTextObject] value
17
- # @return [RichTextObject] self
18
17
  def text=(value)
19
18
  @will_update = true
20
19
  if value.is_a? RichTextObject
@@ -24,7 +23,6 @@ module NotionRubyMapping
24
23
  @text = value
25
24
  @options["plain_text"] = value
26
25
  end
27
- self
28
26
  end
29
27
 
30
28
  protected
@@ -12,61 +12,35 @@ module NotionRubyMapping
12
12
  include StartsWithEndsWith
13
13
  include IsEmptyIsNotEmpty
14
14
 
15
+ ### Public announced methods
16
+
17
+ ## Page property only methods
18
+
19
+ attr_reader :text_objects
20
+
21
+ def_delegators :@text_objects, :[], :<<, :each, :full_text, :delete_at
22
+
23
+ ### Not public announced methods
24
+
25
+ ## Common methods
26
+
15
27
  # @param [String] name
16
- # @param [Hash] json
28
+ # @param [Hash, Array] json
17
29
  # @param [Array<RichTextObject>] text_objects
18
- def initialize(name, will_update: false, json: nil, text_objects: nil)
30
+ def initialize(name, will_update: false, base_type: :page, json: nil, text_objects: nil)
19
31
  raise StandardError, "TextObject is abstract class. Please use RichTextProperty." if instance_of? TextProperty
20
32
 
21
- super name, will_update: will_update
22
- @text_objects = if text_objects
23
- text_objects.map { |to_s| RichTextObject.text_object to_s }
24
- elsif json
25
- json.map { |to| RichTextObject.create_from_json to }
33
+ super name, will_update: will_update, base_type: base_type
34
+ @text_objects = if database?
35
+ json || {}
26
36
  else
27
- []
37
+ RichTextArray.new "title", json: json, text_objects: text_objects
28
38
  end
29
39
  end
30
- attr_reader :text_objects
31
40
 
32
41
  # @return [TrueClass, FalseClass] will update?
33
42
  def will_update
34
- @will_update || @text_objects.map(&:will_update).any?
35
- end
36
-
37
- # @param [Proc] block
38
- # @return [Enumerator]
39
- def each(&block)
40
- return enum_for(:each) unless block
41
-
42
- @text_objects.each(&block)
43
- end
44
-
45
- # @param [String, RichTextObject] to
46
- # @return [NotionRubyMapping::RichTextObject] added RichTextObject
47
- def <<(to)
48
- @will_update = true
49
- rto = RichTextObject.text_object(to)
50
- @text_objects << rto
51
- rto
52
- end
53
-
54
- # @param [Numeric] index index number
55
- # @return [RichTextObject] selected RichTextObject
56
- def [](index)
57
- @text_objects[index]
58
- end
59
-
60
- # @param [Numeric] index
61
- # @return [NotionRubyMapping::RichTextObject] removed RichTextObject
62
- def delete_at(index)
63
- @will_update = true
64
- @text_objects[index]
65
- end
66
-
67
- # @return [String] full_text
68
- def full_text
69
- map(&:text).join ""
43
+ @will_update || page? && @text_objects.will_update
70
44
  end
71
45
  end
72
46
  end
@@ -5,14 +5,25 @@ module NotionRubyMapping
5
5
  class TitleProperty < TextProperty
6
6
  TYPE = "title"
7
7
 
8
+ ### Not public announced methods
9
+
10
+ ## Common methods
11
+
8
12
  # @param [Hash] json
9
13
  def update_from_json(json)
10
14
  @will_update = false
11
- @text_objects = json["title"].map { |to| RichTextObject.create_from_json to }
15
+ if database?
16
+ @json = json || {}
17
+ else
18
+ @text_objects = RichTextArray.new "rich_text", json: json["title"]
19
+ end
12
20
  end
13
21
 
22
+ ## Page property only methods
23
+
14
24
  # @return [Hash] created json
15
25
  def property_values_json
26
+ assert_page_property __method__
16
27
  text_json = map(&:property_values_json)
17
28
  {
18
29
  @name => {
@@ -9,28 +9,39 @@ module NotionRubyMapping
9
9
  include IsEmptyIsNotEmpty
10
10
  TYPE = "url"
11
11
 
12
+ ### Public announced methods
13
+
14
+ ## Common methods
15
+
16
+ # @return [String, Hash, nil] url (Page), {} (Database)
17
+ def url
18
+ @json
19
+ end
20
+
21
+ ## Page property only methods
22
+
23
+ def url=(url)
24
+ @will_update = true
25
+ @json = url
26
+ end
27
+
28
+ ### Not public announced methods
29
+
30
+ ## Common methods
31
+
12
32
  # @param [String] name Property name
13
33
  # @param [String] url url value (optional)
14
- def initialize(name, will_update: false, url: nil)
15
- super name, will_update: will_update
16
- @url = url
34
+ def initialize(name, will_update: false, base_type: :page, json: nil)
35
+ super name, will_update: will_update, base_type: base_type
36
+ @json = json || (database? ? {} : nil)
17
37
  end
18
- attr_reader :url
19
38
 
20
- # @param [Hash] json
21
- def update_from_json(json)
22
- @will_update = false
23
- @url = json["url"]
24
- end
39
+ ## Page property only methods
25
40
 
26
41
  # @return [Hash]
27
42
  def property_values_json
28
- {@name => {"url" => @url, "type" => "url"}}
29
- end
30
-
31
- def url=(url)
32
- @will_update = true
33
- @url = url
43
+ assert_page_property __method__
44
+ {@name => {"url" => @json, "type" => "url"}}
34
45
  end
35
46
  end
36
47
  end
@@ -14,11 +14,11 @@ module NotionRubyMapping
14
14
 
15
15
  # @param [UserObject, String] uo
16
16
  # @return [UserObject] self or created UserObject
17
- def self.user_object(uo)
18
- if uo.is_a? UserObject
19
- uo
17
+ def self.user_object(user_id_or_user)
18
+ if user_id_or_user.is_a? UserObject
19
+ user_id_or_user
20
20
  else
21
- UserObject.new user_id: uo
21
+ UserObject.new user_id: user_id_or_user
22
22
  end
23
23
  end
24
24
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
- VERSION = "0.2.3"
4
+ VERSION = "0.3.0"
5
5
  NOTION_VERSION = "2022-02-22"
6
6
  end
@@ -6,6 +6,7 @@ require "yaml"
6
6
  url_property email_property phone_number_property number_property checkbox_property select_property multi_property
7
7
  multi_select_property date_base_property date_property created_time_property last_edited_time_property
8
8
  people_property created_by_property last_edited_by_property files_property relation_property formula_property
9
- rollup_property query payload property_cache rich_text_object text_object mention_object user_object].each do |k|
9
+ rollup_property query payload property_cache rich_text_object text_object mention_object user_object
10
+ rich_text_array].each do |k|
10
11
  require_relative "notion_ruby_mapping/#{k}"
11
12
  end