notion_ruby_mapping 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
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
data/README.md CHANGED
@@ -6,15 +6,10 @@ Development note is here. → [Idea note of "notion_ruby_mapping"](https://www.n
6
6
 
7
7
  ## Table of Contents
8
8
 
9
- - [notion_ruby_mapping](#notion_ruby_mapping)
9
+ - [notion\_ruby\_mapping](#notion_ruby_mapping)
10
10
  - [Table of Contents](#table-of-contents)
11
11
  - [1. Installation](#1-installation)
12
12
  - [2. Example code](#2-example-code)
13
- - [2.1 Set icon to all icon unsettled pages](#21-set-icon-to-all-icon-unsettled-pages)
14
- - [2.2 Renumbering pages](#22-renumbering-pages)
15
- - [2.3 Change title](#23-change-title)
16
- - [Pattern 1 (replace existing text partially)](#pattern-1-replace-existing-text-partially)
17
- - [Pattern 2 (replace all text)](#pattern-2-replace-all-text)
18
13
  - [3. Preparation](#3-preparation)
19
14
  - [3.1 Create a New Integration](#31-create-a-new-integration)
20
15
  - [3.2 Create client](#32-create-client)
@@ -29,8 +24,13 @@ Development note is here. → [Idea note of "notion_ruby_mapping"](https://www.n
29
24
  - [4.2.2 Query a database](#422-query-a-database)
30
25
  - [4.2.2.1 Complex conditions](#4221-complex-conditions)
31
26
  - [4.2.2.2 Sort criteria](#4222-sort-criteria)
27
+ - [4.2.2.3 Dry run sample](#4223-dry-run-sample)
32
28
  - [4.2.3 Create child page](#423-create-child-page)
33
- - [4.2.4 Update database](#424-update-database)
29
+ - [4.2.4 Create database](#424-create-database)
30
+ - [4.2.5 Update database properties](#425-update-database-properties)
31
+ - [4.2.6 Add a database property](#426-add-a-database-property)
32
+ - [4.2.7 Rename a database property](#427-rename-a-database-property)
33
+ - [4.2.8 Remove database properties](#428-remove-database-properties)
34
34
  - [4.3 List class](#43-list-class)
35
35
  - [4.4 Block class](#44-block-class)
36
36
  - [4.5 Property classes](#45-property-classes)
@@ -47,14 +47,30 @@ Development note is here. → [Idea note of "notion_ruby_mapping"](https://www.n
47
47
  - [4.5.3.7 PeopleProperty](#4537-peopleproperty)
48
48
  - [4.5.3.8 TitleProperty, RichTextProperty](#4538-titleproperty-richtextproperty)
49
49
  - [4.5.3.9 CheckboxProperty](#4539-checkboxproperty)
50
- - [4.5.3.10 EmailProperty](#45310-emailproperty)
51
- - [4.5.3.11 FilesProperty](#45311-filesproperty)
52
- - [4.5.3.12 RelationProperty](#45312-relationproperty)
53
- - [5. XXXObjects](#5-xxxobjects)
50
+ - [4.5.3.10 FilesProperty](#45310-filesproperty)
51
+ - [4.5.3.11 RelationProperty](#45311-relationproperty)
52
+ - [4.5.4 create or update values for Database properties](#454-create-or-update-values-for-database-properties)
53
+ - [4.5.4.1 NumberProperty](#4541-numberproperty)
54
+ - [4.5.4.2 SelectProperty](#4542-selectproperty)
55
+ - [4.5.4.3 MultiSelectProperty](#4543-multiselectproperty)
56
+ - [4.5.4.4 DateProperty](#4544-dateproperty)
57
+ - [4.5.4.4 UrlProperty](#4544-urlproperty)
58
+ - [4.5.4.5 EmailProperty](#4545-emailproperty)
59
+ - [4.5.4.6 PhoneNumberProperty](#4546-phonenumberproperty)
60
+ - [4.5.4.7 PeopleProperty](#4547-peopleproperty)
61
+ - [4.5.4.8 TitleProperty, RichTextProperty](#4548-titleproperty-richtextproperty)
62
+ - [4.5.4.9 CheckboxProperty](#4549-checkboxproperty)
63
+ - [4.5.4.10 FilesProperty](#45410-filesproperty)
64
+ - [4.5.4.11 RelationProperty](#45411-relationproperty)
65
+ - [4.5.4.12 RollupProperty](#45412-rollupproperty)
66
+ - [5. XXXObjects and RichTextArray](#5-xxxobjects-and-richtextarray)
54
67
  - [5.1 RichTextObject](#51-richtextobject)
55
68
  - [5.2 TextObject](#52-textobject)
56
69
  - [5.3 MentionObject](#53-mentionobject)
57
70
  - [5.4 UserObject](#54-userobject)
71
+ - [5.5 RichTextArray](#55-richtextarray)
72
+ - [5.5.1 Constructor for RichTextArray](#551-constructor-for-richtextarray)
73
+ - [5.5.2 Instance methods](#552-instance-methods)
58
74
  - [6. ChangeLog](#6-changelog)
59
75
  - [7. Contributing](#7-contributing)
60
76
  - [8. License](#8-license)
@@ -83,76 +99,9 @@ gem install notion_ruby_mapping
83
99
 
84
100
  ## 2. Example code
85
101
 
86
- ### 2.1 Set icon to all icon unsettled pages
87
-
88
- The following code sets a "💿" icon on all unset pages in the database.
89
-
90
- ```Ruby
91
- require "notion_ruby_mapping"
92
-
93
- include NotionRubyMapping
94
-
95
- token = ENV["NOTION_API_TOKEN"]
96
- database_id = ENV["DATABASE_ID"]
97
-
98
- NotionCache.instance.create_client token
99
-
100
- db = Database.new id: database_id
101
- db.query_database.each do |page|
102
- unless page.icon
103
- page.set_icon(emoji: "💿")
104
- page.save
105
- p page.id
106
- end
107
- end
108
- ```
109
-
110
- |Before execution|After execution|
111
- |---|---|
112
- |![Before execution](images/pre_set_icon.png)|![After execution](images/post_set_icon.png)|
113
-
114
- ### 2.2 Renumbering pages
115
-
116
- The following code sets serial numbers to the pages whose title is not empty in ascending order of titles.
117
-
118
- ```Ruby
119
- db = Database.new id: database_id, assign: [RichTextProperty, "TextTitle"]
120
- tp = db.properties["TextTitle"]
121
- query = tp.filter_is_not_empty.ascending(tp)
122
- db.query_database(tp.filter_is_not_empty.ascending(tp)).each.with_index(1) do |page, index|
123
- page.properties["NumberTitle"].number = index
124
- page.save
125
- end
126
- ```
127
-
128
- | After execution |
129
- |-----------------------------------------------|
130
- | ![After exuecution](images/serial_number.png) |
131
-
132
- ### 2.3 Change title
133
-
134
- The following code update the page title.
135
-
136
- #### Pattern 1 (replace existing text partially)
137
-
138
- ```Ruby
139
- page = Page.find page_id # API access
140
- print page.title # -> ABC\nDEF
141
- tp = page.properties["Title"]
142
- tp[1].text = "GHI"
143
- page.save # API access
144
- print page.title # -> ABC\nGHI
145
- ```
146
-
147
- #### Pattern 2 (replace all text)
148
-
149
- ```Ruby
150
- page = Page.new id: page_id, assign: [TitleProperty, "Title"]
151
- tp = page.properties["Title"]
152
- tp << TextObject.new("JKL")
153
- page.save # API access
154
- print page.title # -> JKL
155
- ```
102
+ 1. [Set icon to all icon unsettled pages](examples/set_icon_to_all_icon_unsettled_pages.md)
103
+ 1. [Renumbering pages](examples/renumbering_pages.md)
104
+ 1. [Change title](examples/change_title.md)
156
105
 
157
106
  ## 3. Preparation
158
107
 
@@ -165,24 +114,34 @@ Please check [Notion documentation](https://developers.notion.com/docs#getting-s
165
114
  Please create a client (notion-ruby-client) before you use the following class.
166
115
 
167
116
  ```Ruby
168
- NotionCache.instance.create_client ENV["NOTION_API_TOKEN"]
117
+ NotionCache.instance.create_client "Secret_XXXXXXXXXXXXXXXXXXXX" # write directly
118
+ NotionCache.instance.create_client ENV["NOTION_API_TOKEN"] # from environment
169
119
  ```
170
120
 
171
121
  ## 4. Usage
172
122
 
123
+ From v0.3.0, `find`, `save` and `query_database` methods can set `dry_run: true` option. When this option is set, these methods create create a shell script for verification instead of calling the Notion API. Some sample codes show the results of dry_run in order to make it easy to understand what the method runs.
124
+
173
125
  ### 4.1 Page
174
126
 
175
127
  #### 4.1.1 Retrieve a page
176
128
 
177
- `Page.find(id)` creates a Page object with `retrieving page API`.
178
- The created object has page information generated from the JSON response.
129
+ `Page.find(id)` creates a Page object with `retrieving page API`. The created object has page information generated from the JSON response.
179
130
 
180
131
  ```Ruby
181
132
  page = Page.find "c01166c6-13ae-45cb-b968-18b4ef2f5a77" # Notion API call
182
133
  ```
183
134
 
184
- `Page.new(id)` creates a Page object without the Notion API.
185
- Since Page.new does not acquire property information, so you need to assign yourself.
135
+ - result of dry run
136
+ ```bash
137
+ #!/bin/sh
138
+ curl 'https://api.notion.com/v1/pages/c01166c6-13ae-45cb-b968-18b4ef2f5a77'\
139
+ -H 'Notion-Version: 2022-02-22'\
140
+ -H 'Authorization: Bearer '"$NOTION_API_KEY"''\
141
+ -H 'Content-Type: application/json'⏎
142
+ ```
143
+
144
+ `Page.new(id)` creates a Page object without the Notion API. Since Page.new does not acquire property information, so you need to assign yourself.
186
145
 
187
146
  ```Ruby
188
147
  # Assign some properties for update manually
@@ -193,17 +152,14 @@ page = Page.new id: "c01166c6-13ae-45cb-b968-18b4ef2f5a77",
193
152
 
194
153
  #### 4.1.2 Update page properties
195
154
 
196
- Page properties with values can be obtained from the retrieved page using `find`.
197
- On the other hand, Page properties without values can be obtained from the property assigned page.
155
+ Page properties with values can be obtained from the retrieved page using `find`. On the other hand, Page properties without values can be obtained from the property assigned page.
198
156
 
199
157
  ```Ruby
200
158
  tp = page.properties["Title"] # TitleProperty
201
159
  np = page.properties["Number"] # NumberProperty
202
160
  ```
203
161
 
204
- Each property object can change values using corresponded methods.
205
- After changing value, `will_update` flag of the property object also set to true.
206
- These methods are explained in the section of each property object class.
162
+ Each property object can change values using corresponded methods. After changing value, `will_update` flag of the property object also set to true. These methods are explained in the section of each property object class.
207
163
 
208
164
  ```Ruby
209
165
  to = tp[1] # TitleProperty has Array of TextObject
@@ -211,21 +167,20 @@ to.text = "ABC" # TextObject can set text by ".text="
211
167
  # or tp[1].text = "ABC"
212
168
 
213
169
  np.number = 3.14159
214
- page.save # Notion API call
215
170
  ```
216
171
 
217
172
  After update some properties, `page.save` method sends `Notion API` and replace the page information using the response of API.
218
173
 
219
174
  ```Ruby
220
- page.update # Notion API call
175
+ page.save # Notion API call
221
176
  ```
222
177
 
223
178
  #### 4.1.3 Update other page information
224
179
 
225
180
  `page.set_icon` can change the page icon using emoji or external url.
226
181
 
227
- [Breaking change on v0.2.2] `page.set_icon` has no longer calling the Notion API.
228
- Please use `page.save` after `page.set_icon` if you want to update or create the page.
182
+ [Breaking change on v0.2.2]
183
+ `page.set_icon` has no longer calling the Notion API. Please use `page.save` after `page.set_icon` if you want to update or create the page.
229
184
 
230
185
  ```Ruby
231
186
  # both methods call Notion API
@@ -238,7 +193,9 @@ obj.save
238
193
 
239
194
  #### 4.1.4 other methods
240
195
 
196
+ - `Page.find id, dry_run: true` create shell script for verification.
241
197
  - `page.save` call Notion API, and so on and replace object information.
198
+ - `page.save dry_run: true` create shell script for verification.
242
199
  - `page.new_record?` returns true if the page was generated by `create_child_page`.
243
200
  - `page.title` returns plain_text string of `Title`.
244
201
  - `page.icon` returns JSON hash for the page icon.
@@ -248,15 +205,22 @@ obj.save
248
205
 
249
206
  #### 4.2.1 Retrieve a database
250
207
 
251
- `Database.find(id)` creates a Database object with `retrieving database API`.
252
- The created object has database information generated from the JSON response.
208
+ `Database.find(id)` creates a Database object with `retrieving database API`. The created object has database information generated from the JSON response.
253
209
 
254
210
  ```Ruby
255
- db = Database.find("c37a2c66-e3aa-4a0d-a447-73de3b80c253") # Notion API call
211
+ db = Database.find "c37a2c66-e3aa-4a0d-a447-73de3b80c253" # Notion API call
212
+ ```
213
+
214
+ ```bash
215
+ # result of dry run
216
+ #!/bin/sh
217
+ curl 'https://api.notion.com/v1/databases/c37a2c66-e3aa-4a0d-a447-73de3b80c253'\
218
+ -H 'Notion-Version: 2022-02-22'\
219
+ -H 'Authorization: Bearer '"$NOTION_API_KEY"''\
220
+ -H 'Content-Type: application/json'⏎
256
221
  ```
257
222
 
258
- `Database.new(id)` creates a Database object without the Notion API.
259
- Since Database.new does not acquire property information, so you need to assign yourself.
223
+ `Database.new(id)` creates a Database object without the Notion API. Since Database.new does not acquire property information, so you need to assign yourself.
260
224
 
261
225
  ```Ruby
262
226
  # assign some properties for update manually
@@ -266,8 +230,7 @@ db = Database.new id: "c37a2c66-e3aa-4a0d-a447-73de3b80c253",
266
230
 
267
231
  #### 4.2.2 Query a database
268
232
 
269
- `db.query_database` obtains a List object with Page objects contained in the database.
270
- You can obtain filtered and ordered pages using Query object.
233
+ `db.query_database` obtains a List object with Page objects contained in the database. You can obtain filtered and ordered pages using Query object.
271
234
 
272
235
  ```Ruby
273
236
  # query_database method calls Notion API
@@ -275,11 +238,9 @@ db.query_database # retrieves all pages (no filter, no sort)
275
238
  db.query_database query # retrieves using query
276
239
  ```
277
240
 
278
- The query object can be generated from the Property objects included in the database object.
279
- The Property object can be obtained from the retrieved or assigned database object like as the Page object.
241
+ The query object can be generated from the Property objects included in the database object. The Property object can be obtained from the retrieved or assigned database object like as the Page object.
280
242
 
281
- `filter_xxxx` methods of the property objects generates a query object.
282
- These methods are explained in the section of each property object class.
243
+ `filter_xxxx` methods of the property objects generates a query object. These methods are explained in the section of each property object class.
283
244
 
284
245
  ```Ruby
285
246
  tp = db.properties["Title"]
@@ -289,8 +250,7 @@ pages = db.query_database query
289
250
 
290
251
  ##### 4.2.2.1 Complex conditions
291
252
 
292
- Complex filters can be generated `and` / `or` methods of the Query object.
293
- Here are some sample scripts and the json parameters created from them.
253
+ Complex filters can be generated `and` / `or` methods of the Query object. Here are some sample scripts and the json parameters created from them.
294
254
 
295
255
  ```Ruby
296
256
  # Prepare some sample properties
@@ -500,8 +460,7 @@ query7 = np.filter_greater_than(100).and(np.filter_less_than(200))
500
460
 
501
461
  ##### 4.2.2.2 Sort criteria
502
462
 
503
- Sort criteria can be appended to an existing query object.
504
- If you don't use the previous filters, you can generate by `Query.new`.
463
+ Sort criteria can be appended to an existing query object. If you don't use the previous filters, you can generate by `Query.new`.
505
464
 
506
465
  - sort criteria only
507
466
 
@@ -543,10 +502,30 @@ query13 = tp.filter_starts_with("A").ascending(tp)
543
502
  [{"property" => "tp", "direction" => "ascending"}]
544
503
  ```
545
504
 
505
+ ##### 4.2.2.3 Dry run sample
506
+
507
+ This is a sample script for query database. If dry_run flag is set, you can see the verification shell script.
508
+
509
+ ```Ruby
510
+ db = Database.new id: tc.database_id, assign: [NumberProperty, "NumberTitle", UrlProperty, "UrlTitle"]
511
+ np, up = target.properties.values_at "NumberTitle", "UrlTitle"
512
+ query = np.filter_greater_than(100).and(up.filter_starts_with("https")).ascending(np)
513
+ print db.query_database query, dry_run: true
514
+ # => Result
515
+ # #!/bin/sh
516
+ # curl -X POST 'https://api.notion.com/v1/databases/XXXXX/query'\
517
+ # -H 'Notion-Version: 2022-02-22'\
518
+ # -H 'Authorization: Bearer '"$NOTION_API_KEY"''\
519
+ # -H 'Content-Type: application/json'\
520
+ # --data '{"filter":{"and":[{"property":"NumberTitle","number":{"greater_than":100}},{"property":"UrlTitle","url":{"starts_with":"https"}}]},"sorts":[{"property":"NumberTitle","direction":"ascending"}],"page_size":100}'⏎
521
+
522
+
523
+ ```
524
+
525
+
546
526
  #### 4.2.3 Create child page
547
527
 
548
- `create_child_page` creates a child page object of the database.
549
- After setting some properties, please call `page.save` to send page information to Notion.
528
+ `create_child_page` creates a child page object of the database. After setting some properties, please call `page.save` to send page information to Notion.
550
529
 
551
530
  ```Ruby
552
531
  page = db.create_child_page TitleProperty, "Name"
@@ -554,9 +533,138 @@ page.properties["Name"] << "New Page"
554
533
  page.save
555
534
  ```
556
535
 
557
- #### 4.2.4 Update database
536
+ - result of dry run
537
+ ```bash
538
+ #!/bin/sh
539
+ curl -X POST 'https://api.notion.com/v1/pages'\
540
+ -H 'Notion-Version: 2022-02-22'\
541
+ -H 'Authorization: Bearer '"$NOTION_API_KEY"''\
542
+ -H 'Content-Type: application/json'\
543
+ --data '{"properties":{"Name":{"type":"title","title":[{"type":"text","text":{"content":"New Page","link":null},"plain_text":"New Page","href":null}]}},"parent":{"database_id":"c37a2c66e3aa4a0da44773de3b80c253"}}'⏎
544
+ ```
558
545
 
559
- === under construction ===
546
+ #### 4.2.4 Create database
547
+
548
+ `create_child_database` method of an existing page creates a child database object. Some properties of the database can be arrange the option. Here is a sample script for creating a database that set all types of properties.
549
+ ```Ruby
550
+ page = Page.find "a sample page id"
551
+ db = parent_page.create_child_database "New database title",
552
+ CheckboxProperty, "Checkbox",
553
+ CreatedByProperty, "CreatedBy",
554
+ CreatedTimeProperty, "CreatedTime",
555
+ DateProperty, "Date",
556
+ EmailProperty, "Email",
557
+ FilesProperty, "Files",
558
+ FormulaProperty, "Formula",
559
+ LastEditedByProperty, "LastEditedBy",
560
+ LastEditedTimeProperty, "LastEditedTime",
561
+ MultiSelectProperty, "MultiSelect",
562
+ NumberProperty, "Number",
563
+ PeopleProperty, "People",
564
+ PhoneNumberProperty, "PhoneNumber",
565
+ RelationProperty, "Relation",
566
+ RollupProperty, "Rollup",
567
+ RichTextProperty, "RichText",
568
+ SelectProperty, "Select",
569
+ TitleProperty, "Title",
570
+ UrlProperty, "Url"
571
+ fp, msp, np, rp, rup, sp = db.properties.values_at "Formula", "MultiSelect", "Number", "Relation", "Rollup", "Select"
572
+ fp.formula_expression = "now()"
573
+ msp.add_multi_select_options name: "MS1", color: "orange"
574
+ msp.add_multi_select_options name: "MS2", color: "green"
575
+ np.format = "yen"
576
+ rp.replace_relation_database database_id: tc.database_id
577
+ rup.relation_property_name = "Relation"
578
+ rup.rollup_property_name = "NumberTitle"
579
+ rup.function = "sum"
580
+ sp.add_select_options name: "S1", color: "yellow"
581
+ sp.add_select_options name: "S2", color: "default"
582
+ db.set_icon emoji: "🎉"
583
+ db.save
584
+ ```
585
+
586
+ #### 4.2.5 Update database properties
587
+
588
+ `save` method updates existing database properties. The database needs to be retrieved using `find` to prevent existing settings from disappearing.
589
+
590
+ ```Ruby
591
+ db = Database.find "c7697137d49f49c2bbcdd6a665c4f921"
592
+ fp, msp, np, rp, rup, sp = db.properties.values_at "Formula", "MultiSelect", "Number", "Relation", "Rollup", "Select"
593
+ fp.formula_expression = "pi"
594
+ msp.add_multi_select_options name: "MS3", color: "blue"
595
+ np.format = "percent"
596
+ rp.replace_relation_database database_id: tc.database_id, synced_property_name: "Renamed table"
597
+ rup.function = "average"
598
+ sp.add_select_options name: "S3", color: "red"
599
+ db.set_icon emoji: "🎉"
600
+ db.database_title << "(Added)"
601
+ db.save
602
+ ```
603
+
604
+ #### 4.2.6 Add a database property
605
+
606
+ `add_property` can add a database property.
607
+
608
+ ```Ruby
609
+ db = Database.find "c7697137d49f49c2bbcdd6a665c4f921"
610
+ db.add_property NumberProperty, "added number property" do |np|
611
+ np.format = "euro" # arrange option
612
+ end
613
+ db.add_property UrlProperty, "added url property" # UrlProperty has no option
614
+ db.save
615
+ ```
616
+
617
+ - result of dry run
618
+
619
+ ```bash
620
+ #!/bin/sh
621
+ curl -X PATCH 'https://api.notion.com/v1/databases/c7697137d49f49c2bbcdd6a665c4f921'\
622
+ -H 'Notion-Version: 2022-02-22'\
623
+ -H 'Authorization: Bearer '"$NOTION_API_KEY"''\
624
+ -H 'Content-Type: application/json'\
625
+ --data '{"properties":{"added number property":{"number":{"format":"euro"}},"added url property":{"url":{}}}}'⏎
626
+ ```
627
+
628
+ #### 4.2.7 Rename a database property
629
+
630
+ `rename_property` can rename a database property.
631
+
632
+ ```Ruby
633
+ properties = db.properties
634
+ properties["added number property"].new_name = "renamed number property"
635
+ properties["added url property"].new_name = "renamed url property"
636
+ db.save
637
+ ```
638
+
639
+ - result of dry run
640
+
641
+ ```bash
642
+ #!/bin/sh
643
+ curl -X PATCH 'https://api.notion.com/v1/databases/c7697137d49f49c2bbcdd6a665c4f921'\
644
+ -H 'Notion-Version: 2022-02-22'\
645
+ -H 'Authorization: Bearer '"$NOTION_API_KEY"''\
646
+ -H 'Content-Type: application/json'\
647
+ --data '{"properties":{"added number property":{"name":"renamed number property"},"added url property":{"name":"renamed url property"}}}'
648
+ ```
649
+
650
+ #### 4.2.8 Remove database properties
651
+
652
+ `remove_properties' can remove some database properties.
653
+
654
+ ```Ruby
655
+ db.remove_property "renamed number property", "renamed url property"
656
+ ```
657
+
658
+ - result of dry run
659
+
660
+ ```bash
661
+ #!/bin/sh
662
+ curl -X PATCH 'https://api.notion.com/v1/databases/c7697137d49f49c2bbcdd6a665c4f921'\
663
+ -H 'Notion-Version: 2022-02-22'\
664
+ -H 'Authorization: Bearer '"$NOTION_API_KEY"''\
665
+ -H 'Content-Type: application/json'\
666
+ --data '{"properties":{"renamed number property":null,"renamed url property":null}}'
667
+ ```
560
668
 
561
669
  ### 4.3 List class
562
670
 
@@ -614,9 +722,11 @@ xp = page.properties["property_name"]
614
722
  xp, yp = page.properties.values_at "xp_name", "yp_name"
615
723
  ```
616
724
 
725
+ Page properties and database properties are objects of the same class, but there are methods for page properties and methods for database properties.
726
+
617
727
  #### 4.5.2 Query object generator of property objects
618
728
 
619
- The following methods for the Property objects generate a query object.
729
+ The following methods for the Property objects generate a query object. These methods can be used for page properties and database properties.
620
730
 
621
731
  - TitleProperty, RichTextProperty, UrlProperty, EmailProperty, PhoneNumberProperty
622
732
  - filter_equals(value)
@@ -691,11 +801,9 @@ The following methods for the Property objects generate a query object.
691
801
 
692
802
  #### 4.5.3 create or update values for Page properties
693
803
 
694
- Retrieving Page object has XXXProperties with values.
695
- On the other hand, Assigned Page object has also XXXProperties, but they don't have any information for pages.
804
+ Retrieving Page object with `find` method has page properties of XXXProperties with values. On the other hand, Assigned Page object has also XXXProperties, but they don't have any information for pages.
696
805
 
697
- XXXProperties can change property values by setter methods.
698
- Since the setter method is different for each class, it will be explained separately.
806
+ XXXProperties can change property values by setter methods. Since the setter method is different for each class, it will be explained separately.
699
807
 
700
808
  ##### 4.5.3.1 NumberProperty
701
809
 
@@ -710,7 +818,7 @@ p np.property_values_json
710
818
 
711
819
  ##### 4.5.3.2 SelectProperty
712
820
 
713
- NumberProperty can set a select name by `.select=`.
821
+ SelectProperty can set a select name by `.select=`.
714
822
 
715
823
  ```Ruby
716
824
  sp = page.properties["SelectTitle"]
@@ -869,7 +977,7 @@ p tp.property_values_json
869
977
 
870
978
  ##### 4.5.3.9 CheckboxProperty
871
979
 
872
- PeopleProperty can set a boolean value by `.checkbox=`.
980
+ CheckboxProperty can set a boolean value by `.checkbox=`.
873
981
 
874
982
  ```Ruby
875
983
  cp = page.properties["CheckboxTitle"]
@@ -878,18 +986,7 @@ p cp.property_values_json
878
986
  # result => {"cp"=>{"checkbox"=>true, "type"=>"checkbox"}}
879
987
  ```
880
988
 
881
- ##### 4.5.3.10 EmailProperty
882
-
883
- EmailProperty can set an email address by `.email=`.
884
-
885
- ```Ruby
886
- ep = page.properties["MailTitle"]
887
- ep.email = "hkobhkob@gmail.com"
888
- p ep.property_values_json
889
- # result => {"ep"=>{"email"=>"hkobhkob@gmail.com", "type"=>"email"}}
890
- ```
891
-
892
- ##### 4.5.3.11 FilesProperty
989
+ ##### 4.5.3.10 FilesProperty
893
990
 
894
991
  FilesProperty can set an external url or Array of external urls by `.files=`.
895
992
 
@@ -904,7 +1001,7 @@ p fp.property_values_json
904
1001
  # Result => {"fp"=>{"files"=>[{"name"=>"F2", "type"=>"external", "external"=>{"url"=>"F2"}}, {"name"=>"F3", "type"=>"external", "external"=>{"url"=>"F3"}}], "type"=>"files"}}
905
1002
  ```
906
1003
 
907
- ##### 4.5.3.12 RelationProperty
1004
+ ##### 4.5.3.11 RelationProperty
908
1005
 
909
1006
  RelationProperty can set an relation's page_id or Array of relation's page_ids by `.relation=`.
910
1007
 
@@ -919,7 +1016,166 @@ p rp.property_values_json
919
1016
  # Result => {"rp"=>{"type"=>"relation", "relation"=>[{"id"=>"R2"}, {"id"=>"R3"}]}}
920
1017
  ```
921
1018
 
922
- ## 5. XXXObjects
1019
+
1020
+ #### 4.5.4 create or update values for Database properties
1021
+
1022
+ Retrieving Database object with `find` method has database properties of XXXProperties with values. On the other hand, Assigned Database object has also XXXProperties, but they don't have any information for databases.
1023
+
1024
+ XXXProperties can change property values by setter methods. Since the setter method is different for each class, it will be explained separately.
1025
+
1026
+ ##### 4.5.4.1 NumberProperty
1027
+
1028
+ NumberProperty can set a format by `.format=`.
1029
+
1030
+ ```Ruby
1031
+ np = db.properties["NumberTitle"]
1032
+ np.format = "percent"
1033
+ print np.property_schema_json
1034
+ # Result => {"NumberTitle"=>{"number"=>{"format"=>"percent"}}}
1035
+ ```
1036
+
1037
+ ##### 4.5.4.2 SelectProperty
1038
+
1039
+ SelectProperty can add a new option by `.add_select_options`.
1040
+
1041
+ ```Ruby
1042
+ sp = db.properties["SelectTitle"]
1043
+ sp.add_select_options name: "S3", color: "red"
1044
+ print sp.property_schema_json
1045
+ # Result => {"Select"=>{"select"=>{"options"=>[{"id"=>"56a526e1-0cec-4b85-b9db-fc68d00e50c6", "name"=>"S1", "color"=>"yellow"}, {"id"=>"6ead7aee-d7f0-40ba-aa5e-59bccf6c50c8", "name"=>"S2", "color"=>"default"}, {"name"=>"S3", "color"=>"red"}]}}}
1046
+ ```
1047
+
1048
+ If you want to edit existing values, you should access `edit_select_options` array. It sets `will_update` flag to true.
1049
+ ```Ruby
1050
+ sp.edit_select_options[0]["name"] = "new S1"
1051
+ p sp.property_values_json
1052
+ # Result => {"Select"=>{"select"=>{"options"=>[{"id"=>"56a526e1-0cec-4b85-b9db-fc68d00e50c6", "name"=>"new S1", "color"=>"yellow"}, {"id"=>"6ead7aee-d7f0-40ba-aa5e-59bccf6c50c8", "name"=>"S2", "color"=>"default"}]}}}⏎
1053
+ ```
1054
+
1055
+ ##### 4.5.4.3 MultiSelectProperty
1056
+
1057
+ MultiSelectProperty can add a new option by `.add_multi_select_options`.
1058
+
1059
+ ```Ruby
1060
+ msp = db.properties["MultiSelectTitle"]
1061
+ msp.add_multi_select_options name: "MS3", color: "blue"
1062
+ print msp.property_schema_json
1063
+ # Result => {"MultiSelectTitle"=>{"multi_select"=>{"options"=>[{"id"=>"98aaa1c0-4634-47e2-bfae-d739a8c5e564", "name"=>"MS1", "color"=>"orange"}, {"id"=>"71756a93-cfd8-4675-b508-facb1c31af2c", "name"=>"MS2", "color"=>"green"}, {"name"=>"MS3", "color"=>"blue"}]}}}
1064
+ ```
1065
+
1066
+ If you want to edit existing values, you should access `edit_multi_select_options` array. It sets `will_update` flag to true.
1067
+ ```Ruby
1068
+ msp.edit_multi_select_options[0]["name"] = "new MS1"
1069
+ p msp.property_values_json
1070
+ # Result => {"MultiSelectTitle"=>{"multi_select"=>{"options"=>[{"id"=>"98aaa1c0-4634-47e2-bfae-d739a8c5e564", "name"=>"new MS1", "color"=>"orange"}, {"id"=>"71756a93-cfd8-4675-b508-facb1c31af2c", "name"=>"MS2", "color"=>"green"}]}}}⏎
1071
+ ```
1072
+
1073
+ ##### 4.5.4.4 DateProperty
1074
+
1075
+ DateProperty has no option.
1076
+
1077
+ ```Ruby
1078
+ dp = db.properties["DateTitle"]
1079
+ print dp.property_schema_json
1080
+ # Result => {"DateTitle"=>{"date"=>{}}}
1081
+ ```
1082
+
1083
+ ##### 4.5.4.4 UrlProperty
1084
+
1085
+ UrlProperty has no option.
1086
+
1087
+ ```Ruby
1088
+ up = db.properties["UrlTitle"]
1089
+ print up.property_schema_json
1090
+ # Result => {"UrlTitle"=>{"url"=>{}}}
1091
+ ```
1092
+
1093
+ ##### 4.5.4.5 EmailProperty
1094
+
1095
+ EmailProperty has no option.
1096
+
1097
+ ```Ruby
1098
+ ep = db.properties["MailTitle"]
1099
+ print ep.property_schema_json
1100
+ # Result => {"MailTitle"=>{"email"=>{}}}
1101
+ ```
1102
+
1103
+ ##### 4.5.4.6 PhoneNumberProperty
1104
+
1105
+ PhoneNumberProperty has no option.
1106
+
1107
+ ```Ruby
1108
+ pp = db.properties["TelTitle"]
1109
+ print ep.property_schema_json
1110
+ # Result => {"TelTitle"=>{"phone_number"=>{}}}
1111
+ ```
1112
+
1113
+ ##### 4.5.4.7 PeopleProperty
1114
+
1115
+ PeopleProperty has no option.
1116
+
1117
+ ```Ruby
1118
+ usp = db.properties["UserTitle"]
1119
+ print usp.property_schema_json
1120
+ # Result => {"UserTitle"=>{"people"=>{}}}
1121
+ ```
1122
+
1123
+ ##### 4.5.4.8 TitleProperty, RichTextProperty
1124
+
1125
+ TitleProperty and RichTextProperty have no option.
1126
+
1127
+ ```Ruby
1128
+ tp, rtp = db.properties.values_at "Title", "TextTitle"
1129
+ print tp.property_schema_json
1130
+ # Result => {"Title"=>{"title"=>{}}}
1131
+ print rtp.property_schema_json
1132
+ # Result => {"TextTitle"=>{"rich_text"=>{}}}
1133
+ ```
1134
+
1135
+ ##### 4.5.4.9 CheckboxProperty
1136
+
1137
+ CheckboxProperty has no option.
1138
+
1139
+ ```Ruby
1140
+ cp = db.properties["CheckboxTitle"]
1141
+ print cp.property_schema_json
1142
+ # Result => {"CheckboxTitle"=>{"checkbox"=>{}}}
1143
+ ```
1144
+
1145
+ ##### 4.5.4.10 FilesProperty
1146
+
1147
+ FilesProperty has no option.
1148
+
1149
+ ```Ruby
1150
+ fp = db.properties["FilesTitle"]
1151
+ print fp.property_schema_json
1152
+ # Result => {"FilesTitle"=>{"files"=>{}}}
1153
+ ```
1154
+
1155
+ ##### 4.5.4.11 RelationProperty
1156
+
1157
+ RelationProperty can set an relation's database_id by `.replace_relation_database`.
1158
+
1159
+ ```Ruby
1160
+ rp = db.properties["RelationTitle"]
1161
+ rp.replace_relation_database database_id: "new database id"
1162
+ p rp.property_values_json
1163
+ # Result => {"Relation"=>{"relation"=>{"database_id"=>"new database id"}}}
1164
+ ```
1165
+
1166
+ ##### 4.5.4.12 RollupProperty
1167
+
1168
+ RollupProperty can set rollup property name, relation property name and function by `.rollup_property_name=`, `relation_property_name=` and `function=`, respectively.
1169
+
1170
+ ```Ruby
1171
+ rup = db.properties["RollupTitle"]
1172
+ rup.relation_property_name = "new relation property name"
1173
+ rup.rollup_property_name = "new rollup property name"
1174
+ rup.function = "average"
1175
+ # Result => {"Rollup"=>{"rollup"=>{"function"=>"average", "relation_property_name"=>"new relation property name", "rollup_property_name"=>"new rollup property name"}}}
1176
+ ```
1177
+
1178
+ ## 5. XXXObjects and RichTextArray
923
1179
 
924
1180
  ### 5.1 RichTextObject
925
1181
 
@@ -969,8 +1225,33 @@ UserObject is a class for users.
969
1225
  u = User.new id: "user_id"
970
1226
  ```
971
1227
 
1228
+ ### 5.5 RichTextArray
1229
+
1230
+ Some properties and Database title have an array of RichTextObject. `RichTextArray` is the delegate class for the array of RichTextObject.
1231
+ Moreover, some methods of TitleProperty, RichTextProperty and Database.title delegate to the included RichTextArray.
1232
+
1233
+ #### 5.5.1 Constructor for RichTextArray
1234
+
1235
+ RichTextArray can be created by a String, some Strings, a RichTextObject and some RichTextObjects. String values will convert to simple TextObjects.
1236
+
1237
+ ```Ruby
1238
+ nullString = RichTextArray.new "title"
1239
+ aString = RichTextArray.new "title", text_objects: "A string"
1240
+ twoStrings = RichTextArray.new "title", text_objects: %W[ABC\n DEF]
1241
+ aTextObject = RichTextArray.new "title", text_objects: TextObject.new "A TextObject"
1242
+ textMentionObjects = RichTextArray.new "title", [TextObject.new("A TextObject"), MentionObject.new(user_id: "ABC")]
1243
+ ```
1244
+
1245
+ #### 5.5.2 Instance methods
1246
+
1247
+ - `rto << text_or_text_object`: append a text or RichTextObject
1248
+ - `rto[0]`: obtain the object specified by the index
1249
+ - `delete_at 0`: obtain the object specified by the index and delete from the array
1250
+ - `full_text`: obtain joined plain text
1251
+
972
1252
  ## 6. ChangeLog
973
1253
 
1254
+ - 2022/3/25 added create_child_database, update_database, add_property, rename_property and remove_property
974
1255
  - 2022/3/17 added template_mention objects, tools/an command
975
1256
  - 2022/3/16 added database.create_child_page and base.save instead of base.update/create
976
1257
  - 2022/3/15 Fixed not to reload from API when all contents are loaded