notion_ruby_mapping 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +500 -141
  3. data/env.yml.sample +2 -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 +130 -73
  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 +78 -8
  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 +27 -19
  19. data/lib/notion_ruby_mapping/mention_object.rb +26 -1
  20. data/lib/notion_ruby_mapping/multi_select_property.rb +83 -21
  21. data/lib/notion_ruby_mapping/notion_cache.rb +25 -10
  22. data/lib/notion_ruby_mapping/number_property.rb +71 -10
  23. data/lib/notion_ruby_mapping/page.rb +33 -5
  24. data/lib/notion_ruby_mapping/payload.rb +23 -5
  25. data/lib/notion_ruby_mapping/people_property.rb +47 -19
  26. data/lib/notion_ruby_mapping/phone_number_property.rb +26 -15
  27. data/lib/notion_ruby_mapping/property.rb +140 -49
  28. data/lib/notion_ruby_mapping/property_cache.rb +31 -3
  29. data/lib/notion_ruby_mapping/query.rb +10 -0
  30. data/lib/notion_ruby_mapping/relation_property.rb +74 -21
  31. data/lib/notion_ruby_mapping/rich_text_array.rb +81 -0
  32. data/lib/notion_ruby_mapping/rich_text_object.rb +58 -1
  33. data/lib/notion_ruby_mapping/rich_text_property.rb +12 -1
  34. data/lib/notion_ruby_mapping/rollup_property.rb +82 -10
  35. data/lib/notion_ruby_mapping/select_property.rb +88 -13
  36. data/lib/notion_ruby_mapping/text_object.rb +0 -51
  37. data/lib/notion_ruby_mapping/text_property.rb +19 -45
  38. data/lib/notion_ruby_mapping/title_property.rb +12 -1
  39. data/lib/notion_ruby_mapping/url_property.rb +26 -15
  40. data/lib/notion_ruby_mapping/user_object.rb +4 -4
  41. data/lib/notion_ruby_mapping/version.rb +1 -1
  42. data/lib/notion_ruby_mapping.rb +2 -1
  43. data/tools/an +103 -0
  44. metadata +7 -2
@@ -4,16 +4,17 @@ module NotionRubyMapping
4
4
  # PropertyCache class
5
5
  class PropertyCache
6
6
  include Enumerable
7
- def initialize(json = {})
7
+ def initialize(json = {}, base_type: :page)
8
8
  @properties = {}
9
9
  @json = json
10
+ @base_type = base_type
10
11
  end
11
12
  attr_writer :json
12
13
 
13
14
  # @param [String] key
14
15
  # @return [Property] Property for key
15
16
  def [](key)
16
- @properties[key] ||= Property.create_from_json key, @json[key]
17
+ @properties[key] ||= Property.create_from_json key, @json[key], @base_type
17
18
  end
18
19
 
19
20
  # @param [Array] key
@@ -44,7 +45,14 @@ module NotionRubyMapping
44
45
  self
45
46
  end
46
47
 
47
- # @return [Hash] created json
48
+
49
+ def clear_will_update
50
+ @properties.each do |_, property|
51
+ property.clear_will_update
52
+ end
53
+ end
54
+
55
+ # @return [Hash] created property values json
48
56
  def property_values_json
49
57
  @properties.each_with_object({}) do |(_, property), ans|
50
58
  if property.will_update
@@ -53,5 +61,25 @@ module NotionRubyMapping
53
61
  end
54
62
  end
55
63
  end
64
+
65
+ # @return [Hash] created property schema json
66
+ def property_schema_json
67
+ @properties.each_with_object({}) do |(_, property), ans|
68
+ if property.will_update
69
+ ans["properties"] ||= {}
70
+ ans["properties"].merge! property.property_schema_json
71
+ end
72
+ end
73
+ end
74
+
75
+ # @return [Hash] created update property schema json
76
+ def update_property_schema_json
77
+ @properties.each_with_object({}) do |(_, property), ans|
78
+ if property.will_update
79
+ ans["properties"] ||= {}
80
+ ans["properties"].merge! property.update_property_schema_json
81
+ end
82
+ end
83
+ end
56
84
  end
57
85
  end
@@ -49,5 +49,15 @@ module NotionRubyMapping
49
49
  @sort << {key => property.name, "direction" => "descending"}
50
50
  self
51
51
  end
52
+
53
+ # @return [Hash]
54
+ def query_json
55
+ parameters = {}
56
+ parameters[:filter] = filter unless filter.empty?
57
+ parameters[:sorts] = sort unless sort.empty?
58
+ parameters[:start_cursor] = start_cursor if start_cursor
59
+ parameters[:page_size] = page_size if page_size
60
+ parameters
61
+ end
52
62
  end
53
63
  end
@@ -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"]
@@ -32,8 +33,16 @@ module NotionRubyMapping
32
33
  MentionObject.new options.merge({"database_id" => mention["database"]["id"]})
33
34
  when "date"
34
35
  MentionObject.new options.merge(mention["date"].slice("start", "end", "time_zone"))
36
+ when "template_mention"
37
+ template_mention = mention["template_mention"]
38
+ case template_mention["type"]
39
+ when "template_mention_date"
40
+ MentionObject.new options.merge({"template_mention" => template_mention["template_mention_date"]})
41
+ else
42
+ MentionObject.new options.merge({"template_mention" => template_mention["template_mention_user"]})
43
+ end
35
44
  else
36
- raise StandardError, json
45
+ raise StandardError, "Unknown mention type: #{mention["type"]}"
37
46
  end
38
47
  else
39
48
  raise StandardError, json
@@ -60,6 +69,54 @@ module NotionRubyMapping
60
69
  }.merge annotations_json
61
70
  end
62
71
 
72
+ # @param [String, RichTextObject] value
73
+ # @return [String] input text
74
+ def plain_text=(value)
75
+ text(value)
76
+ end
77
+
78
+ # @param [Boolean] flag
79
+ # @return [Boolean] input flag
80
+ def bold=(flag)
81
+ @will_update = true
82
+ @options["bold"] = flag
83
+ end
84
+
85
+ # @param [Boolean] flag
86
+ # @return [Boolean] input flag
87
+ def italic=(flag)
88
+ @will_update = true
89
+ @options["italic"] = flag
90
+ end
91
+
92
+ # @param [Boolean] flag
93
+ # @return [Boolean] input flag
94
+ def strikethrough=(flag)
95
+ @will_update = true
96
+ @options["strikethrough"] = flag
97
+ end
98
+
99
+ # @param [Boolean] flag
100
+ # @return [Boolean] input flag
101
+ def underline=(flag)
102
+ @will_update = true
103
+ @options["underline"] = flag
104
+ end
105
+
106
+ # @param [Boolean] flag
107
+ # @return [Boolean] input flag
108
+ def code=(flag)
109
+ @will_update = true
110
+ @options["code"] = flag
111
+ end
112
+
113
+ # @param [String] color
114
+ # @return [String] input color
115
+ def color=(color)
116
+ @will_update = true
117
+ @options["color"] = color
118
+ end
119
+
63
120
  protected
64
121
 
65
122
  # @return [Hash] options
@@ -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,66 +14,15 @@ 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
21
20
  @options = value.options
22
21
  @text = value.text
23
22
  else
24
- p value
25
23
  @text = value
26
24
  @options["plain_text"] = value
27
25
  end
28
- self
29
- end
30
-
31
- # @param [String, RichTextObject] value
32
- # @return [String] input text
33
- def plain_text=(value)
34
- text(value)
35
- end
36
-
37
- # @param [Boolean] flag
38
- # @return [Boolean] input flag
39
- def bold=(flag)
40
- @will_update = true
41
- @options["bold"] = flag
42
- end
43
-
44
- # @param [Boolean] flag
45
- # @return [Boolean] input flag
46
- def italic=(flag)
47
- @will_update = true
48
- @options["italic"] = flag
49
- end
50
-
51
- # @param [Boolean] flag
52
- # @return [Boolean] input flag
53
- def strikethrough=(flag)
54
- @will_update = true
55
- @options["strikethrough"] = flag
56
- end
57
-
58
- # @param [Boolean] flag
59
- # @return [Boolean] input flag
60
- def underline=(flag)
61
- @will_update = true
62
- @options["underline"] = flag
63
- end
64
-
65
- # @param [Boolean] flag
66
- # @return [Boolean] input flag
67
- def code=(flag)
68
- @will_update = true
69
- @options["code"] = flag
70
- end
71
-
72
- # @param [String] color
73
- # @return [String] input color
74
- def color=(color)
75
- @will_update = true
76
- @options["color"] = color
77
26
  end
78
27
 
79
28
  protected