notion_ruby_mapping 0.1.1 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c557600e1d6f89386696ffeadd7b3169fc0f3bfd19cc9aab29ce4e401d5b8969
4
- data.tar.gz: 3dc90d601a14e96bc6c65b44a75231095cc52a7efc89067b5e37653133e546cf
3
+ metadata.gz: 5aef3ce3be5f734faf9803496c1196e715ad8513fbabc143abe43b4d4780a274
4
+ data.tar.gz: 1384fc70512cc6a396693fb7a37f8a80311bac85e26616a22ea8a5b3c491e28e
5
5
  SHA512:
6
- metadata.gz: 3177e1c978dfda8968986afc96c4456a3842112d5de4d8b661074be3aaf3eb3a75b87297ac3b5701874091ac3444e8d88782747efa4ffbab02f3299875363d47
7
- data.tar.gz: 3ad09dca8a9f818e35b8f8b1c611f89273efc4c9b34a78daf5fb34232372f5827afe5e307011380fa47263bc2de694461339d4b27793e4b2cb1af0fecdf75f6f
6
+ metadata.gz: cf783b7b53434fefb0f556a9f0fd17ca4dc4bac220e90bb609abf454a1e05ec0de37f4937998cd04fc34c037e87bea9ca3c454593a53a1a0de7998fea7009c9f
7
+ data.tar.gz: ea70aa30d1f9d082cab295320d79b4a975a67255fc61ce4956379e98ca4b4996093ac9e0b1c19e80815b233792be5ff5d29b50a49115d0d1e68461237dfefdfd
data/README.md CHANGED
@@ -6,25 +6,41 @@ Development note is here. → [Idea note of "notion_ruby_mapping"](https://www.n
6
6
 
7
7
  ## Table of Contents
8
8
  - [NotionRubyMapping](#notionrubymapping)
9
- - [Installation](#installation)
10
- - [Example code](#example-code)
11
- - [Usage](#usage)
12
- - [Create a New Integration](#create-a-new-integration)
13
- - [Create client](#create-client)
14
- - [Classes](#classes)
15
- - [Base class](#base-class)
16
- - [Database class](#database-class)
17
- - [Query class](#query-class)
18
- - [Page class](#page-class)
19
- - [List class](#list-class)
20
- - [Block class](#block-class)
21
- - [ChangeLog](#changelog)
22
- - [Contributing](#contributing)
23
- - [License](#license)
24
- - [Code of Conduct](#code-of-conduct)
25
- - [Acknowledgements](#acknowledgements)
26
-
27
- ## Installation
9
+ - [Table of Contents](#table-of-contents)
10
+ - [1. Installation](#1-installation)
11
+ - [2. Example code](#2-example-code)
12
+ - [3. Usage](#3-usage)
13
+ - [3.1 Create a New Integration](#31-create-a-new-integration)
14
+ - [3.2 Create client](#32-create-client)
15
+ - [3.3 Retrieve a database](#33-retrieve-a-database)
16
+ - [3.4 Retrieve a page](#34-retrieve-a-page)
17
+ - [3.5 Query a database](#35-query-a-database)
18
+ - [3.5.1 A simple example](#351-a-simple-example)
19
+ - [3.5.2 Property classes](#352-property-classes)
20
+ - [3.5.3 Query object generator of property objects](#353-query-object-generator-of-property-objects)
21
+ - [3.5.4 Complex conditions](#354-complex-conditions)
22
+ - [3.5.5 Sort criteria](#355-sort-criteria)
23
+ - [3.6 Access the database or page values and properties](#36-access-the-database-or-page-values-and-properties)
24
+ - [3.7 Set icon (Database / Page)](#37-set-icon-database--page)
25
+ - [3.8 Update page property values](#38-update-page-property-values)
26
+ - [3.8.1 Assign property and update it (fastest: one API call only)](#381-assign-property-and-update-it-fastest-one-api-call-only)
27
+ - [3.8.2 Update the loaded page (easy but slow: two API calls)](#382-update-the-loaded-page-easy-but-slow-two-api-calls)
28
+ - [3.8.3 Update the unloaded page with auto-load (easy but slow: two API calls)](#383-update-the-unloaded-page-with-auto-load-easy-but-slow-two-api-calls)
29
+ - [3.8.4 Explanation of how to update each property](#384-explanation-of-how-to-update-each-property)
30
+ - [3.8.4.1 NumberProperty](#3841-numberproperty)
31
+ - [3.8.4.2 SelectProperty](#3842-selectproperty)
32
+ - [3.8.4.3 MultiSelectProperty](#3843-multiselectproperty)
33
+ - [3.8.4.4 DateProperty](#3844-dateproperty)
34
+ - [3.9 Iteration of list object](#39-iteration-of-list-object)
35
+ - [4. ChangeLog](#4-changelog)
36
+ - [5. Contributing](#5-contributing)
37
+ - [6. License](#6-license)
38
+ - [7. Code of Conduct](#7-code-of-conduct)
39
+ - [8. Acknowledgements](#8-acknowledgements)
40
+
41
+ <!-- @import "[TOC]" {cmd="toc" depthFrom=2 depthTo=4 orderedList=false} -->
42
+
43
+ ## 1. Installation
28
44
 
29
45
  Add this line to your application's Gemfile:
30
46
 
@@ -40,7 +56,7 @@ Or install it yourself as:
40
56
 
41
57
  $ gem install notion_ruby_mapping
42
58
 
43
- ## Example code
59
+ ## 2. Example code
44
60
 
45
61
  The following code sets a "💿" icon on all unset pages in the database.
46
62
  ```Ruby
@@ -58,6 +74,10 @@ Database.query(database_id).each do |page|
58
74
  end
59
75
  ```
60
76
 
77
+ |Before execution|After execution|
78
+ |---|---|
79
+ |![Before execution](images/pre_set_icon.png)|![After execution](images/post_set_icon.png)|
80
+
61
81
  The following code sets serial numbers to the pages whose title is not empty in ascending order of titles.
62
82
  ```Ruby
63
83
  tp = RichTextProperty.new("TextTitle")
@@ -65,52 +85,59 @@ Database.query(database_id, tp.filter_is_not_empty.ascending(tp)).each.with_inde
65
85
  page.properties["NumberTitle"].number = index
66
86
  page.update
67
87
  end
68
-
69
88
  ```
89
+ | After execution |
90
+ |-----------------------------------------------|
91
+ | ![After exuecution](images/serial_number.png) |
70
92
 
71
- |Before execution|After execution|
72
- |---|---|
73
- |![Before execution](images/pre_set_icon.png)|![After execution](images/post_set_icon.png)|
93
+ ## 3. Usage
74
94
 
75
- ## Usage
76
-
77
- ### Create a New Integration
95
+ ### 3.1 Create a New Integration
78
96
 
79
97
  Please check [Notion documentation](https://developers.notion.com/docs#getting-started).
80
98
 
81
- ### Create client
99
+ ### 3.2 Create client
82
100
 
101
+ Please create a client (notion-ruby-client) before you use the following class.
83
102
  ```Ruby
84
103
  NotionCache.instance.create_client ENV["NOTION_API_TOKEN"]
85
104
  ```
86
- Please create a client (notion-ruby-client) before you use the following class.
87
105
 
88
- ### Classes
106
+ ### 3.3 Retrieve a database
107
+
108
+ Database.find(id) creates a Database object from the results of Notion API.
89
109
 
90
- #### Base class (Abstract class for Database / Page / Block / List)
110
+ ```Ruby
111
+ db = Database.find("c37a2c66-e3aa-4a0d-a447-73de3b80c253")
112
+ ```
91
113
 
92
- - Set icon (only Database / Page)
114
+ Database.new(id) creates a Database object without accessing to Notion.
115
+ If you want to retrieve the contents after creation, please use a `reload` method.
93
116
 
94
117
  ```Ruby
95
- obj.set_icon emoji: "💿" # set emoji
96
- obj.set_icon url: "https://cdn.profile-image.st-hatena.com/users/hkob/profile.png" # set external url
118
+ db = Database.new("c37a2c66-e3aa-4a0d-a447-73de3b80c253")
119
+ db.reload
97
120
  ```
98
121
 
99
- - Get values and properties
122
+ ### 3.4 Retrieve a page
123
+
124
+ Page.find(id) creates a Page object from the results of Notion API.
100
125
 
101
126
  ```Ruby
102
- obj.icon # obtain icon json
103
- obj["icon"] # same as obj.icon
104
- obj.properties["NumberTitle"]
127
+ page = Page.find("c01166c6-13ae-45cb-b968-18b4ef2f5a77")
105
128
  ```
106
129
 
107
- #### Database class
130
+ Page.new(id) creates a Page object without accessing to Notion.
131
+ If you want to retrieve the contents after creation, please use a `reload` method.
108
132
 
109
- - Retrieve a database
110
133
  ```Ruby
111
- db = Database.find("c37a2c66-e3aa-4a0d-a447-73de3b80c253")
134
+ page = Page.new("c01166c6-13ae-45cb-b968-18b4ef2f5a77")
135
+ page.reload
112
136
  ```
113
- - Query a database
137
+
138
+ ### 3.5 Query a database
139
+
140
+ #### 3.5.1 A simple example
114
141
 
115
142
  Gets a List object of Page objects contained in the database.
116
143
  You can obtain filtered and ordered pages using Query object.
@@ -119,8 +146,6 @@ Database.query("c37a2c66-e3aa-4a0d-a447-73de3b80c253") # retrieves all pages
119
146
  Database.query("c37a2c66-e3aa-4a0d-a447-73de3b80c253", query) # retrieves using query
120
147
  ```
121
148
 
122
- #### Query class and related *Property class
123
-
124
149
  Query object can be generated from the following Property objects.
125
150
  For example, in order to obtain the pages whose title starts with "A" and ordered by ascending,
126
151
  the following code can be used.
@@ -130,6 +155,35 @@ query = tp.filter_starts_with("A").ascending(tp)
130
155
  pages = Database.query database_id, query
131
156
  ```
132
157
 
158
+ #### 3.5.2 Property classes
159
+
160
+ There are the following 17 XXXProperty classes corresponding to Notion databases.
161
+ They are child classes of a `Property` class
162
+ and these constructor method has a common form where the first argument is the property name.
163
+ ```Ruby
164
+ xp = XXXProperty.new "property name"
165
+ ```
166
+
167
+ 1. TitleProperty
168
+ 2. RichTextProperty
169
+ 3. UrlProperty
170
+ 4. EmailProperty
171
+ 5. PhoneNumberProperty
172
+ 6. NumberProperty
173
+ 7. CheckboxProperty
174
+ 8. SelectProperty
175
+ 9. MultiSelectProperty
176
+ 10. PeopleProperty
177
+ 11. CreatedByProperty
178
+ 12. LastEditedByProperty
179
+ 13. DateProperty
180
+ 14. CreatedTimeProperty
181
+ 15. LastEditedTimeProperty
182
+ 16. FilesProperty
183
+ 17. FormulaProperty
184
+
185
+ #### 3.5.3 Query object generator of property objects
186
+
133
187
  The following methods for the Property objects generate a query object.
134
188
  - TitleProperty, RichTextProperty, UrlProperty, EmailProperty, PhoneNumberProperty
135
189
  - filter_equals(value)
@@ -202,16 +256,19 @@ The following methods for the Property objects generate a query object.
202
256
  - filter_is_empty
203
257
  - filter_is_not_empty
204
258
 
259
+ #### 3.5.4 Complex conditions
205
260
  Complex filters can be generated `and` / `or` methods.
261
+ Here are some sample scripts and the json parameters created from them.
262
+
206
263
  ```Ruby
207
- # Prepare sample properties
264
+ # Prepare some sample properties
208
265
  tp = TitleProperty.new "tp"
209
266
  np = NumberProperty.new "np"
210
267
  cp = CheckboxProperty.new "cp"
211
268
  letp = LastEditedTimeProperty.new "letp"
212
269
  ```
213
270
 
214
- - (A and B) filter
271
+ - query1: (A and B) filter
215
272
  ```Ruby
216
273
  query1 = tp.filter_starts_with("start")
217
274
  .and(np.filter_greater_than(100))
@@ -231,7 +288,7 @@ query1 = tp.filter_starts_with("start")
231
288
  }
232
289
  ```
233
290
 
234
- - (A and B and C) filter
291
+ - query2: (A and B and C) filter
235
292
  ```Ruby
236
293
  query2 = tp.filter_starts_with("start")
237
294
  .and(np.filter_greater_than(100))
@@ -256,7 +313,7 @@ query2 = tp.filter_starts_with("start")
256
313
  }
257
314
  ```
258
315
 
259
- - (A or B) filter
316
+ - query3: (A or B) filter
260
317
  ```Ruby
261
318
  query3 = tp.filter_starts_with("start")
262
319
  .or(np.filter_greater_than(100))
@@ -276,7 +333,7 @@ query3 = tp.filter_starts_with("start")
276
333
  }
277
334
  ```
278
335
 
279
- - (A or B or C) filter
336
+ - query4: (A or B or C) filter
280
337
  ```Ruby
281
338
  query4 = tp.filter_starts_with("start")
282
339
  .or(np.filter_greater_than(100))
@@ -301,7 +358,7 @@ query4 = tp.filter_starts_with("start")
301
358
  }
302
359
  ```
303
360
 
304
- - ((A and B) or C) filter
361
+ - query5: ((A and B) or C) filter
305
362
  ```Ruby
306
363
  query5 = tp.filter_starts_with("start")
307
364
  .and(np.filter_greater_than(100))
@@ -330,7 +387,7 @@ query5 = tp.filter_starts_with("start")
330
387
  }
331
388
  ```
332
389
 
333
- - ((A or B) and C) filter
390
+ - query6: ((A or B) and C) filter
334
391
  ```Ruby
335
392
  query6 = tp.filter_starts_with("start")
336
393
  .or(np.filter_greater_than(100))
@@ -359,7 +416,7 @@ query6 = tp.filter_starts_with("start")
359
416
  }
360
417
  ```
361
418
 
362
- - ((A and B) or (C and D)) filter
419
+ - query7: ((A and B) or (C and D)) filter
363
420
  ```Ruby
364
421
  query7 = np.filter_greater_than(100).and(np.filter_less_than(200))
365
422
  .or(np.filter_greater_than(300).and(np.filter_less_than(400)))
@@ -395,7 +452,11 @@ query7 = np.filter_greater_than(100).and(np.filter_less_than(200))
395
452
  }
396
453
  ```
397
454
 
398
- - sort criteria
455
+ #### 3.5.5 Sort criteria
456
+ Sort criteria can be appended to an existing query object.
457
+ If you don't use the previous filters, you can generate by `Query.new`.
458
+
459
+ - sort criteria only
399
460
  ```Ruby
400
461
  query8 = Query.new.ascending tp
401
462
  query9 = Query.new.ascending letp
@@ -427,86 +488,211 @@ query12 = Query.new.ascending(tp).descending letp
427
488
  query13 = tp.filter_starts_with("A").ascending(tp)
428
489
 
429
490
  # Result of query13.filter
430
- {"property" => "tp", "title" => {"starts_with" => "start"}},
491
+ {"property" => "tp", "title" => {"starts_with" => "start"}}
431
492
 
432
493
  # Result of query13.sort
433
494
  [{"property" => "tp", "direction" => "ascending"}]
434
495
  ```
435
496
 
436
- #### Page class
497
+ ### 3.6 Access the database or page values and properties
498
+
499
+ Database or Page values can be accessed by [] access with keywords.
437
500
 
438
- - Retrieve a page
439
501
  ```Ruby
440
- page = Page.find("c01166c6-13ae-45cb-b968-18b4ef2f5a77")
502
+ obj["icon"]
441
503
  ```
442
504
 
443
- - Update values and properties
505
+ Almost values returns a hash or an array object except "properties".
506
+ `obj["properties"]` or `obj.properties` returns a PropertyCache object.
507
+ The PropertyCache object is created from json information on the first access and is cached to the Page or Database object.
508
+ If page or database object is generated by `.new` method, the object don't have json information.
509
+ In this case, the `.properties` method calls `reload` method automatically, then it creates the PropertyCache object from the loaded json information (called auto-load function).
510
+
511
+ ```Ruby
512
+ obj["properties"]
513
+ # or
514
+ obj.properties
515
+ ```
516
+
517
+ Each Property object is obtained by [] access with the property name.
518
+ The Property object is also created on the first access and is cached to the PropertyCache object.
519
+
520
+ ```Ruby
521
+ np = obj.properties["NumberTitle"]
522
+ ```
523
+
524
+ ## 3.7 Set icon (Database / Page)
525
+
526
+ Database or Page icon can update by `obj.set_icon` method.
527
+ You can set icon as emoji or external url.
528
+
529
+ ```Ruby
530
+ obj.set_icon emoji: "💿" # set emoji
531
+ obj.set_icon url: "https://cdn.profile-image.st-hatena.com/users/hkob/profile.png" # set external url
532
+ ```
533
+
534
+ ## 3.8 Update page property values
444
535
 
445
536
  Page properties can update in the following three ways.
446
537
 
447
- 1. update the property directory (fastest: one API call only)
538
+ ### 3.8.1 Assign property and update it (fastest: one API call only)
539
+
540
+ This is the fastest way using an empty Page object.
541
+ `page.assign_property` generates an empty PropertyCache and an empty XXXProperty.
542
+ After updating the property value, please call `page.update`.
543
+ Notion API will be called using a JSON payload generated from the PropertyCache object.
544
+
448
545
  ```Ruby
449
546
  page = Page.new id: page_id
450
- np = NumberProperty.new "NumberTitle", number: 3.14
451
- page.add_property_for_update np
547
+ property = page.assign_property NumberProperty, "NumberTitle"
548
+ property.number = 2022
452
549
  page.update # update page API call
453
550
  print page
454
551
  ```
455
552
 
456
- 2. update the loaded page (easy but slow: two API call)
553
+ ### 3.8.2 Update the loaded page (easy but slow: two API calls)
554
+
555
+ This is an easiest way using a loaded Page object.
556
+ A PropertyCache object is automatically generated by `page.properties`.
557
+ After updating the property value, please call `page.update`.
558
+ Notion API will be called using a JSON payload generated from the PropertyCache object.
559
+ There are two API calls (find and update).
560
+
457
561
  ```Ruby
458
562
  page = Page.find first_page_id # retrieve page API call
459
- page.properties["NumberTitle"].number = 2022
563
+ property = page.properties["NumberTitle"]
564
+ property.number = 2022
460
565
  page.update # update page API call
461
566
  print page
462
567
  ```
463
568
 
464
- 3. update the unloaded page using autoload (easy but slow: two API call)
569
+ ### 3.8.3 Update the unloaded page with auto-load (easy but slow: two API calls)
570
+
571
+ This is also an easiest way using an auto-load function of the Page object.
572
+ A PropertyCache object is automatically generated by `page.properties` using the auto-load function of page objects.
573
+ After updating the property value, please call `page.update`.
574
+ Notion API will be called using a JSON payload generated from the PropertyCache object.
575
+ There are also two API calls (find and update).
576
+
465
577
  ```Ruby
466
578
  page = Page.new id: first_page_id
467
- page.properties["NumberTitle"].number = 12345 # retrieve page API call (autoload)
579
+ property = page.properties["NumberTitle"] # retrieve page API call (auto-load)
580
+ property.number = 12345
468
581
  page.update # update page API call
469
582
  print page
470
583
  ```
471
584
 
472
- - Retrieve block children (List object)
585
+ ### 3.8.4 Explanation of how to update each property
586
+
587
+ #### 3.8.4.1 NumberProperty
588
+
589
+ NumberProperty can set a number by `.number=`.
590
+
591
+ ```Ruby
592
+ np = page.properties["NumberTitle"]
593
+ np.number = 456
594
+ p np.create_json
595
+ # Result => {"number" => 456}
596
+ ```
597
+
598
+ #### 3.8.4.2 SelectProperty
599
+
600
+ NumberProperty can set a select name by `.select=`.
601
+
473
602
  ```Ruby
474
- children = page.children
603
+ sp = page.properties["SelectTitle"]
604
+ sp.select = "Select 2"
605
+ p sp.create_json
606
+ # Result => {"select" => {"name" => "Select 2"}}
475
607
  ```
476
608
 
477
- #### List class
609
+ #### 3.8.4.3 MultiSelectProperty
610
+
611
+ MultiSelectProperty can set a select value or Array of select values by `.multi_select=`.
478
612
 
479
- - access for each components
480
613
  ```Ruby
481
- list.each do |obj| # obj's class is Page or Block
482
- # do something
483
- end
614
+ msp = page.properties["MultiSelectTitle"]
615
+ msp.select = "MS2"
616
+ p msp.create_json
617
+ # Result => {"multi_select" => [{"name" => "MS2"}]}
618
+
619
+ msp.multi_select = %w[MS2 MS1]
620
+ p msp.create_json
621
+ # Result => {"multi_select" => [{"name" => "MS2"}, {"name" => "MS1"}]}
622
+ ```
623
+
624
+ #### 3.8.4.4 DateProperty
625
+
626
+ DateProperty can set a start_date or end_date by `.start_date=` or `end_date=`.
627
+ Date, Time, DateTime or String object can be used to the argument.
628
+
629
+ ```Ruby
630
+ dp = page.properties["DateTitle"]
631
+ dp.start_date = Date.new(2022, 2, 22)
632
+ p dp.create_json
633
+ # Result => {"start" => "2022-02-22"}
634
+
635
+ dp.start_date = Time.new(2022, 2, 22, 1, 23, 45, "+09:00")
636
+ p dp.create_json
637
+ # Result => {"start" => "2022-02-22T01:23:45+09:00"}
638
+
639
+ dp.start_date = DateTime.new(2022, 2, 23, 1, 23, 45, "+09:00")
640
+ p dp.create_json
641
+ # Result => {"start" => "2022-02-23T01:23:45+09:00"}
642
+
643
+ dp.start_date = Date.new(2022, 2, 20)
644
+ dp.end_date = Date.new(2022, 2, 22)
645
+ p dp.create_json
646
+ # Result => {"start" => "2022-02-20", "end" => "2022-02-22"}
647
+
648
+ dp.start_date = Time.new(2022, 2, 21, 1, 23, 45, "+09:00")
649
+ dp.end_date = Time.new(2022, 2, 22, 1, 23, 45, "+09:00")
650
+ p dp.create_json
651
+ # Result => {"start" => "2022-02-21T01:23:45+09:00", "end" => "2022-02-22T01:23:45+09:00"}
652
+
653
+ dp.start_date = DateTime.new(2022, 2, 21, 1, 23, 45, "+09:00")
654
+ dp.end_date = DateTime.new(2022, 2, 22, 1, 23, 45, "+09:00")
655
+ p dp.create_json
656
+ # result => {"start" => "2022-02-21T01:23:45+09:00", "end" => "2022-02-22T01:23:45+09:00"}
484
657
  ```
485
658
 
486
- #### Block class
659
+ ### 3.9 Iteration of list object
487
660
 
488
- Not implemented
661
+ Some methods like as `Database.query` return a list object.
662
+ Since the list object also has a json array object, `Enumerable` module is implemented.
663
+ Each iteration creates a Page or Block object and calls the block with it as an argument.
489
664
 
490
- ## ChangeLog
665
+ ```Ruby
666
+ pages = Database.query("c37a2c66-e3aa-4a0d-a447-73de3b80c253") # retrieves all pages
667
+ pages.each do |page| # obj's class is Page or Block
668
+ page.set_icon(emoji: "💿") unless page["icon"]
669
+ end
670
+ ```
491
671
 
672
+ ## 4. ChangeLog
673
+
674
+ - 2022/2/25 add_property_for_update -> assign_property, update README.md
675
+ - 2022/2/20 add support for MultiSelectProperty
676
+ - 2022/2/19 add support for SelectProperty
677
+ - 2022/2/17 added Page#properties, Page#add_property_for_update, Page#update
492
678
  - 2022/2/17 added Page#properties, Page#add_property_for_update, Page#update
493
679
  - 2022/2/16 added PropertyCache and Payload class
494
680
  - 2022/2/14 added Database#set_icon
495
681
  - 2022/2/13 added Page#set_icon
496
682
  - 2022/2/13 First commit
497
683
 
498
- ## Contributing
684
+ ## 5. Contributing
499
685
 
500
686
  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).
501
687
 
502
- ## License
688
+ ## 6. License
503
689
 
504
690
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
505
691
 
506
- ## Code of Conduct
692
+ ## 7. Code of Conduct
507
693
 
508
694
  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).
509
695
 
510
- ## Acknowledgements
696
+ ## 8. Acknowledgements
511
697
 
512
- The code depends on [notion-ruby-client](https://github.com/orbit-love/notion-ruby-client).
698
+ The code depends on [notion-ruby-client](https://github.com/orbit-love/notion-ruby-client).
Binary file
@@ -40,12 +40,18 @@ module NotionRubyMapping
40
40
  unless @json
41
41
  return nil if @id.nil?
42
42
 
43
- update_json reload
43
+ reload
44
44
  end
45
45
  @property_cache = PropertyCache.new json_properties
46
46
  end
47
47
  @property_cache
48
- end
48
+ end
49
+
50
+ # @return [NotionRubyMapping::Base] reloaded self
51
+ def reload
52
+ update_json reload_json
53
+ self
54
+ end
49
55
 
50
56
  # @return [Hash] json properties
51
57
  def json_properties
@@ -85,7 +91,7 @@ module NotionRubyMapping
85
91
  end
86
92
  self
87
93
  end
88
-
94
+
89
95
  # @param [String] key
90
96
  # @return [NotionRubyMapping::PropertyCache, Hash] obtained Page value or PropertyCache
91
97
  def [](key)
@@ -110,10 +116,23 @@ module NotionRubyMapping
110
116
  # @param [Property] property Property object for udpate or create
111
117
  # @return [NotionRubyMapping::Base]
112
118
  def add_property_for_update(property)
113
- properties.add_property property, will_update: true
119
+ @property_cache ||= PropertyCache.new {}
120
+ @property_cache.add_property property, will_update: true
114
121
  self
115
122
  end
116
123
 
124
+ # @param [Class] klass
125
+ # @param [String] title
126
+ # @return [Property] generated property
127
+ def assign_property(klass, title)
128
+ @property_cache ||= PropertyCache.new {}
129
+
130
+ property = klass.new(title)
131
+ property.will_update = true
132
+ @property_cache.add_property property
133
+ property
134
+ end
135
+
117
136
  # @return [Hash] created json
118
137
  def create_json
119
138
  payload.create_json @property_cache&.create_json
@@ -17,8 +17,12 @@ module NotionRubyMapping
17
17
  # @param [String] id database_id (with or without "-")
18
18
  # @param [Payload] payload
19
19
  def update
20
- update_json @nc.update_database @id, @payload.create_json
20
+ update_json @nc.update_database(@id, create_json)
21
+ end
22
+
23
+ # @return [Hash]
24
+ def reload_json
25
+ @nc.database_json @id
21
26
  end
22
27
  end
23
28
  end
24
-
@@ -7,7 +7,7 @@ module NotionRubyMapping
7
7
  class DateBaseProperty < Property
8
8
  include IsEmptyIsNotEmpty
9
9
 
10
- # @param [Date, Time, DateTime, String] obj
10
+ # @param [Date, Time, DateTime, String, nil] obj
11
11
  # @return [String] iso8601 format string
12
12
  def value_str(obj)
13
13
  case obj
@@ -3,5 +3,42 @@
3
3
  module NotionRubyMapping
4
4
  class DateProperty < DateBaseProperty
5
5
  TYPE = "date"
6
+
7
+ # @param [String] name
8
+ # @param [Hash] json
9
+ # @param [Array] multi_select
10
+ def initialize(name, json: nil, start_date: nil, end_date: nil, time_zone: nil)
11
+ super(name, json: json)
12
+ @end_date = end_date
13
+ @start_date = start_date || end_date
14
+ @time_zone = time_zone
15
+ end
16
+
17
+ # @return [Hash] created json
18
+ def create_json
19
+ if @end_date
20
+ {"date" => {"start" => value_str(@start_date || @end_date), "end" => value_str(@end_date)}}
21
+ elsif @start_date
22
+ {"date" => {"start" => value_str(@start_date)}}
23
+ else
24
+ {"date" => @json || {}}
25
+ end
26
+ end
27
+
28
+ # @param [Hash] multi_select
29
+ # @return [Array, nil] settled array
30
+ def start_date=(start_date)
31
+ @will_update = true
32
+ @start_date = start_date
33
+ @end_date = nil if @start_date.class != @end_date.class || @start_date > @end_date
34
+ end
35
+
36
+ # @param [Hash] multi_select
37
+ # @return [Array, nil] settled array
38
+ def end_date=(end_date)
39
+ @will_update = true
40
+ @end_date = end_date
41
+ @end_date = nil if @start_date.class != @end_date.class || @start_date > @end_date
42
+ end
6
43
  end
7
44
  end
@@ -4,5 +4,25 @@ module NotionRubyMapping
4
4
  # MultiSelect property
5
5
  class MultiSelectProperty < MultiProperty
6
6
  TYPE = "multi_select"
7
+
8
+ # @param [String] name
9
+ # @param [Hash] json
10
+ # @param [Array] multi_select
11
+ def initialize(name, json: nil, multi_select: nil)
12
+ super(name, json: json)
13
+ @multi_select = multi_select ? Array(multi_select) : nil
14
+ end
15
+
16
+ # @return [Hash] created json
17
+ def create_json
18
+ {"multi_select" => @multi_select ? (@multi_select.map { |v| {"name" => v} }) : @json} || {}
19
+ end
20
+
21
+ # @param [Hash] multi_select
22
+ # @return [Array, nil] settled array
23
+ def multi_select=(multi_select)
24
+ @will_update = true
25
+ @multi_select = multi_select ? Array(multi_select) : nil
26
+ end
7
27
  end
8
28
  end
@@ -14,7 +14,6 @@ module NotionRubyMapping
14
14
  @wait = 0
15
15
  end
16
16
  attr_reader :object_hash, :client
17
- attr_accessor :use_cache
18
17
 
19
18
  # @param [String] notion_token
20
19
  # @return [NotionRubyMapping::NotionCache] self (NotionCache.instance)
@@ -9,15 +9,17 @@ module NotionRubyMapping
9
9
  TYPE = "number"
10
10
 
11
11
  # @param [String] name Property name
12
- # @param [Number, Fixnum] number Number value (optional)
13
- # @param [String] format Format string (optional, default: "number")
14
- def initialize(name, number: nil)
15
- super(name)
12
+ # @param [Hash] json
13
+ # @param [Float] number Number value (optional)
14
+ def initialize(name, json: nil, number: nil)
15
+ super(name, json: json)
16
16
  @number = number
17
17
  end
18
+ attr_reader :number
18
19
 
20
+ # @return [Hash]
19
21
  def create_json
20
- {"type" => "number", "number" => @number}
22
+ {"number" => @number || @json}
21
23
  end
22
24
 
23
25
  def number=(n)
@@ -3,18 +3,16 @@
3
3
  module NotionRubyMapping
4
4
  # Notion page object
5
5
  class Page < Base
6
- def self.find(key)
7
- NotionCache.instance.page key
6
+ def self.find(id)
7
+ NotionCache.instance.page id
8
8
  end
9
9
 
10
- # @param [String] id page_id (with or without "-")
11
- # @param [Payload] payload
12
10
  def update
13
11
  update_json @nc.update_page(@id, create_json)
14
12
  end
15
-
13
+
16
14
  # @return [Hash]
17
- def reload
15
+ def reload_json
18
16
  @nc.page_json @id
19
17
  end
20
18
  end
@@ -4,10 +4,12 @@ module NotionRubyMapping
4
4
  # abstract class for property
5
5
  class Property
6
6
  # @param [String] name Property name
7
+ # @param [Hash] json
7
8
  # @return [Property] generated Property object
8
- def initialize(name)
9
+ def initialize(name, json: nil)
9
10
  @name = name
10
11
  @will_update = false
12
+ @json = json
11
13
  end
12
14
  attr_reader :name
13
15
  attr_accessor :will_update
@@ -25,12 +27,18 @@ module NotionRubyMapping
25
27
  end
26
28
 
27
29
  # @param [String] key
28
- # @param [Hash] json
29
- # @return [NotionRubyMapping::NumberProperty, nil] generated Property object
30
- def self.create_from_json(key, json)
31
- case json["type"]
32
- when "number"
33
- NumberProperty.new key, number: json["number"]
30
+ # @param [Hash] input_json
31
+ # @return [NotionRubyMapping::Property, nil] generated Property object
32
+ def self.create_from_json(key, input_json)
33
+ keys = input_json.keys
34
+ if keys.include? "number"
35
+ NumberProperty.new key, json: input_json["number"]
36
+ elsif keys.include? "select"
37
+ SelectProperty.new key, json: input_json["select"]
38
+ elsif keys.include? "multi_select"
39
+ MultiSelectProperty.new key, json: input_json["multi_select"]
40
+ elsif keys.include? "date"
41
+ DateProperty.new key, json: input_json["date"]
34
42
  else
35
43
  nil
36
44
  end
@@ -6,5 +6,25 @@ module NotionRubyMapping
6
6
  include EqualsDoesNotEqual
7
7
  include IsEmptyIsNotEmpty
8
8
  TYPE = "select"
9
+
10
+ # @param [String] name Property name
11
+ # @param [Hash] json
12
+ # @param [String] select String value (optional)
13
+ def initialize(name, json: nil, select: nil)
14
+ super(name, json: json)
15
+ @select = select
16
+ end
17
+
18
+ # @return [Hash]
19
+ def create_json
20
+ {"select" => @select ? {"name" => @select} : @json} || {}
21
+ end
22
+
23
+ # @param [String] select
24
+ # @return [String] settled value
25
+ def select=(select)
26
+ @will_update = true
27
+ @select = select
28
+ end
9
29
  end
10
- end
30
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.4"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notion_ruby_mapping
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroyuki KOBAYASHI
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-17 00:00:00.000000000 Z
11
+ date: 2022-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: notion-ruby-client
@@ -115,6 +115,7 @@ files:
115
115
  - env.yml.sample
116
116
  - images/post_set_icon.png
117
117
  - images/pre_set_icon.png
118
+ - images/serial_number.png
118
119
  - lib/notion_ruby_mapping.rb
119
120
  - lib/notion_ruby_mapping/base.rb
120
121
  - lib/notion_ruby_mapping/block.rb