notion 1.0.6 → 1.0.7

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: 04355fc0473c8f446e79ec4904105c0fb4fcb8a436032099e59d41ce6abbc9fc
4
- data.tar.gz: df7cf2d629fcd8c9dcf076a3186c2d49deef0f73454c051a2ad5530ffa98501d
3
+ metadata.gz: 260bf00a0f7ffc3dec1cbf364c69815e5dcbf829677710e5b740bd58476ca9f7
4
+ data.tar.gz: 7220c9399aca9756e4773dda4b5fbdd573bd3decfe32758797ea11e309e6f0e0
5
5
  SHA512:
6
- metadata.gz: 2359d17e2408b50b04a3ae1c9b4015de93b4e7d92b06a5b64d6cc8af48380e2affa7e4eb17d20e35def404116bdecef838bd9ec19c9c39867c05116de02eca40
7
- data.tar.gz: febb7a95cc0bd70c59e921a6479b4e4f7c80b6ad1443366aef0658358877876a04b37f9993827e15fe6d9eb0386efb7fa082063b0e6758e8ddeead1bb9dd5aa2
6
+ metadata.gz: d5933ae50944a7f77e6c68410d4291935b769869b59f71817dabd98df2e5a50f86f6200c4b7bc9800249e274c4e3ecd1e5d339df66cb08e258c50c25308e3bba
7
+ data.tar.gz: e01f29a16242b81b586d7852062efe030a760f30620096a691be6ee47c45de3d5f1b1ef7ce0f166a6c3328e3972ca470aaffd51b235f8d9f2f6c8b6ead69c193
@@ -33,7 +33,9 @@ module NotionAPI
33
33
  transaction_id = extract_id(SecureRandom.hex(16))
34
34
  space_id = extract_id(SecureRandom.hex(16))
35
35
  new_block_id = extract_id(SecureRandom.hex(16))
36
- schema = extract_collection_schema(@collection_id, @view_id)
36
+ collection_data = extract_collection_data(collection_id, view_id)
37
+ last_row_id = collection_data["collection_view"][@view_id]["value"]["page_sort"][-1]
38
+ schema = collection_data['collection'][collection_id]['value']['schema']
37
39
  keys = schema.keys
38
40
  col_map = {}
39
41
  keys.map { |key| col_map[schema[key]["name"]] = key }
@@ -47,17 +49,24 @@ module NotionAPI
47
49
  instantiate_row = Utils::CollectionViewComponents.add_new_row(new_block_id)
48
50
  set_block_alive = Utils::CollectionViewComponents.set_collection_blocks_alive(new_block_id, @collection_id)
49
51
  new_block_edited_time = Utils::BlockComponents.last_edited_time(new_block_id)
50
- parent_edited_time = Utils::BlockComponents.last_edited_time(@parent_id)
52
+ page_sort = Utils::BlockComponents.row_location_add(last_row_id, new_block_id, @view_id)
51
53
 
52
54
  operations = [
53
55
  instantiate_row,
54
56
  set_block_alive,
55
57
  new_block_edited_time,
56
- parent_edited_time,
58
+ page_sort
57
59
  ]
58
60
 
59
61
  data.keys.each_with_index do |col_name, j|
60
62
  unless col_map.keys.include?(col_name.to_s); raise ArgumentError, "Column '#{col_name.to_s}' does not exist." end
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"].map {|option| option["value"]}
65
+ if !options.include?(data[col_name])
66
+ create_new_option = Utils::CollectionViewComponents.add_new_option(col_map[col_name.to_s], data[col_name], @collection_id)
67
+ operations.push(create_new_option)
68
+ end
69
+ end
61
70
  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"])
62
71
  operations.push(child_component)
63
72
  end
@@ -200,14 +200,14 @@ module Utils
200
200
 
201
201
  args = if command == "listAfter"
202
202
  {
203
- after: target || block_id,
204
- id: new_block_id || block_id,
205
- }
203
+ after: target || block_id,
204
+ id: new_block_id || block_id,
205
+ }
206
206
  else
207
207
  {
208
- before: target || block_id,
209
- id: new_block_id || block_id,
210
- }
208
+ before: target || block_id,
209
+ id: new_block_id || block_id,
210
+ }
211
211
  end
212
212
 
213
213
  {
@@ -219,6 +219,21 @@ module Utils
219
219
  }
220
220
  end
221
221
 
222
+ def self.row_location_add(last_row_id, block_id, view_id)
223
+ {
224
+ "table": "collection_view",
225
+ "id": view_id,
226
+ "path": [
227
+ "page_sort"
228
+ ],
229
+ "command": "listAfter",
230
+ "args": {
231
+ "after": last_row_id,
232
+ "id": block_id
233
+ }
234
+ }
235
+ end
236
+
222
237
  def self.block_location_remove(block_parent_id, block_id)
223
238
  # ! removes a notion block
224
239
  # ! block_parent_id -> the parent ID of the block to remove : ``str``
@@ -276,9 +291,9 @@ module Utils
276
291
  def self.add_emoji_icon(block_id, icon)
277
292
  {
278
293
  id: block_id,
279
- table:"block",
280
- path:["format","page_icon"],
281
- command:"set","args": icon
294
+ table: "block",
295
+ path: ["format", "page_icon"],
296
+ command: "set", "args": icon,
282
297
  }
283
298
  end
284
299
  end
@@ -383,14 +398,14 @@ module Utils
383
398
  # ! new_block_id -> id of the new block
384
399
  # ! data -> json data to insert into table.
385
400
  col_names = data[0].keys
386
- data_mappings = {Integer => "number", String => "text", Array => "text", Float => "number", Date => "date"}
401
+ data_mappings = { Integer => "number", String => "text", Array => "text", Float => "number", Date => "date" }
387
402
  exceptions = [ArgumentError, TypeError]
388
403
  data_types = col_names.map do |name|
389
404
  # TODO: this is a little hacky... should probably think about a better way or add a requirement for user input to match a certain criteria.
390
- begin
405
+ begin
391
406
  DateTime.parse(data[0][name]) ? data_mappings[Date] : nil
392
407
  rescue *exceptions
393
- data_mappings[data[0][name].class]
408
+ data_mappings[data[0][name].class]
394
409
  end
395
410
  end
396
411
 
@@ -403,18 +418,18 @@ module Utils
403
418
  end
404
419
  end
405
420
  return {
406
- id: collection_id,
407
- table: "collection",
408
- path: [],
409
- command: "update",
410
- args: {
411
- id: collection_id,
412
- schema: schema_conf,
413
- parent_id: new_block_id,
414
- parent_table: "block",
415
- alive: true,
416
- },
417
- }, data_types
421
+ id: collection_id,
422
+ table: "collection",
423
+ path: [],
424
+ command: "update",
425
+ args: {
426
+ id: collection_id,
427
+ schema: schema_conf,
428
+ parent_id: new_block_id,
429
+ parent_table: "block",
430
+ alive: true,
431
+ },
432
+ }, data_types
418
433
  end
419
434
 
420
435
  def self.set_collection_title(collection_title, collection_id)
@@ -440,6 +455,11 @@ module Utils
440
455
  # ! column -> the name of the column to insert data into.
441
456
  # ! value -> the value to insert into the column.
442
457
  # ! mapping -> the column data type.
458
+ simple_mappings = ["title", "text", "phone_number", "email", "url", "number", "checkbox", "select", "multi_select"]
459
+ datetime_mappings = ["date"]
460
+ media_mappings = ["file"]
461
+ person_mappings = ["person"]
462
+
443
463
  table = "block"
444
464
  path = [
445
465
  "properties",
@@ -447,12 +467,47 @@ module Utils
447
467
  ]
448
468
  command = "set"
449
469
 
470
+ if simple_mappings.include?(mapping)
471
+ args = [[value]]
472
+ elsif media_mappings.include?(mapping)
473
+ args = [[value, [["a", value]]]]
474
+ elsif datetime_mappings.include?(mapping)
475
+ args = [["‣", [["d", { "type": "date", "start_date": value }]]]]
476
+ elsif person_mappings.include?(mapping)
477
+ args = [["‣",
478
+ [["u", value]]
479
+ ]]
480
+ else
481
+ raise SchemaTypeError, "Invalid property type: #{mapping}"
482
+ end
483
+
450
484
  {
451
- id: block_id,
452
485
  table: table,
486
+ id: block_id,
487
+ command: command,
453
488
  path: path,
489
+ args: args,
490
+ }
491
+ end
492
+
493
+ def self.add_new_option(column, value, collection_id)
494
+ table = "collection"
495
+ path = ["schema", column, "options"]
496
+ command = "keyedObjectListAfter"
497
+ args = {
498
+ "value": {
499
+ "id": SecureRandom.hex(16),
500
+ "value": value,
501
+ "color": "green"
502
+ }
503
+ }
504
+
505
+ {
506
+ table: table,
507
+ id: collection_id,
454
508
  command: command,
455
- args: mapping == "date" ? [["‣",[["d",{"type": "date","start_date": value}]]]] : [[value]],
509
+ path: path,
510
+ args: args,
456
511
  }
457
512
  end
458
513
 
@@ -505,45 +560,45 @@ module Utils
505
560
  args["format"] = {
506
561
  "table_properties" => [
507
562
  {
508
- "property" => "title",
509
- "visible" => true,
510
- "width" => 280,
511
- },
563
+ "property" => "title",
564
+ "visible" => true,
565
+ "width" => 280,
566
+ },
512
567
  {
513
- "property" => "aliases",
514
- "visible" => true,
515
- "width" => 200,
516
- },
568
+ "property" => "aliases",
569
+ "visible" => true,
570
+ "width" => 200,
571
+ },
517
572
  {
518
- "property" => "category",
519
- "visible" => true,
520
- "width" => 200,
521
- },
573
+ "property" => "category",
574
+ "visible" => true,
575
+ "width" => 200,
576
+ },
522
577
  {
523
- "property" => "description",
524
- "visible" => true,
525
- "width" => 200,
526
- },
578
+ "property" => "description",
579
+ "visible" => true,
580
+ "width" => 200,
581
+ },
527
582
  {
528
- "property" => "ios_version",
529
- "visible" => true,
530
- "width" => 200,
531
- },
583
+ "property" => "ios_version",
584
+ "visible" => true,
585
+ "width" => 200,
586
+ },
587
+ {
588
+ "property" => "tags",
589
+ "visible" => true,
590
+ "width" => 200,
591
+ },
532
592
  {
533
- "property" => "tags",
534
- "visible" => true,
535
- "width" => 200,
536
- },
537
- {
538
593
  "property" => "phone",
539
594
  "visible" => true,
540
595
  "width" => 200,
541
596
  },
542
597
  {
543
- "property" => "unicode_version",
544
- "visible" => true,
545
- "width" => 200,
546
- }
598
+ "property" => "unicode_version",
599
+ "visible" => true,
600
+ "width" => 200,
601
+ },
547
602
  ],
548
603
  }
549
604
  {
@@ -563,11 +618,11 @@ module Utils
563
618
  table = "block"
564
619
  path = [
565
620
  "properties",
566
- column_name
621
+ column_name,
567
622
  ]
568
623
  command = "set"
569
624
  args = [[
570
- new_value
625
+ new_value,
571
626
  ]]
572
627
 
573
628
  {
@@ -575,11 +630,18 @@ module Utils
575
630
  table: table,
576
631
  path: path,
577
632
  command: command,
578
- args: args
633
+ args: args,
579
634
  }
580
635
  end
581
636
  end
582
637
 
638
+ class SchemaTypeError < StandardError
639
+ def initialize(msg="Custom exception that is raised when an invalid property type is passed as a mapping.", exception_type="schema_type")
640
+ @exception_type = exception_type
641
+ super(msg)
642
+ end
643
+ end
644
+
583
645
  def build_payload(operations, request_ids)
584
646
  # ! properly formats the payload for Notions backend.
585
647
  # ! operations -> an array of hashes that define the operations to perform : ``Array[Hash]``
@@ -600,4 +662,4 @@ module Utils
600
662
  }
601
663
  payload
602
664
  end
603
- end
665
+ end
@@ -1,3 +1,3 @@
1
1
  module NotionAPI
2
- VERSION = '1.0.6'
2
+ VERSION = '1.0.7'
3
3
  end
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.0.6
4
+ version: 1.0.7
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-04 00:00:00.000000000 Z
11
+ date: 2020-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty