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
data/README.md CHANGED
@@ -6,20 +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
-
10
- <!-- @import "[TOC]" {cmd="toc" depthFrom=1 depthTo=6 orderedList=false} -->
11
-
12
- <!-- code_chunk_output -->
13
-
14
9
  - [notion\_ruby\_mapping](#notion_ruby_mapping)
15
10
  - [Table of Contents](#table-of-contents)
16
11
  - [1. Installation](#1-installation)
17
12
  - [2. Example code](#2-example-code)
18
- - [2.1 Set icon to all icon unsettled pages](#21-set-icon-to-all-icon-unsettled-pages)
19
- - [2.2 Renumbering pages](#22-renumbering-pages)
20
- - [2.3 Change title](#23-change-title)
21
- - [Pattern 1 (replace existing text partially)](#pattern-1-replace-existing-text-partially)
22
- - [Pattern 2 (replace all text)](#pattern-2-replace-all-text)
23
13
  - [3. Preparation](#3-preparation)
24
14
  - [3.1 Create a New Integration](#31-create-a-new-integration)
25
15
  - [3.2 Create client](#32-create-client)
@@ -29,12 +19,18 @@ Development note is here. → [Idea note of "notion_ruby_mapping"](https://www.n
29
19
  - [4.1.2 Update page properties](#412-update-page-properties)
30
20
  - [4.1.3 Update other page information](#413-update-other-page-information)
31
21
  - [4.1.4 other methods](#414-other-methods)
32
- - [4.2 Page](#42-page)
22
+ - [4.2 Database](#42-database)
33
23
  - [4.2.1 Retrieve a database](#421-retrieve-a-database)
34
24
  - [4.2.2 Query a database](#422-query-a-database)
35
25
  - [4.2.2.1 Complex conditions](#4221-complex-conditions)
36
26
  - [4.2.2.2 Sort criteria](#4222-sort-criteria)
37
- - [4.2.3 Update database](#423-update-database)
27
+ - [4.2.2.3 Dry run sample](#4223-dry-run-sample)
28
+ - [4.2.3 Create child page](#423-create-child-page)
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)
38
34
  - [4.3 List class](#43-list-class)
39
35
  - [4.4 Block class](#44-block-class)
40
36
  - [4.5 Property classes](#45-property-classes)
@@ -51,17 +47,35 @@ Development note is here. → [Idea note of "notion_ruby_mapping"](https://www.n
51
47
  - [4.5.3.7 PeopleProperty](#4537-peopleproperty)
52
48
  - [4.5.3.8 TitleProperty, RichTextProperty](#4538-titleproperty-richtextproperty)
53
49
  - [4.5.3.9 CheckboxProperty](#4539-checkboxproperty)
54
- - [4.5.3.10 EmailProperty](#45310-emailproperty)
55
- - [4.5.3.11 FilesProperty](#45311-filesproperty)
56
- - [4.5.3.12 RelationProperty](#45312-relationproperty)
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)
67
+ - [5.1 RichTextObject](#51-richtextobject)
68
+ - [5.2 TextObject](#52-textobject)
69
+ - [5.3 MentionObject](#53-mentionobject)
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)
57
74
  - [6. ChangeLog](#6-changelog)
58
- - [6. Contributing](#6-contributing)
59
- - [7. License](#7-license)
60
- - [8. Code of Conduct](#8-code-of-conduct)
61
- - [9. Acknowledgements](#9-acknowledgements)
62
-
63
- <!-- /code_chunk_output -->
64
-
75
+ - [7. Contributing](#7-contributing)
76
+ - [8. License](#8-license)
77
+ - [9. Code of Conduct](#9-code-of-conduct)
78
+ - [10. Acknowledgements](#10-acknowledgements)
65
79
 
66
80
  ## 1. Installation
67
81
 
@@ -85,72 +99,9 @@ gem install notion_ruby_mapping
85
99
 
86
100
  ## 2. Example code
87
101
 
88
- ### 2.1 Set icon to all icon unsettled pages
89
-
90
- The following code sets a "💿" icon on all unset pages in the database.
91
-
92
- ```Ruby
93
- require "notion_ruby_mapping"
94
-
95
- include NotionRubyMapping
96
-
97
- token = ENV["NOTION_API_TOKEN"]
98
- database_id = ENV["DATABASE_ID"]
99
-
100
- NotionCache.instance.create_client token
101
-
102
- db = Database.new id: database_id
103
- db.query_database.each do |page|
104
- p page.set_icon(emoji: "💿").id unless page.icon
105
- end
106
- ```
107
-
108
- |Before execution|After execution|
109
- |---|---|
110
- |![Before execution](images/pre_set_icon.png)|![After execution](images/post_set_icon.png)|
111
-
112
- ### 2.2 Renumbering pages
113
-
114
- The following code sets serial numbers to the pages whose title is not empty in ascending order of titles.
115
-
116
- ```Ruby
117
- db = Database.new id: database_id, assign: [RichTextProperty, "TextTitle"]
118
- tp = db.properties["TextTitle"]
119
- query = tp.filter_is_not_empty.ascending(tp)
120
- db.query_database(tp.filter_is_not_empty.ascending(tp)).each.with_index(1) do |page, index|
121
- page.properties["NumberTitle"].number = index
122
- page.update
123
- end
124
- ```
125
-
126
- | After execution |
127
- |-----------------------------------------------|
128
- | ![After exuecution](images/serial_number.png) |
129
-
130
- ### 2.3 Change title
131
-
132
- The following code update the page title.
133
-
134
- #### Pattern 1 (replace existing text partially)
135
-
136
- ```Ruby
137
- page = Page.find page_id # API access
138
- print page.title # -> ABC\nDEF
139
- tp = page.properties["Title"]
140
- tp[1].text = "GHI"
141
- page.update # API access
142
- print page.title # -> ABC\nGHI
143
- ```
144
-
145
- #### Pattern 2 (replace all text)
146
-
147
- ```Ruby
148
- page = Page.new id: page_id, assign: [TitleProperty, "Title"]
149
- tp = page.properties["Title"]
150
- tp << TextObject.new("JKL")
151
- page.update # API access
152
- print page.title # -> JKL
153
- ```
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)
154
105
 
155
106
  ## 3. Preparation
156
107
 
@@ -163,24 +114,34 @@ Please check [Notion documentation](https://developers.notion.com/docs#getting-s
163
114
  Please create a client (notion-ruby-client) before you use the following class.
164
115
 
165
116
  ```Ruby
166
- 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
167
119
  ```
168
120
 
169
121
  ## 4. Usage
170
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
+
171
125
  ### 4.1 Page
172
126
 
173
127
  #### 4.1.1 Retrieve a page
174
128
 
175
- `Page.find(id)` creates a Page object with `retrieving page API`.
176
- 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.
177
130
 
178
131
  ```Ruby
179
132
  page = Page.find "c01166c6-13ae-45cb-b968-18b4ef2f5a77" # Notion API call
180
133
  ```
181
134
 
182
- `Page.new(id)` creates a Page object without the Notion API.
183
- 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.
184
145
 
185
146
  ```Ruby
186
147
  # Assign some properties for update manually
@@ -191,17 +152,14 @@ page = Page.new id: "c01166c6-13ae-45cb-b968-18b4ef2f5a77",
191
152
 
192
153
  #### 4.1.2 Update page properties
193
154
 
194
- Page properties with values can be obtained from the retrieved page using `find`.
195
- 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.
196
156
 
197
157
  ```Ruby
198
158
  tp = page.properties["Title"] # TitleProperty
199
159
  np = page.properties["Number"] # NumberProperty
200
160
  ```
201
161
 
202
- Each property object can change values using corresponded methods.
203
- After changing value, `will_update` flag of the property object also set to true.
204
- 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.
205
163
 
206
164
  ```Ruby
207
165
  to = tp[1] # TitleProperty has Array of TextObject
@@ -211,41 +169,58 @@ to.text = "ABC" # TextObject can set text by ".text="
211
169
  np.number = 3.14159
212
170
  ```
213
171
 
214
- After update some properties, `page.update` method sends `update page API` and replace the page information using the response of API.
172
+ After update some properties, `page.save` method sends `Notion API` and replace the page information using the response of API.
215
173
 
216
174
  ```Ruby
217
- page.update # Notion API call
175
+ page.save # Notion API call
218
176
  ```
219
177
 
220
178
  #### 4.1.3 Update other page information
221
179
 
222
180
  `page.set_icon` can change the page icon using emoji or external url.
223
181
 
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.
184
+
224
185
  ```Ruby
225
186
  # both methods call Notion API
226
187
  obj.set_icon emoji: "💿" # set emoji
188
+ obj.save
189
+
227
190
  obj.set_icon url: "https://cdn.profile-image.st-hatena.com/users/hkob/profile.png" # set external url
191
+ obj.save
228
192
  ```
229
193
 
230
194
  #### 4.1.4 other methods
231
195
 
196
+ - `Page.find id, dry_run: true` create shell script for verification.
197
+ - `page.save` call Notion API, and so on and replace object information.
198
+ - `page.save dry_run: true` create shell script for verification.
199
+ - `page.new_record?` returns true if the page was generated by `create_child_page`.
232
200
  - `page.title` returns plain_text string of `Title`.
233
201
  - `page.icon` returns JSON hash for the page icon.
234
202
  - `page[key]` returns a hash or an array object except "properties".
235
203
 
236
- ### 4.2 Page
204
+ ### 4.2 Database
237
205
 
238
206
  #### 4.2.1 Retrieve a database
239
207
 
240
- `Database.find(id)` creates a Database object with `retrieving database API`.
241
- 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.
242
209
 
243
210
  ```Ruby
244
- 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'⏎
245
221
  ```
246
222
 
247
- `Database.new(id)` creates a Database object without the Notion API.
248
- 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.
249
224
 
250
225
  ```Ruby
251
226
  # assign some properties for update manually
@@ -255,8 +230,7 @@ db = Database.new id: "c37a2c66-e3aa-4a0d-a447-73de3b80c253",
255
230
 
256
231
  #### 4.2.2 Query a database
257
232
 
258
- `db.query_database` obtains a List object with Page objects contained in the database.
259
- 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.
260
234
 
261
235
  ```Ruby
262
236
  # query_database method calls Notion API
@@ -264,11 +238,9 @@ db.query_database # retrieves all pages (no filter, no sort)
264
238
  db.query_database query # retrieves using query
265
239
  ```
266
240
 
267
- The query object can be generated from the Property objects included in the database object.
268
- 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.
269
242
 
270
- `filter_xxxx` methods of the property objects generates a query object.
271
- 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.
272
244
 
273
245
  ```Ruby
274
246
  tp = db.properties["Title"]
@@ -278,8 +250,7 @@ pages = db.query_database query
278
250
 
279
251
  ##### 4.2.2.1 Complex conditions
280
252
 
281
- Complex filters can be generated `and` / `or` methods of the Query object.
282
- 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.
283
254
 
284
255
  ```Ruby
285
256
  # Prepare some sample properties
@@ -489,8 +460,7 @@ query7 = np.filter_greater_than(100).and(np.filter_less_than(200))
489
460
 
490
461
  ##### 4.2.2.2 Sort criteria
491
462
 
492
- Sort criteria can be appended to an existing query object.
493
- 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`.
494
464
 
495
465
  - sort criteria only
496
466
 
@@ -532,9 +502,169 @@ query13 = tp.filter_starts_with("A").ascending(tp)
532
502
  [{"property" => "tp", "direction" => "ascending"}]
533
503
  ```
534
504
 
535
- #### 4.2.3 Update database
505
+ ##### 4.2.2.3 Dry run sample
536
506
 
537
- === under construction ===
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
+
526
+ #### 4.2.3 Create child page
527
+
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.
529
+
530
+ ```Ruby
531
+ page = db.create_child_page TitleProperty, "Name"
532
+ page.properties["Name"] << "New Page"
533
+ page.save
534
+ ```
535
+
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
+ ```
545
+
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
+ ```
538
668
 
539
669
  ### 4.3 List class
540
670
 
@@ -592,9 +722,11 @@ xp = page.properties["property_name"]
592
722
  xp, yp = page.properties.values_at "xp_name", "yp_name"
593
723
  ```
594
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
+
595
727
  #### 4.5.2 Query object generator of property objects
596
728
 
597
- 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.
598
730
 
599
731
  - TitleProperty, RichTextProperty, UrlProperty, EmailProperty, PhoneNumberProperty
600
732
  - filter_equals(value)
@@ -669,11 +801,9 @@ The following methods for the Property objects generate a query object.
669
801
 
670
802
  #### 4.5.3 create or update values for Page properties
671
803
 
672
- Retrieving Page object has XXXProperties with values.
673
- 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.
674
805
 
675
- XXXProperties can change property values by setter methods.
676
- 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.
677
807
 
678
808
  ##### 4.5.3.1 NumberProperty
679
809
 
@@ -688,7 +818,7 @@ p np.property_values_json
688
818
 
689
819
  ##### 4.5.3.2 SelectProperty
690
820
 
691
- NumberProperty can set a select name by `.select=`.
821
+ SelectProperty can set a select name by `.select=`.
692
822
 
693
823
  ```Ruby
694
824
  sp = page.properties["SelectTitle"]
@@ -822,6 +952,7 @@ p tp.property_values_json
822
952
  ```
823
953
 
824
954
  `<<` method appends a new TextObject or a String.
955
+
825
956
  ```Ruby
826
957
  to = TextObject.new "DEF"
827
958
  to.bold = true
@@ -836,6 +967,7 @@ p tp.property_values_json
836
967
  ```
837
968
 
838
969
  `delete_at(index)` method remove a TextObject at index.
970
+
839
971
  ```Ruby
840
972
  tp.delete_at 1
841
973
  tp << "GHI"
@@ -845,7 +977,8 @@ p tp.property_values_json
845
977
 
846
978
  ##### 4.5.3.9 CheckboxProperty
847
979
 
848
- PeopleProperty can set a boolean value by `.checkbox=`.
980
+ CheckboxProperty can set a boolean value by `.checkbox=`.
981
+
849
982
  ```Ruby
850
983
  cp = page.properties["CheckboxTitle"]
851
984
  cp.checkbox = true
@@ -853,17 +986,7 @@ p cp.property_values_json
853
986
  # result => {"cp"=>{"checkbox"=>true, "type"=>"checkbox"}}
854
987
  ```
855
988
 
856
- ##### 4.5.3.10 EmailProperty
857
-
858
- EmailProperty can set an email address by `.email=`.
859
- ```Ruby
860
- ep = page.properties["MailTitle"]
861
- ep.email = "hkobhkob@gmail.com"
862
- p ep.property_values_json
863
- # result => {"ep"=>{"email"=>"hkobhkob@gmail.com", "type"=>"email"}}
864
- ```
865
-
866
- ##### 4.5.3.11 FilesProperty
989
+ ##### 4.5.3.10 FilesProperty
867
990
 
868
991
  FilesProperty can set an external url or Array of external urls by `.files=`.
869
992
 
@@ -878,7 +1001,7 @@ p fp.property_values_json
878
1001
  # Result => {"fp"=>{"files"=>[{"name"=>"F2", "type"=>"external", "external"=>{"url"=>"F2"}}, {"name"=>"F3", "type"=>"external", "external"=>{"url"=>"F3"}}], "type"=>"files"}}
879
1002
  ```
880
1003
 
881
- ##### 4.5.3.12 RelationProperty
1004
+ ##### 4.5.3.11 RelationProperty
882
1005
 
883
1006
  RelationProperty can set an relation's page_id or Array of relation's page_ids by `.relation=`.
884
1007
 
@@ -894,8 +1017,244 @@ p rp.property_values_json
894
1017
  ```
895
1018
 
896
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
1179
+
1180
+ ### 5.1 RichTextObject
1181
+
1182
+ RichTextObject is an abstract class for TextObject and MentionObject.
1183
+ It can store a link and some annotations.
1184
+
1185
+ There are common instance methods for its subclass objects.
1186
+
1187
+ - plain_text=(value)
1188
+ - bold=(flag)
1189
+ - italic=(flag)
1190
+ - strikethrough=(flag)
1191
+ - underline=(flag)
1192
+ - code=(flag)
1193
+ - color=(color)
1194
+
1195
+ ### 5.2 TextObject
1196
+
1197
+ TextObject is a class for texts.
1198
+ `TextObject.new(text)` creates a TextObject.
1199
+ After creating or retrieving TextObject, `to.text=` replaces including text.
1200
+
1201
+ ```Ruby
1202
+ to = TextObject.new "first text"
1203
+ to.text = "replaced text"
1204
+ ```
1205
+
1206
+ ### 5.3 MentionObject
1207
+
1208
+ MentionObject is a class for mentions for user, page, database, date and template_mention.
1209
+
1210
+ ```Ruby
1211
+ mention_user = MentionObject.new user_id: "user_id", plain_text: "m_user"
1212
+ mention_page = MentionObject.new page_id: "page_id", plain_text: "m_page"
1213
+ mention_db = MentionObject.new database_id: "database_id", plain_text: "m_db"
1214
+ mention_date = MentionObject.new start: "2022-03-17", plain_text: "m_date"
1215
+ mention_today = MentionObject.new mention_template: "today"
1216
+ mention_now = MentionObject.new mention_template: "now"
1217
+ mention_user = MentionObject.new mention_template: "user"
1218
+ ```
1219
+
1220
+ ### 5.4 UserObject
1221
+
1222
+ UserObject is a class for users.
1223
+
1224
+ ```Ruby
1225
+ u = User.new id: "user_id"
1226
+ ```
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
+
897
1252
  ## 6. ChangeLog
898
1253
 
1254
+ - 2022/3/25 added create_child_database, update_database, add_property, rename_property and remove_property
1255
+ - 2022/3/17 added template_mention objects, tools/an command
1256
+ - 2022/3/16 added database.create_child_page and base.save instead of base.update/create
1257
+ - 2022/3/15 Fixed not to reload from API when all contents are loaded
899
1258
  - 2022/3/14 Exclude notion-ruby-client, update Property values, update for Notion-Version 2022-02-22
900
1259
  - 2022/2/25 add_property_for_update -> assign_property, update README.md
901
1260
  - 2022/2/20 add support for MultiSelectProperty
@@ -907,18 +1266,18 @@ p rp.property_values_json
907
1266
  - 2022/2/13 added Page#set_icon
908
1267
  - 2022/2/13 First commit
909
1268
 
910
- ## 6. Contributing
1269
+ ## 7. Contributing
911
1270
 
912
1271
  Bug reports and pull requests are welcome on GitHub at <https://github.com/hkob/notion_ruby_mapping>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/notion_ruby_mapping/blob/main/CODE_OF_CONDUCT.md).
913
1272
 
914
- ## 7. License
1273
+ ## 8. License
915
1274
 
916
1275
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
917
1276
 
918
- ## 8. Code of Conduct
1277
+ ## 9. Code of Conduct
919
1278
 
920
1279
  Everyone interacting in the NotionRubyMapping project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/notion_ruby_mapping/blob/main/CODE_OF_CONDUCT.md).
921
1280
 
922
- ## 9. Acknowledgements
1281
+ ## 10. Acknowledgements
923
1282
 
924
1283
  The code depends on [notion-ruby-client](https://github.com/orbit-love/notion-ruby-client).