notion 1.1.0 → 1.1.1
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 +8 -4
- data/lib/notion_api/core.rb +13 -4
- data/lib/notion_api/notion_types/collection_view_blocks.rb +47 -15
- data/lib/notion_api/utils.rb +3 -2
- data/lib/notion_api/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: 761cf6b775c714eb4b8eb65710baf3407eaa6061d717b18e59510a048c0edf7f
|
4
|
+
data.tar.gz: 7f3ba657e798d7e9a24b0aa77ee098462fdb164f958309fdc12ca39d54b78231
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 281bc33e957a074c02bf2866e526aa866d2f75945f7c9dbf9ec0a41a5ac7b5795fe2cedf429e8b2e6220b16550d2bf8c9c7107832672bf4915124fb8c9dae167
|
7
|
+
data.tar.gz: f02fde242ad8ebfeef9edc3c1850270a8d1459a93b1fafd83ec88eae323ca4dba0ae07bacfc9ef38e053012e81c71b2690c7fe6b68d88e31ca7898e3a4592df7
|
data/README.md
CHANGED
@@ -310,8 +310,12 @@ From here, you can instantiate the Notion Client with the following code:
|
|
310
310
|
)
|
311
311
|
```
|
312
312
|
### Retrieve a full-page Collection View
|
313
|
-
|
314
|
-
|
315
|
-
|
313
|
+
A full-page collection view must have a URL that follows the below pattern:
|
314
|
+
https://www.notion.so/danmurphy/[page-id]?v=[view-id]
|
315
|
+
Then, it can be retrieved with the following code:
|
316
|
+
```ruby
|
317
|
+
>>> @client = NotionAPI::Client.new(
|
318
|
+
"<insert_v2_token_here>"
|
319
|
+
)
|
320
|
+
>>> @client.get_page(https://www.notion.so/danmurphy/[page-id]?v=[view-id])
|
316
321
|
```
|
317
|
-
To avoid this, you must pass only the ID of the full-page collection-view to the `get_page` method. This is next up on the features list, so passing the full URL will be supported soon:smile:
|
data/lib/notion_api/core.rb
CHANGED
@@ -235,17 +235,26 @@ module NotionAPI
|
|
235
235
|
# ! parse and clean the URL or ID object provided.
|
236
236
|
# ! url_or_id -> the block ID or URL : ``str``
|
237
237
|
http_or_https = url_or_id.match(/^(http|https)/) # true if http or https in url_or_id...
|
238
|
+
collection_view_match = url_or_id.match(/(\?v=)/)
|
239
|
+
|
238
240
|
if (url_or_id.length == 36) && ((url_or_id.split('-').length == 5) && !http_or_https)
|
239
241
|
# passes if url_or_id is perfectly formatted already...
|
240
242
|
url_or_id
|
241
|
-
elsif (http_or_https && (url_or_id.split('-').last.length == 32)) || (!http_or_https && (url_or_id.length == 32))
|
243
|
+
elsif (http_or_https && (url_or_id.split('-').last.length == 32)) || (!http_or_https && (url_or_id.length == 32)) || (collection_view_match)
|
242
244
|
# passes if either:
|
243
245
|
# 1. a URL is passed as url_or_id and the ID at the end is 32 characters long or
|
244
246
|
# 2. a URL is not passed and the ID length is 32 [aka unformatted]
|
245
247
|
pattern = [8, 13, 18, 23]
|
246
|
-
|
247
|
-
|
248
|
-
|
248
|
+
if collection_view_match
|
249
|
+
id_without_view = url_or_id.split('?')[0]
|
250
|
+
clean_id = id_without_view.split('/').last
|
251
|
+
pattern.each { |index| clean_id.insert(index, '-') }
|
252
|
+
clean_id
|
253
|
+
else
|
254
|
+
id = url_or_id.split('-').last
|
255
|
+
pattern.each { |index| id.insert(index, '-') }
|
256
|
+
id
|
257
|
+
end
|
249
258
|
else
|
250
259
|
raise ArgumentError, 'Expected a Notion page URL or a page ID. Please consult the documentation for further information.'
|
251
260
|
end
|
@@ -35,7 +35,7 @@ module NotionAPI
|
|
35
35
|
new_block_id = extract_id(SecureRandom.hex(16))
|
36
36
|
collection_data = extract_collection_data(collection_id, view_id)
|
37
37
|
last_row_id = collection_data["collection_view"][@view_id]["value"]["page_sort"][-1]
|
38
|
-
schema = collection_data[
|
38
|
+
schema = collection_data["collection"][collection_id]["value"]["schema"]
|
39
39
|
keys = schema.keys
|
40
40
|
col_map = {}
|
41
41
|
keys.map { |key| col_map[schema[key]["name"]] = key }
|
@@ -55,16 +55,19 @@ module NotionAPI
|
|
55
55
|
instantiate_row,
|
56
56
|
set_block_alive,
|
57
57
|
new_block_edited_time,
|
58
|
-
page_sort
|
58
|
+
page_sort,
|
59
59
|
]
|
60
60
|
|
61
61
|
data.keys.each_with_index do |col_name, j|
|
62
62
|
unless col_map.keys.include?(col_name.to_s); raise ArgumentError, "Column '#{col_name.to_s}' does not exist." end
|
63
63
|
if %q[select multi_select].include?(schema[col_map[col_name.to_s]]["type"])
|
64
|
-
options = schema[col_map[col_name.to_s]]["options"].nil? ? [] : schema[col_map[col_name.to_s]]["options"].map {|option| option["value"]}
|
65
|
-
|
66
|
-
|
67
|
-
|
64
|
+
options = schema[col_map[col_name.to_s]]["options"].nil? ? [] : schema[col_map[col_name.to_s]]["options"].map { |option| option["value"] }
|
65
|
+
multi_select_multi_options = data[col_name].split(",")
|
66
|
+
multi_select_multi_options.each do |option|
|
67
|
+
if !options.include?(option.strip)
|
68
|
+
create_new_option = Utils::CollectionViewComponents.add_new_option(col_map[col_name.to_s], option.strip, @collection_id)
|
69
|
+
operations.push(create_new_option)
|
70
|
+
end
|
68
71
|
end
|
69
72
|
end
|
70
73
|
child_component = Utils::CollectionViewComponents.insert_data(new_block_id, col_map[col_name.to_s], data[col_name], schema[col_map[col_name.to_s]]["type"])
|
@@ -83,7 +86,18 @@ module NotionAPI
|
|
83
86
|
unless response.code == 200; raise "There was an issue completing your request. Here is the response from Notion: #{response.body}, and here is the payload that was sent: #{operations}.
|
84
87
|
Please try again, and if issues persist open an issue in GitHub."; end
|
85
88
|
|
86
|
-
NotionAPI::CollectionViewRow.new(new_block_id, @parent_id, @collection_id, @view_id)
|
89
|
+
collection_row = NotionAPI::CollectionViewRow.new(new_block_id, @parent_id, @collection_id, @view_id)
|
90
|
+
|
91
|
+
properties = {}
|
92
|
+
data.keys.each do |col|
|
93
|
+
properties[col_map[col.to_s]] = [[data[col]]]
|
94
|
+
end
|
95
|
+
|
96
|
+
collection_data["block"][collection_row.id] = {"role"=>"editor", "value"=>{"id"=> collection_row.id, "version"=>12, "type"=>"page", "properties"=> properties, "created_time"=>1607253360000, "last_edited_time"=>1607253360000, "parent_id"=>"dde513c6-2428-4a5d-a830-7a67fdbf6b48", "parent_table"=>"collection", "alive"=>true, "created_by_table"=>"notion_user", "created_by_id"=>"0c5f02f3-495d-4b73-b1c5-9f6fe03a8c26", "last_edited_by_table"=>"notion_user", "last_edited_by_id"=>"0c5f02f3-495d-4b73-b1c5-9f6fe03a8c26", "shard_id"=>955090, "space_id"=>"f687f7de-7f4c-4a86-b109-941a8dae92d2"}}
|
97
|
+
row_data = collection_data["block"][collection_row.id]
|
98
|
+
create_singleton_methods_and_instance_variables(collection_row, row_data)
|
99
|
+
|
100
|
+
collection_row
|
87
101
|
end
|
88
102
|
|
89
103
|
def add_property(name, type)
|
@@ -214,6 +228,7 @@ module NotionAPI
|
|
214
228
|
end
|
215
229
|
clean_row_instances
|
216
230
|
end
|
231
|
+
|
217
232
|
def create_singleton_methods_and_instance_variables(row, row_data)
|
218
233
|
# ! creates singleton methods for each property in a CollectionView.
|
219
234
|
# ! row -> the block ID of the 'row' to retrieve: ``str``
|
@@ -223,7 +238,7 @@ module NotionAPI
|
|
223
238
|
column_mappings = schema.keys
|
224
239
|
column_hash = {}
|
225
240
|
column_names = column_mappings.map { |mapping| column_hash[mapping] = schema[mapping]["name"].downcase }
|
226
|
-
|
241
|
+
|
227
242
|
column_hash.keys.each_with_index do |column, i|
|
228
243
|
# loop over the column names...
|
229
244
|
# set instance variables for each column, allowing the dev to 'read' the column value
|
@@ -233,18 +248,23 @@ module NotionAPI
|
|
233
248
|
if row_data["value"]["properties"].nil? or row_data["value"]["properties"][column].nil?
|
234
249
|
value = ""
|
235
250
|
else
|
236
|
-
value = row_data["value"]["properties"][column][0][0]
|
251
|
+
value = row_data["value"]["properties"][column][0][0]
|
252
|
+
if ["‣"].include?(value.to_s)
|
253
|
+
value = row_data["value"]["properties"][column][0][1].flatten[-1]
|
254
|
+
end
|
237
255
|
end
|
238
256
|
|
239
257
|
row.instance_variable_set("@#{cleaned_column}", value)
|
240
258
|
CollectionViewRow.class_eval { attr_reader cleaned_column }
|
241
|
-
# then, define singleton methods for each column that are used to update the table cell
|
259
|
+
# then, define singleton methods for each column that are used to update the table cell
|
242
260
|
row.define_singleton_method("#{cleaned_column}=") do |new_value|
|
243
261
|
# neat way to get the name of the currently invoked method...
|
244
262
|
parsed_method = __method__.to_s[0...-1].split("_").join(" ")
|
245
263
|
cookies = Core.options["cookies"]
|
246
264
|
headers = Core.options["headers"]
|
247
|
-
|
265
|
+
|
266
|
+
p new_value, column_hash.key(parsed_method), column_names, schema
|
267
|
+
|
248
268
|
request_id = extract_id(SecureRandom.hex(16))
|
249
269
|
transaction_id = extract_id(SecureRandom.hex(16))
|
250
270
|
space_id = extract_id(SecureRandom.hex(16))
|
@@ -255,12 +275,24 @@ module NotionAPI
|
|
255
275
|
space_id: space_id,
|
256
276
|
}
|
257
277
|
|
258
|
-
|
278
|
+
update_property_value_hash = Utils::CollectionViewComponents.update_property_value(@id, column_hash.key(parsed_method), new_value)
|
259
279
|
|
260
280
|
operations = [
|
261
|
-
|
281
|
+
update_property_value_hash,
|
262
282
|
]
|
263
283
|
|
284
|
+
if %q[select multi_select].include?(schema[column_hash.key(parsed_method)]["type"])
|
285
|
+
p "ENTERED THE ABYSS"
|
286
|
+
options = schema[column_hash.key(parsed_method)]["options"].nil? ? [] : schema[column_hash.key(parsed_method)]["options"].map { |option| option["value"] }
|
287
|
+
multi_select_multi_options = new_value.split(",")
|
288
|
+
multi_select_multi_options.each do |option|
|
289
|
+
if !options.include?(option.strip)
|
290
|
+
create_new_option = Utils::CollectionViewComponents.add_new_option(column_hash.key(parsed_method), option.strip, @collection_id)
|
291
|
+
operations.push(create_new_option)
|
292
|
+
end
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
264
296
|
request_url = URLS[:UPDATE_BLOCK]
|
265
297
|
request_body = build_payload(operations, request_ids)
|
266
298
|
response = HTTParty.post(
|
@@ -270,8 +302,8 @@ module NotionAPI
|
|
270
302
|
headers: headers,
|
271
303
|
)
|
272
304
|
unless response.code == 200; raise "There was an issue completing your request. Here is the response from Notion: #{response.body}, and here is the payload that was sent: #{operations}.
|
273
|
-
Please try again, and if issues persist open an issue in GitHub.";end
|
274
|
-
|
305
|
+
Please try again, and if issues persist open an issue in GitHub."; end
|
306
|
+
|
275
307
|
# set the instance variable to the updated value!
|
276
308
|
_ = row.instance_variable_set("@#{__method__.to_s[0...-1]}", new_value)
|
277
309
|
row
|
data/lib/notion_api/utils.rb
CHANGED
@@ -491,11 +491,12 @@ module Utils
|
|
491
491
|
end
|
492
492
|
|
493
493
|
def self.add_new_option(column, value, collection_id)
|
494
|
-
colors = ["default", "gray", "brown", "orange", "yellow", "green", "blue", "purple", "pink", "red"]
|
495
|
-
random_color = colors[rand(0...colors.length)]
|
496
494
|
table = "collection"
|
497
495
|
path = ["schema", column, "options"]
|
498
496
|
command = "keyedObjectListAfter"
|
497
|
+
colors = ["default", "gray", "brown", "orange", "yellow", "green", "blue", "purple", "pink", "red"]
|
498
|
+
random_color = colors[rand(0...colors.length)]
|
499
|
+
|
499
500
|
args = {
|
500
501
|
"value": {
|
501
502
|
"id": SecureRandom.hex(16),
|
data/lib/notion_api/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: notion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Murphy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-12-
|
11
|
+
date: 2020-12-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|