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
@@ -5,64 +5,89 @@ module NotionRubyMapping
5
5
  class DateProperty < DateBaseProperty
6
6
  TYPE = "date"
7
7
 
8
+ ### Public announced methods
9
+
10
+ ## Common methods
11
+
12
+ # @return [Hash]
13
+ def date
14
+ @json
15
+ end
16
+
17
+ ## Page property only methods
18
+
19
+ # @return [Date, Time, DateTime, String]
20
+ def end_date
21
+ @json["end"]
22
+ end
23
+
24
+ # @param [Date, Time, DateTime, String] edt
25
+ def end_date=(edt)
26
+ @will_update = true
27
+ sdt = start_date
28
+ edt = nil if sdt.class != edt.class || sdt > edt
29
+ @json["end"] = edt
30
+ end
31
+
32
+ # @return [Date, Time, DateTime, String]
33
+ def start_date
34
+ @json["start"]
35
+ end
36
+
37
+ # @param [Date, Time, DateTime, String] sdt
38
+ def start_date=(sdt)
39
+ @will_update = true
40
+ edt = end_date
41
+ @json["end"] = nil if sdt.class != edt.class || sdt > edt
42
+ @json["start"] = sdt
43
+ end
44
+
45
+ # @return [String]
46
+ def time_zone
47
+ @json["time_zone"]
48
+ end
49
+
50
+ # @param [String] tzone
51
+ def time_zone=(tzone)
52
+ @will_update = true
53
+ @json["time_zone"] = tzone
54
+ end
55
+
56
+ ### Not public announced methods
57
+
58
+ ## Common methods
59
+
8
60
  # @param [String] name
9
61
  # @param [Hash] json
10
62
  # @param [Date, Time, DateTime, String, nil] start_date
11
63
  # @param [Date, Time, DateTime, String, nil] end_date
12
64
  # @param [String, nil] time_zone
13
- def initialize(name, will_update: false, json: nil, start_date: nil, end_date: nil, time_zone: nil)
14
- super name, will_update: will_update
15
- @start_date = start_date || json && json["start"]
16
- @end_date = end_date || json && json["end"]
17
- @time_zone = time_zone || json && json["time_zone"]
65
+ def initialize(name, will_update: false, base_type: :page, json: nil, start_date: nil, end_date: nil, time_zone: nil)
66
+ super name, will_update: will_update, base_type: base_type
67
+ @json = json || {}
68
+ return if database?
69
+
70
+ @json = json || {}
71
+ @json["start"] = start_date if start_date
72
+ @json["end"] = end_date if end_date
73
+ @json["time_zone"] = time_zone if time_zone
18
74
  end
19
75
 
76
+ ## Page property only methods
77
+
20
78
  # @return [Hash] created json
21
79
  def property_values_json
80
+ assert_page_property __method__
22
81
  {
23
82
  @name => {
24
83
  "type" => "date",
25
84
  "date" => {
26
- "start" => value_str(@start_date),
27
- "end" => value_str(@end_date),
28
- "time_zone" => @time_zone,
85
+ "start" => value_str(@json["start"]),
86
+ "end" => value_str(@json["end"]),
87
+ "time_zone" => @json["time_zone"],
29
88
  },
30
89
  },
31
90
  }
32
91
  end
33
-
34
- # @param [Date, Time, DateTime, String] start_date
35
- # @return [Date, Time, DateTime, String]
36
- def start_date=(start_date)
37
- @will_update = true
38
- @start_date = start_date
39
- @end_date = nil if @start_date.class != @end_date.class || @start_date > @end_date
40
- @start_date
41
- end
42
-
43
- # @param [Date, Time, DateTime, String] end_date
44
- # @return [Date, Time, DateTime, String]
45
- def end_date=(end_date)
46
- @will_update = true
47
- @end_date = end_date
48
- @end_date = nil if @start_date.class != @end_date.class || @start_date > @end_date
49
- @end_date
50
- end
51
-
52
- # @param [String] time_zone
53
- # @return [Array, nil] settled array
54
- def time_zone=(time_zone)
55
- @will_update = true
56
- @time_zone = time_zone
57
- end
58
-
59
- # @param [Hash] json
60
- def update_from_json(json)
61
- @will_update = false
62
- jd = json["date"]
63
- @start_date = jd["start"]
64
- @end_date = jd["end"]
65
- @time_zone = jd["time_zone"]
66
- end
67
92
  end
68
93
  end
@@ -9,28 +9,38 @@ module NotionRubyMapping
9
9
  include IsEmptyIsNotEmpty
10
10
  TYPE = "email"
11
11
 
12
- # @param [String] name Property name
13
- # @param [String] email email value (optional)
14
- def initialize(name, will_update: false, email: nil)
15
- super name, will_update: will_update
16
- @email = email
17
- end
18
- attr_reader :email
12
+ ### Public announced methods
19
13
 
20
- # @param [Hash] json
21
- def update_from_json(json)
22
- @will_update = false
23
- @email = json["email"]
24
- end
14
+ ## Common methods
25
15
 
26
- # @return [Hash]
27
- def property_values_json
28
- {@name => {"email" => @email, "type" => "email"}}
16
+ # @return [String, Hash]
17
+ def email
18
+ @json
29
19
  end
30
20
 
21
+ ## Page property only methods
22
+
23
+ # @param [String] str
31
24
  def email=(email)
32
25
  @will_update = true
33
- @email = email
26
+ @json = email
27
+ end
28
+
29
+ ### Not public announced methods
30
+
31
+ ## Common methods
32
+
33
+ # @param [String] name Property name
34
+ # @param [String] email email value (optional)
35
+ def initialize(name, will_update: false, base_type: :page, json: nil)
36
+ super name, will_update: will_update, base_type: base_type
37
+ @json = json || {}
38
+ end
39
+
40
+ # @return [Hash]
41
+ def property_values_json
42
+ assert_page_property __method__
43
+ {@name => {"email" => @json, "type" => "email"}}
34
44
  end
35
45
  end
36
46
  end
@@ -6,13 +6,49 @@ module NotionRubyMapping
6
6
  include IsEmptyIsNotEmpty
7
7
  TYPE = "files"
8
8
 
9
+ ### Public announced methods
10
+
11
+ ## Common methods
12
+
13
+ # @return [Hash]
14
+ def files
15
+ @json
16
+ end
17
+
18
+ ## Page property only methods
19
+
20
+ def files=(files = [])
21
+ @will_update = true
22
+ @json = Array(files).map { |url| url_to_hash url }
23
+ end
24
+
25
+ ### Not public announced methods
26
+
27
+ ## Common methods
28
+
9
29
  # @param [String] name Property name
10
30
  # @param [String] files files value (optional)
11
- def initialize(name, will_update: false, json: nil, files: [])
12
- super name, will_update: will_update
13
- @files = json || Array(files).map { |url| url_to_hash url } || []
31
+ def initialize(name, will_update: false, base_type: :page, json: nil, files: [])
32
+ super name, will_update: will_update, base_type: base_type
33
+ if database?
34
+ @json = json || {}
35
+ else
36
+ @json = json || []
37
+ @json = Array(files).map { |url| url_to_hash url } unless files.empty?
38
+ end
14
39
  end
15
- attr_reader :files
40
+
41
+ # @return [Hash]
42
+ def property_values_json
43
+ assert_page_property __method__
44
+ if @json.map { |f| f["type"] }.include? "file"
45
+ {}
46
+ else
47
+ {@name => {"files" => @json, "type" => "files"}}
48
+ end
49
+ end
50
+
51
+ protected
16
52
 
17
53
  # @param [String] url
18
54
  # @return [Hash]
@@ -25,25 +61,5 @@ module NotionRubyMapping
25
61
  },
26
62
  }
27
63
  end
28
-
29
- # @param [Hash] json
30
- def update_from_json(json)
31
- @will_update = false
32
- @files = json["files"]
33
- end
34
-
35
- # @return [Hash]
36
- def property_values_json
37
- if @files.map { |f| f["type"] }.include? "file"
38
- {}
39
- else
40
- {@name => {"files" => @files, "type" => "files"}}
41
- end
42
- end
43
-
44
- def files=(files = [])
45
- @will_update = true
46
- @files = Array(files).map { |url| url_to_hash url } || []
47
- end
48
64
  end
49
65
  end
@@ -8,22 +8,63 @@ module NotionRubyMapping
8
8
  include GreaterThanLessThan
9
9
  TYPE = "formula"
10
10
 
11
+ ### Public announced methods
12
+
13
+ ## Common methods
14
+
15
+ # @return [Hash]
16
+ def formula
17
+ @json
18
+ end
19
+
20
+ ## Database property only methods
21
+
22
+ def formula_expression
23
+ assert_database_property __method__
24
+ @json["expression"]
25
+ end
26
+
27
+ # @param [String] formula
28
+ def formula_expression=(f_e)
29
+ assert_database_property __method__
30
+ @will_update = true
31
+ @json["expression"] = f_e
32
+ end
33
+
34
+ ### Not public announced methods
35
+
36
+ ## Common methods
37
+
11
38
  # @param [String] name
12
39
  # @param [Hash] json
13
- def initialize(name, json: nil)
14
- super name, will_update: false
15
- @json = json
40
+ def initialize(name, will_update: false, base_type: :page, json: nil, formula: nil)
41
+ super name, will_update: will_update, base_type: base_type
42
+ @json = json || {}
43
+ return unless database?
44
+
45
+ @json["expression"] = formula if formula
16
46
  end
17
47
 
18
- # @param [Hash] json
19
- def update_from_json(json)
20
- @will_update = false
21
- @json = json["formula"]
48
+ ## Database property only methods
49
+
50
+ # @return [Hash]
51
+ def update_property_schema_json
52
+ assert_database_property __method__
53
+ ans = super
54
+ return ans if ans != {} || !@will_update
55
+
56
+ ans[@name] ||= {}
57
+ ans[@name]["formula"] = @json
58
+ ans
22
59
  end
23
60
 
24
- # @return [Hash] {} created_time cannot be updated
25
- def property_values_json
26
- {}
61
+ protected
62
+
63
+ ## Database property only methods
64
+
65
+ # @return [Hash]
66
+ def property_schema_json_sub
67
+ {"expression" => formula_expression}
27
68
  end
28
69
  end
29
70
  end
@@ -5,24 +5,37 @@ module NotionRubyMapping
5
5
  class LastEditedByProperty < MultiProperty
6
6
  TYPE = "last_edited_by"
7
7
 
8
+ ### Public announced methods
9
+
10
+ ## Common methods
11
+
12
+ # @return [NotionRubyMapping::UserObject, Hash]
13
+ def last_edited_by
14
+ @json
15
+ end
16
+
17
+ ### Not public announced methods
18
+
19
+ ## Common methods
20
+
8
21
  # @param [String] name Property name
9
22
  # @param [String] user_id user_id (optional)
10
23
  # @param [Hash] json json (optional)
11
- def initialize(name, user_id: nil, json: nil)
12
- super name, will_update: false
13
- @user = UserObject.new user_id: user_id, json: json
24
+ def initialize(name, will_update: false, base_type: :page, json: nil, user_id: nil)
25
+ super name, will_update: will_update, base_type: base_type
26
+ @json = if database?
27
+ json || {}
28
+ else
29
+ UserObject.new user_id: user_id, json: json
30
+ end
14
31
  end
15
32
  attr_reader :user
16
33
 
17
34
  # @param [Hash] json
18
35
  def update_from_json(json)
19
36
  @will_update = false
20
- @user = UserObject.new json: json["last_edited_by"]
21
- end
22
-
23
- # @return [Hash] {} created_time cannot be updated
24
- def property_values_json
25
- {}
37
+ leb = json["last_edited_by"]
38
+ @json = database? ? leb : UserObject.new(json: leb)
26
39
  end
27
40
  end
28
41
  end
@@ -5,23 +5,25 @@ module NotionRubyMapping
5
5
  class LastEditedTimeProperty < DateBaseProperty
6
6
  TYPE = "last_edited_time"
7
7
 
8
- # @param [String] name Property name
9
- # @param [String] last_edited_time last_edited_time value (optional)
10
- def initialize(name, last_edited_time: nil)
11
- super name, will_update: false
12
- @last_edited_time = last_edited_time
13
- end
14
- attr_reader :last_edited_time
8
+ ### Public announced methods
9
+
10
+ ## Common methods
15
11
 
16
- # @param [Hash] json
17
- def update_from_json(json)
18
- @will_update = false
19
- @last_edited_time = json["last_edited_time"]
12
+ # @return [Date, Hash]
13
+ def last_edited_time
14
+ @json
20
15
  end
21
16
 
22
- # @return [Hash] {} created_time cannot be updated
23
- def property_values_json
24
- {}
17
+ ### Not public announced methods
18
+
19
+ ## Common methods
20
+
21
+ # @param [String] name Property name
22
+ # @param [String] json last_edited_time value (optional)
23
+ def initialize(name, will_update: false, base_type: :page, json: nil)
24
+ super name, will_update: will_update, base_type: base_type
25
+ @json = json
26
+ @json ||= {} if database?
25
27
  end
26
28
  end
27
29
  end
@@ -8,6 +8,7 @@ module NotionRubyMapping
8
8
  def initialize(json: nil, id: nil, database: nil, query: nil)
9
9
  super(json: json, id: id)
10
10
  @has_more = @json["has_more"]
11
+ @load_all_contents = !@has_more
11
12
  @database = database
12
13
  @query = query
13
14
  @index = 0
@@ -15,39 +16,46 @@ module NotionRubyMapping
15
16
  end
16
17
  attr_reader :has_more
17
18
 
19
+ ### Public announced methods
20
+
18
21
  def each
19
22
  return enum_for(:each) unless block_given?
20
23
 
21
- unless @has_content # re-exec
22
- @query.start_cursor = nil
23
- @json = @database.query_database @query
24
- @index = 0
25
- @has_more = @json["has_more"]
26
- @has_content = true
27
- end
28
-
29
- while @has_content
30
- if @index < results.length
31
- object = Base.create_from_json(results[@index])
32
- @index += 1
33
- yield object
34
- elsif @has_more
35
- if @database
36
- @query.start_cursor = @json["next_cursor"]
24
+ if @database
25
+ unless @has_content # re-exec
26
+ unless @load_all_contents
27
+ @query.start_cursor = nil
37
28
  @json = @database.query_database @query
38
- @index = 0
39
29
  @has_more = @json["has_more"]
30
+ end
31
+ @index = 0
32
+ @has_content = true
33
+ end
34
+
35
+ while @has_content
36
+ if @index < results.length
37
+ object = Base.create_from_json(results[@index])
38
+ @index += 1
39
+ yield object
40
+ elsif @has_more
41
+ if @database
42
+ @query.start_cursor = @json["next_cursor"]
43
+ @json = @database.query_database @query
44
+ @index = 0
45
+ @has_more = @json["has_more"]
46
+ else
47
+ @has_content = false
48
+ end
40
49
  else
41
50
  @has_content = false
42
51
  end
43
- else
44
- @has_content = false
45
52
  end
46
53
  end
47
54
  end
48
55
 
49
56
  private
50
57
 
58
+ # @return [Hash]
51
59
  def results
52
60
  @json["results"]
53
61
  end
@@ -41,8 +41,33 @@ module NotionRubyMapping
41
41
  "type" => "date",
42
42
  "date" => @options.slice("start", "end", "time_zone"),
43
43
  }
44
+ elsif @options.key? "template_mention"
45
+ sub = case @options["template_mention"]
46
+ when "today"
47
+ @options["plain_text"] = "@Today"
48
+ {
49
+ "type" => "template_mention_date",
50
+ "template_mention_date" => "today",
51
+ }
52
+ when "now"
53
+ @options["plain_text"] = "@Now"
54
+ {
55
+ "type" => "template_mention_date",
56
+ "template_mention_date" => "now",
57
+ }
58
+ else
59
+ @options["plain_text"] = "@Me"
60
+ {
61
+ "type" => "template_mention_user",
62
+ "template_mention_user" => "me",
63
+ }
64
+ end
65
+ {
66
+ "type" => "template_mention",
67
+ "template_mention" => sub,
68
+ }
44
69
  else
45
- raise StandardError, "Irregular mention type"
70
+ raise StandardError, "Irregular mention type: #{@options.keys}"
46
71
  end
47
72
  end
48
73
  end
@@ -7,41 +7,103 @@ module NotionRubyMapping
7
7
  include IsEmptyIsNotEmpty
8
8
  TYPE = "multi_select"
9
9
 
10
+ ### Public announced methods
11
+
12
+ ## Common methods
13
+
14
+ # @return [Array, Hash]
15
+ def multi_select
16
+ @json
17
+ end
18
+
19
+ # @return [Array]
20
+ def multi_select_names
21
+ mshs = @base_type == :page ? @json : @json["options"]
22
+ mshs.map { |h| h["name"] }
23
+ end
24
+
25
+ ## Database property only methods
26
+
27
+ # @param [String] name
28
+ # @param [String] color
29
+ # @return [Array] added array
30
+ def add_multi_select_options(name:, color:)
31
+ edit_multi_select_options << {"name" => name, "color" => color}
32
+ end
33
+
34
+ # @return [Array] copyed multi select options
35
+ def edit_multi_select_options
36
+ assert_database_property __method__
37
+ @will_update = true
38
+ @json["options"] ||= []
39
+ end
40
+
41
+ # @return [Array]
42
+ def multi_select_options
43
+ assert_database_property __method__
44
+ @json["options"] || []
45
+ end
46
+
47
+ ## Page property only methods
48
+
49
+ # @param [Hash] multi_select
50
+ # @return [Array, nil] settled array
51
+ def multi_select=(multi_select)
52
+ @will_update = true
53
+ @json = multi_select ? Array(multi_select).map { |ms| {"name" => ms} } : []
54
+ end
55
+
56
+ ### Not public announced methods
57
+
58
+ ## Common methods
59
+
10
60
  # @param [String] name
11
61
  # @param [Hash] json
12
- # @param [Array] multi_select
13
- def initialize(name, will_update: false, json: nil, multi_select: nil)
14
- super name, will_update: will_update
15
- @multi_select = if multi_select
16
- Array(multi_select)
17
- elsif json.is_a? Array
18
- json.map { |ms| ms["name"] }
19
- else
20
- []
21
- end
62
+ # @param [Array<String>, String] multi_select
63
+ def initialize(name, will_update: false, base_type: :page, json: nil, multi_select: nil)
64
+ super name, will_update: will_update, base_type: base_type
65
+ if database?
66
+ @json = json || {"options" => []}
67
+ else
68
+ @json = json || []
69
+ @json = Array(multi_select).map { |ms| {"name" => ms} } unless multi_select.nil?
70
+ end
71
+ end
72
+
73
+ ## Database property only methods
74
+
75
+ # @return [Hash]
76
+ def update_property_schema_json
77
+ assert_database_property __method__
78
+ ans = super
79
+ return ans if ans != {} || !@will_update
80
+
81
+ ans[@name] ||= {}
82
+ ans[@name]["multi_select"] ||= {}
83
+ ans[@name]["multi_select"]["options"] = @json["options"]
84
+ ans
22
85
  end
23
86
 
87
+ ## Page property only methods
88
+
24
89
  # @return [Hash] created json
25
90
  def property_values_json
91
+ assert_page_property __method__
26
92
  {
27
93
  @name => {
28
94
  "type" => "multi_select",
29
- "multi_select" => @multi_select.map { |v| {"name" => v} },
95
+ "multi_select" => @json,
30
96
  },
31
97
  }
32
98
  end
33
99
 
34
- # @param [Hash] multi_select
35
- # @return [Array, nil] settled array
36
- def multi_select=(multi_select)
37
- @will_update = true
38
- @multi_select = multi_select ? Array(multi_select) : []
39
- end
100
+ protected
40
101
 
41
- # @param [Hash] json
42
- def update_from_json(json)
43
- @will_update = false
44
- @multi_select = json["multi_select"].map { |ms| ms["name"] }
102
+ ## Database property only methods
103
+
104
+ # @return [Hash]
105
+ def property_schema_json_sub
106
+ {"options" => edit_multi_select_options}
45
107
  end
46
108
  end
47
109
  end