notion_ruby_mapping 0.3.0 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +34 -11
- data/lib/notion_ruby_mapping/base.rb +12 -4
- data/lib/notion_ruby_mapping/database.rb +6 -1
- data/lib/notion_ruby_mapping/property.rb +2 -0
- data/lib/notion_ruby_mapping/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1d0dd6fcc42d250f8da729a07d788c2f2dd49739cc8810edde17c4eea913f06
|
4
|
+
data.tar.gz: d9c7822db6b4d40505626d95e7e947c8436ca9d2ff0d61409cf15dba30adb97d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b568105ff9e1acb644432233c3d2e8de8c7b3151d27f014a331437b90459d3afde4e25f4fd2a0e429149f781fe01cb77664fd12ac00e5990ab64780a031faf1d
|
7
|
+
data.tar.gz: 1f5871c01afe0aae9d15a978b3dfc85ed641b5017210247dc9e80b451afb857e15f0404aaefe3983a27596759a1e7bc4997c21d3c970742d342e6948f035bfd6
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@ Development note is here. → [Idea note of "notion_ruby_mapping"](https://www.n
|
|
6
6
|
|
7
7
|
## Table of Contents
|
8
8
|
|
9
|
-
- [
|
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)
|
@@ -31,6 +31,7 @@ Development note is here. → [Idea note of "notion_ruby_mapping"](https://www.n
|
|
31
31
|
- [4.2.6 Add a database property](#426-add-a-database-property)
|
32
32
|
- [4.2.7 Rename a database property](#427-rename-a-database-property)
|
33
33
|
- [4.2.8 Remove database properties](#428-remove-database-properties)
|
34
|
+
- [4.2.9 other methods](#429-other-methods)
|
34
35
|
- [4.3 List class](#43-list-class)
|
35
36
|
- [4.4 Block class](#44-block-class)
|
36
37
|
- [4.5 Property classes](#45-property-classes)
|
@@ -133,12 +134,13 @@ page = Page.find "c01166c6-13ae-45cb-b968-18b4ef2f5a77" # Notion API call
|
|
133
134
|
```
|
134
135
|
|
135
136
|
- result of dry run
|
137
|
+
|
136
138
|
```bash
|
137
139
|
#!/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'
|
140
|
+
curl 'https://api.notion.com/v1/pages/c01166c6-13ae-45cb-b968-18b4ef2f5a77' \
|
141
|
+
-H 'Notion-Version: 2022-02-22' \
|
142
|
+
-H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
|
143
|
+
-H 'Content-Type: application/json'
|
142
144
|
```
|
143
145
|
|
144
146
|
`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.
|
@@ -217,7 +219,7 @@ db = Database.find "c37a2c66-e3aa-4a0d-a447-73de3b80c253" # Notion API call
|
|
217
219
|
curl 'https://api.notion.com/v1/databases/c37a2c66-e3aa-4a0d-a447-73de3b80c253'\
|
218
220
|
-H 'Notion-Version: 2022-02-22'\
|
219
221
|
-H 'Authorization: Bearer '"$NOTION_API_KEY"''\
|
220
|
-
-H 'Content-Type: application/json'
|
222
|
+
-H 'Content-Type: application/json'
|
221
223
|
```
|
222
224
|
|
223
225
|
`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.
|
@@ -522,18 +524,22 @@ print db.query_database query, dry_run: true
|
|
522
524
|
|
523
525
|
```
|
524
526
|
|
525
|
-
|
526
527
|
#### 4.2.3 Create child page
|
527
528
|
|
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
|
+
`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. Properties of the created child page are automatically assigned using the parent database. if a block is provided, the method will yield the new Page object and the properties (PropertyCache object) to that block for initialization.
|
529
530
|
|
530
531
|
```Ruby
|
531
|
-
page = db.create_child_page
|
532
|
-
|
532
|
+
page = db.create_child_page do |p, pp|
|
533
|
+
# p is the new Page object
|
534
|
+
# pp is the properties of the new Page object (PropertyCache Object)
|
535
|
+
p.set_icon emoji: "🎉"
|
536
|
+
pp["Name"] << "New Page"
|
537
|
+
end
|
533
538
|
page.save
|
534
539
|
```
|
535
540
|
|
536
541
|
- result of dry run
|
542
|
+
|
537
543
|
```bash
|
538
544
|
#!/bin/sh
|
539
545
|
curl -X POST 'https://api.notion.com/v1/pages'\
|
@@ -546,6 +552,7 @@ curl -X POST 'https://api.notion.com/v1/pages'\
|
|
546
552
|
#### 4.2.4 Create database
|
547
553
|
|
548
554
|
`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.
|
555
|
+
|
549
556
|
```Ruby
|
550
557
|
page = Page.find "a sample page id"
|
551
558
|
db = parent_page.create_child_database "New database title",
|
@@ -666,6 +673,19 @@ curl -X PATCH 'https://api.notion.com/v1/databases/c7697137d49f49c2bbcdd6a665c4f
|
|
666
673
|
--data '{"properties":{"renamed number property":null,"renamed url property":null}}'
|
667
674
|
```
|
668
675
|
|
676
|
+
#### 4.2.9 other methods
|
677
|
+
|
678
|
+
- `Database.find id, dry_run: true` create shell script for verification.
|
679
|
+
- `db.save` call Notion API, and so on and replace object information.
|
680
|
+
- `db.save dry_run: true` create shell script for verification.
|
681
|
+
- `db.new_record?` returns true if the database was generated by `create_child_database`.
|
682
|
+
- `db.database_title` returns plain_text string of `Database`.
|
683
|
+
- `db.title` returns plain_text string of `TitleProperty`.
|
684
|
+
- `db.icon` returns JSON hash for the page icon.
|
685
|
+
- `db[key]` returns a hash or an array object except "properties".
|
686
|
+
- `db.created_time` returns CreatedTimeProperty for filter
|
687
|
+
- `db.last_edited_time` returns LastEditedTimeProperty for filter
|
688
|
+
|
669
689
|
### 4.3 List class
|
670
690
|
|
671
691
|
`db.query_database` and other API list results returns a List object.
|
@@ -1016,7 +1036,6 @@ p rp.property_values_json
|
|
1016
1036
|
# Result => {"rp"=>{"type"=>"relation", "relation"=>[{"id"=>"R2"}, {"id"=>"R3"}]}}
|
1017
1037
|
```
|
1018
1038
|
|
1019
|
-
|
1020
1039
|
#### 4.5.4 create or update values for Database properties
|
1021
1040
|
|
1022
1041
|
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.
|
@@ -1046,6 +1065,7 @@ print sp.property_schema_json
|
|
1046
1065
|
```
|
1047
1066
|
|
1048
1067
|
If you want to edit existing values, you should access `edit_select_options` array. It sets `will_update` flag to true.
|
1068
|
+
|
1049
1069
|
```Ruby
|
1050
1070
|
sp.edit_select_options[0]["name"] = "new S1"
|
1051
1071
|
p sp.property_values_json
|
@@ -1064,6 +1084,7 @@ print msp.property_schema_json
|
|
1064
1084
|
```
|
1065
1085
|
|
1066
1086
|
If you want to edit existing values, you should access `edit_multi_select_options` array. It sets `will_update` flag to true.
|
1087
|
+
|
1067
1088
|
```Ruby
|
1068
1089
|
msp.edit_multi_select_options[0]["name"] = "new MS1"
|
1069
1090
|
p msp.property_values_json
|
@@ -1251,6 +1272,8 @@ textMentionObjects = RichTextArray.new "title", [TextObject.new("A TextObject"),
|
|
1251
1272
|
|
1252
1273
|
## 6. ChangeLog
|
1253
1274
|
|
1275
|
+
- 2022/3/27 create_child_page can receive a block for initialization.
|
1276
|
+
- 2022/3/27 properties of a created child page are automatically assigned using the parent database.
|
1254
1277
|
- 2022/3/25 added create_child_database, update_database, add_property, rename_property and remove_property
|
1255
1278
|
- 2022/3/17 added template_mention objects, tools/an command
|
1256
1279
|
- 2022/3/16 added database.create_child_page and base.save instead of base.update/create
|
@@ -19,7 +19,10 @@ module NotionRubyMapping
|
|
19
19
|
@property_cache = nil
|
20
20
|
@created_time = nil
|
21
21
|
@last_edited_time = nil
|
22
|
+
return if assign.empty?
|
23
|
+
|
22
24
|
assign.each_slice(2) { |(klass, key)| assign_property(klass, key) }
|
25
|
+
@json ||= {}
|
23
26
|
end
|
24
27
|
attr_reader :json, :id
|
25
28
|
|
@@ -41,7 +44,7 @@ module NotionRubyMapping
|
|
41
44
|
|
42
45
|
# @return [NotionRubyMapping::CreatedTimeProperty]
|
43
46
|
def created_time
|
44
|
-
@created_time ||= CreatedTimeProperty.new("
|
47
|
+
@created_time ||= CreatedTimeProperty.new("__timestamp__")
|
45
48
|
end
|
46
49
|
|
47
50
|
# @return [TrueClass, FalseClass] true if Database object
|
@@ -54,6 +57,11 @@ module NotionRubyMapping
|
|
54
57
|
self["icon"]
|
55
58
|
end
|
56
59
|
|
60
|
+
# @return [NotionRubyMapping::LastEditedTimeProperty]
|
61
|
+
def last_edited_time
|
62
|
+
@last_edited_time ||= LastEditedTimeProperty.new("__timestamp__")
|
63
|
+
end
|
64
|
+
|
57
65
|
# @return [Boolean] true if new record
|
58
66
|
def new_record?
|
59
67
|
@new_record
|
@@ -168,7 +176,7 @@ module NotionRubyMapping
|
|
168
176
|
# @param [Hash] json
|
169
177
|
# @return [NotionRubyMapping::Base]
|
170
178
|
def update_json(json)
|
171
|
-
raise StandardError, json.inspect unless json["object"] != "error" && @json.nil? || @json["type"] == json["type"]
|
179
|
+
raise StandardError, json.inspect unless json["object"] != "error" && (@json.nil? || @json["type"] == json["type"])
|
172
180
|
|
173
181
|
@json = json
|
174
182
|
@id = @nc.hex_id(@json["id"])
|
@@ -189,11 +197,11 @@ module NotionRubyMapping
|
|
189
197
|
shell = [
|
190
198
|
"#!/bin/sh\ncurl #{method == :get ? "" : "-X #{method.to_s.upcase}"} 'https://api.notion.com/#{path}'",
|
191
199
|
" -H 'Notion-Version: 2022-02-22'",
|
192
|
-
" -H 'Authorization: Bearer '\"$NOTION_API_KEY\"''"
|
200
|
+
" -H 'Authorization: Bearer '\"$NOTION_API_KEY\"''",
|
193
201
|
]
|
194
202
|
shell << " -H 'Content-Type: application/json'" unless path == :get
|
195
203
|
shell << " --data '#{JSON.generate json}'" if json
|
196
|
-
shell.join("
|
204
|
+
shell.join(" \\\n")
|
197
205
|
end
|
198
206
|
|
199
207
|
protected
|
@@ -30,7 +30,12 @@ module NotionRubyMapping
|
|
30
30
|
# @param [Array<Property, Class, String>] assign
|
31
31
|
# @return [NotionRubyMapping::Base]
|
32
32
|
def create_child_page(*assign)
|
33
|
-
|
33
|
+
assign = properties.map { |p| [p.class, p.name] }.flatten if assign.empty?
|
34
|
+
page = Page.new assign: assign, parent: {"database_id" => @id}
|
35
|
+
pp = page.properties
|
36
|
+
pp.clear_will_update
|
37
|
+
yield page, pp if block_given?
|
38
|
+
page
|
34
39
|
end
|
35
40
|
|
36
41
|
# @return [NotionRubyMapping::RichTextArray]
|
@@ -91,6 +91,8 @@ module NotionRubyMapping
|
|
91
91
|
def make_filter_query(key, value, rollup = nil, rollup_type = nil)
|
92
92
|
if rollup
|
93
93
|
Query.new filter: {"property" => @name, rollup => {rollup_type => {key => value}}}
|
94
|
+
elsif @name == "__timestamp__"
|
95
|
+
Query.new filter: {"timestamp" => type, type => {key => value}}
|
94
96
|
else
|
95
97
|
Query.new filter: {"property" => @name, type => {key => value}}
|
96
98
|
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.3.
|
4
|
+
version: 0.3.3
|
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-03-
|
11
|
+
date: 2022-03-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|