notion 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,16 +1,16 @@
1
1
  module NotionAPI
2
2
 
3
- # good for just about anything (-:
4
- class TextBlock < BlockTemplate
5
- @notion_type = 'text'
6
- @type = 'text'
7
-
8
- def type
9
- NotionAPI::TextBlock.notion_type
10
- end
11
-
12
- class << self
13
- attr_reader :notion_type, :type
14
- end
3
+ # good for just about anything (-:
4
+ class TextBlock < BlockTemplate
5
+ @notion_type = "text"
6
+ @type = "text"
7
+
8
+ def type
9
+ NotionAPI::TextBlock.notion_type
10
+ end
11
+
12
+ class << self
13
+ attr_reader :notion_type, :type
15
14
  end
16
- end
15
+ end
16
+ end
@@ -1,60 +1,60 @@
1
1
  module NotionAPI
2
2
 
3
- # To-Do block: best for checklists and tracking to-dos.
4
- class TodoBlock < BlockTemplate
5
- @notion_type = 'to_do'
6
- @type = 'to_do'
7
-
8
- def type
9
- NotionAPI::TodoBlock.notion_type
10
- end
11
-
12
- class << self
13
- attr_reader :notion_type, :type
14
- end
15
-
16
- def checked=(checked_value)
17
- # ! change the checked property of the Todo Block.
18
- # ! checked_value -> boolean value used to determine whether the block should be checked [yes] or not [no] : ``str``
19
- # set static variables for request
20
- cookies = Core.options['cookies']
21
- headers = Core.options['headers']
22
- request_url = URLS[:UPDATE_BLOCK]
23
-
24
- # set unique values for request
25
- request_id = extract_id(SecureRandom.hex(16))
26
- transaction_id = extract_id(SecureRandom.hex(16))
27
- space_id = extract_id(SecureRandom.hex(16))
28
- request_ids = {
29
- request_id: request_id,
30
- transaction_id: transaction_id,
31
- space_id: space_id
32
- }
33
-
34
- if %w[yes no].include?(checked_value.downcase)
35
- checked_hash = Utils::BlockComponents.checked_todo(@id, checked_value.downcase)
36
- last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(@parent_id)
37
- last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)
38
-
39
- operations = [
40
- checked_hash,
41
- last_edited_time_parent_hash,
42
- last_edited_time_child_hash
43
- ]
44
- request_body = build_payload(operations, request_ids)
45
- response = HTTParty.post(
46
- request_url,
47
- body: request_body.to_json,
48
- cookies: cookies,
49
- headers: headers
50
- )
51
- 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}.
52
- Please try again, and if issues persist open an issue in GitHub."; end
53
-
54
- true
55
- else
56
- false
57
- end
3
+ # To-Do block: best for checklists and tracking to-dos.
4
+ class TodoBlock < BlockTemplate
5
+ @notion_type = "to_do"
6
+ @type = "to_do"
7
+
8
+ def type
9
+ NotionAPI::TodoBlock.notion_type
10
+ end
11
+
12
+ class << self
13
+ attr_reader :notion_type, :type
14
+ end
15
+
16
+ def checked=(checked_value)
17
+ # ! change the checked property of the Todo Block.
18
+ # ! checked_value -> boolean value used to determine whether the block should be checked [yes] or not [no] : ``str``
19
+ # set static variables for request
20
+ cookies = Core.options["cookies"]
21
+ headers = Core.options["headers"]
22
+ request_url = URLS[:UPDATE_BLOCK]
23
+
24
+ # set unique values for request
25
+ request_id = extract_id(SecureRandom.hex(16))
26
+ transaction_id = extract_id(SecureRandom.hex(16))
27
+ space_id = extract_id(SecureRandom.hex(16))
28
+ request_ids = {
29
+ request_id: request_id,
30
+ transaction_id: transaction_id,
31
+ space_id: space_id,
32
+ }
33
+
34
+ if %w[yes no].include?(checked_value.downcase)
35
+ checked_hash = Utils::BlockComponents.checked_todo(@id, checked_value.downcase)
36
+ last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(@parent_id)
37
+ last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)
38
+
39
+ operations = [
40
+ checked_hash,
41
+ last_edited_time_parent_hash,
42
+ last_edited_time_child_hash,
43
+ ]
44
+ request_body = build_payload(operations, request_ids)
45
+ response = HTTParty.post(
46
+ request_url,
47
+ body: request_body.to_json,
48
+ cookies: cookies,
49
+ headers: headers,
50
+ )
51
+ 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}.
52
+ Please try again, and if issues persist open an issue in GitHub."; end
53
+
54
+ true
55
+ else
56
+ false
58
57
  end
59
58
  end
60
- end
59
+ end
60
+ end
@@ -1,16 +1,16 @@
1
1
  module NotionAPI
2
2
 
3
- # Toggle block: best for storing children blocks
4
- class ToggleBlock < BlockTemplate
5
- @notion_type = 'toggle'
6
- @type = 'toggle'
7
-
8
- def type
9
- NotionAPI::ToggleBlock.notion_type
10
- end
11
-
12
- class << self
13
- attr_reader :notion_type, :type
14
- end
3
+ # Toggle block: best for storing children blocks
4
+ class ToggleBlock < BlockTemplate
5
+ @notion_type = "toggle"
6
+ @type = "toggle"
7
+
8
+ def type
9
+ NotionAPI::ToggleBlock.notion_type
10
+ end
11
+
12
+ class << self
13
+ attr_reader :notion_type, :type
15
14
  end
16
- end
15
+ end
16
+ end
@@ -11,6 +11,27 @@ module Utils
11
11
  class BlockComponents
12
12
  # ! Each function defined here builds one component that is included in each request sent to Notions backend.
13
13
  # ! Each request sent will contain multiple components.
14
+ # TODO figure this out
15
+ def self.build_payload(operations, request_ids)
16
+ # ! properly formats the payload for Notions backend.
17
+ # ! operations -> an array of hashes that define the operations to perform : ``Array[Hash]``
18
+ # ! request_ids -> the unique IDs for the request : ``str``
19
+ request_id = request_ids[:request_id]
20
+ transaction_id = request_ids[:transaction_id]
21
+ space_id = request_ids[:space_id]
22
+ payload = {
23
+ requestId: request_id,
24
+ transactions: [
25
+ {
26
+ id: transaction_id,
27
+ shardId: 955_090,
28
+ spaceId: space_id,
29
+ operations: operations,
30
+ },
31
+ ],
32
+ }
33
+ payload
34
+ end
14
35
  def self.create(block_id, block_type)
15
36
  # ! payload for creating a block.
16
37
  # ! block_id -> id of the new block : ``str``
@@ -220,18 +241,22 @@ module Utils
220
241
  end
221
242
 
222
243
  def self.row_location_add(last_row_id, block_id, view_id)
244
+ # ! add a new row to a Collection View table
245
+ # ! last_row_id -> ID of the last row in the CV : ``str``
246
+ # ! block_id -> ID of the blow : ``str``
247
+ # ! view_id -> ID of the View : ``str``
223
248
  {
224
249
  "table": "collection_view",
225
250
  "id": view_id,
226
251
  "path": [
227
- "page_sort"
252
+ "page_sort",
228
253
  ],
229
254
  "command": "listAfter",
230
255
  "args": {
231
- "after": last_row_id,
232
- "id": block_id
233
- }
234
- }
256
+ "after": last_row_id,
257
+ "id": block_id,
258
+ },
259
+ }
235
260
  end
236
261
 
237
262
  def self.block_location_remove(block_parent_id, block_id)
@@ -289,6 +314,9 @@ module Utils
289
314
  }
290
315
  end
291
316
  def self.add_emoji_icon(block_id, icon)
317
+ # ! add an emoji icon for either a page or callout block
318
+ # ! block_id -> the ID of the block : ``str``
319
+ # ! icon -> the icon for the block. This is currently randomly chosen. : ``str``
292
320
  {
293
321
  id: block_id,
294
322
  table: "block",
@@ -296,6 +324,44 @@ module Utils
296
324
  command: "set", "args": icon,
297
325
  }
298
326
  end
327
+
328
+ def self.source(new_block_id, url)
329
+ # ! set the source for the ImageBlock
330
+ # ! new_block_id -> the ID of the new ImageBlock: ``str``
331
+ # ! url -> the URL for the image
332
+ table = "block"
333
+ path = ["properties"]
334
+ command = "update"
335
+
336
+ {
337
+ id: new_block_id,
338
+ table: table,
339
+ path: path,
340
+ command: command,
341
+ args: {
342
+ source: [[
343
+ url,
344
+ ]],
345
+ },
346
+ }
347
+ end
348
+
349
+ def self.display_source(new_block_id, block_url)
350
+ # ! set the display source for the ImageBlock
351
+ # ! new_block_id -> the ID of the new ImageBlock: ``str``
352
+ # ! block_url -> the URL of the ImageBlock: ``str``
353
+ {
354
+ "id": new_block_id,
355
+ "table": "block",
356
+ "path": [
357
+ "format",
358
+ ],
359
+ "command": "update",
360
+ "args": {
361
+ "display_source": block_url,
362
+ },
363
+ }
364
+ end
299
365
  end
300
366
 
301
367
  class CollectionViewComponents
@@ -459,6 +525,7 @@ module Utils
459
525
  datetime_mappings = ["date"]
460
526
  media_mappings = ["file"]
461
527
  person_mappings = ["person"]
528
+ page_mappings = ["relation"]
462
529
 
463
530
  table = "block"
464
531
  path = [
@@ -475,10 +542,12 @@ module Utils
475
542
  args = [["‣", [["d", { "type": "date", "start_date": value }]]]]
476
543
  elsif person_mappings.include?(mapping)
477
544
  args = [["‣",
478
- [["u", value]]
479
- ]]
480
- else
481
- raise SchemaTypeError, "Invalid property type: #{mapping}"
545
+ [["u", value]]]]
546
+ elsif page_mappings.include?(mapping)
547
+ args = [["‣",
548
+ [["p", value]]]]
549
+ else
550
+ raise SchemaTypeError, "Invalid property type: #{mapping}"
482
551
  end
483
552
 
484
553
  {
@@ -499,10 +568,10 @@ module Utils
499
568
 
500
569
  args = {
501
570
  "value": {
502
- "id": SecureRandom.hex(16),
503
- "value": value,
504
- "color": random_color
505
- }
571
+ "id": SecureRandom.hex(16),
572
+ "value": value,
573
+ "color": random_color,
574
+ },
506
575
  }
507
576
 
508
577
  {
@@ -560,50 +629,7 @@ module Utils
560
629
  # ! payload for adding a column to the table.
561
630
  # ! collection_id -> the collection ID : ``str``
562
631
  # ! args -> the definition of the column : ``str``
563
- args["format"] = {
564
- "table_properties" => [
565
- {
566
- "property" => "title",
567
- "visible" => true,
568
- "width" => 280,
569
- },
570
- {
571
- "property" => "aliases",
572
- "visible" => true,
573
- "width" => 200,
574
- },
575
- {
576
- "property" => "category",
577
- "visible" => true,
578
- "width" => 200,
579
- },
580
- {
581
- "property" => "description",
582
- "visible" => true,
583
- "width" => 200,
584
- },
585
- {
586
- "property" => "ios_version",
587
- "visible" => true,
588
- "width" => 200,
589
- },
590
- {
591
- "property" => "tags",
592
- "visible" => true,
593
- "width" => 200,
594
- },
595
- {
596
- "property" => "phone",
597
- "visible" => true,
598
- "width" => 200,
599
- },
600
- {
601
- "property" => "unicode_version",
602
- "visible" => true,
603
- "width" => 200,
604
- },
605
- ],
606
- }
632
+
607
633
  {
608
634
  id: collection_id,
609
635
  table: "collection",
@@ -613,20 +639,28 @@ module Utils
613
639
  }
614
640
  end
615
641
 
616
- def self.update_property_value(page_id, column_name, new_value)
642
+ def self.update_property_value(page_id, column_name, new_value, column_type)
617
643
  # ! update the specified column_name to new_value
618
644
  # ! page_id -> the ID of the page: ``str``
619
645
  # ! column_name -> the name of the column ["property"] to update: ``str``
620
646
  # ! new_value -> the new value to assign to that column ["property"]: ``str``
647
+ # ! column_type -> the type of the property: ``str``
621
648
  table = "block"
622
649
  path = [
623
650
  "properties",
624
651
  column_name,
625
652
  ]
626
653
  command = "set"
627
- args = [[
628
- new_value,
629
- ]]
654
+
655
+ if column_type == "relation"
656
+ args = [["‣", [[
657
+ "p", new_value,
658
+ ]]]]
659
+ else
660
+ args = [[
661
+ new_value,
662
+ ]]
663
+ end
630
664
 
631
665
  {
632
666
  id: page_id,
@@ -639,7 +673,7 @@ module Utils
639
673
  end
640
674
 
641
675
  class SchemaTypeError < StandardError
642
- def initialize(msg="Custom exception that is raised when an invalid property type is passed as a mapping.", exception_type="schema_type")
676
+ def initialize(msg = "Custom exception that is raised when an invalid property type is passed as a mapping.", exception_type = "schema_type")
643
677
  @exception_type = exception_type
644
678
  super(msg)
645
679
  end
@@ -665,4 +699,4 @@ module Utils
665
699
  }
666
700
  payload
667
701
  end
668
- end
702
+ end
@@ -1,3 +1,3 @@
1
1
  module NotionAPI
2
- VERSION = '1.1.2'
2
+ VERSION = '1.1.3'
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.1.2
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Murphy
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-09 00:00:00.000000000 Z
11
+ date: 2021-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -139,7 +139,7 @@ licenses:
139
139
  - MIT
140
140
  metadata:
141
141
  allowed_push_host: https://rubygems.org
142
- post_install_message:
142
+ post_install_message:
143
143
  rdoc_options: []
144
144
  require_paths:
145
145
  - lib
@@ -154,8 +154,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
154
  - !ruby/object:Gem::Version
155
155
  version: '0'
156
156
  requirements: []
157
- rubygems_version: 3.1.2
158
- signing_key:
157
+ rubygems_version: 3.0.3
158
+ signing_key:
159
159
  specification_version: 4
160
160
  summary: A lightweight gem that allows you to easily read, write, and update Notion
161
161
  data with Ruby