notion 1.0.7 → 1.1.4

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.
@@ -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
+
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
  {
@@ -494,12 +563,15 @@ module Utils
494
563
  table = "collection"
495
564
  path = ["schema", column, "options"]
496
565
  command = "keyedObjectListAfter"
566
+ colors = ["default", "gray", "brown", "orange", "yellow", "green", "blue", "purple", "pink", "red"]
567
+ random_color = colors[rand(0...colors.length)]
568
+
497
569
  args = {
498
570
  "value": {
499
- "id": SecureRandom.hex(16),
500
- "value": value,
501
- "color": "green"
502
- }
571
+ "id": SecureRandom.hex(16),
572
+ "value": value,
573
+ "color": random_color,
574
+ },
503
575
  }
504
576
 
505
577
  {
@@ -557,50 +629,7 @@ module Utils
557
629
  # ! payload for adding a column to the table.
558
630
  # ! collection_id -> the collection ID : ``str``
559
631
  # ! args -> the definition of the column : ``str``
560
- args["format"] = {
561
- "table_properties" => [
562
- {
563
- "property" => "title",
564
- "visible" => true,
565
- "width" => 280,
566
- },
567
- {
568
- "property" => "aliases",
569
- "visible" => true,
570
- "width" => 200,
571
- },
572
- {
573
- "property" => "category",
574
- "visible" => true,
575
- "width" => 200,
576
- },
577
- {
578
- "property" => "description",
579
- "visible" => true,
580
- "width" => 200,
581
- },
582
- {
583
- "property" => "ios_version",
584
- "visible" => true,
585
- "width" => 200,
586
- },
587
- {
588
- "property" => "tags",
589
- "visible" => true,
590
- "width" => 200,
591
- },
592
- {
593
- "property" => "phone",
594
- "visible" => true,
595
- "width" => 200,
596
- },
597
- {
598
- "property" => "unicode_version",
599
- "visible" => true,
600
- "width" => 200,
601
- },
602
- ],
603
- }
632
+
604
633
  {
605
634
  id: collection_id,
606
635
  table: "collection",
@@ -610,20 +639,28 @@ module Utils
610
639
  }
611
640
  end
612
641
 
613
- 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)
614
643
  # ! update the specified column_name to new_value
615
644
  # ! page_id -> the ID of the page: ``str``
616
645
  # ! column_name -> the name of the column ["property"] to update: ``str``
617
646
  # ! new_value -> the new value to assign to that column ["property"]: ``str``
647
+ # ! column_type -> the type of the property: ``str``
618
648
  table = "block"
619
649
  path = [
620
650
  "properties",
621
651
  column_name,
622
652
  ]
623
653
  command = "set"
624
- args = [[
625
- new_value,
626
- ]]
654
+
655
+ if column_type == "relation"
656
+ args = [["‣", [[
657
+ "p", new_value,
658
+ ]]]]
659
+ else
660
+ args = [[
661
+ new_value,
662
+ ]]
663
+ end
627
664
 
628
665
  {
629
666
  id: page_id,
@@ -636,7 +673,7 @@ module Utils
636
673
  end
637
674
 
638
675
  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")
676
+ def initialize(msg = "Custom exception that is raised when an invalid property type is passed as a mapping.", exception_type = "schema_type")
640
677
  @exception_type = exception_type
641
678
  super(msg)
642
679
  end
@@ -662,4 +699,4 @@ module Utils
662
699
  }
663
700
  payload
664
701
  end
665
- end
702
+ end
@@ -1,3 +1,3 @@
1
1
  module NotionAPI
2
- VERSION = '1.0.7'
2
+ VERSION = '1.1.4'
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.7
4
+ version: 1.1.4
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-06 00:00:00.000000000 Z
11
+ date: 2021-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 2.1.0
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 2.1.0
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +122,7 @@ files:
122
122
  - lib/notion_api/notion_types/header_block.rb
123
123
  - lib/notion_api/notion_types/image_block.rb
124
124
  - lib/notion_api/notion_types/latex_block.rb
125
+ - lib/notion_api/notion_types/link_block.rb
125
126
  - lib/notion_api/notion_types/numbered_block.rb
126
127
  - lib/notion_api/notion_types/page_block.rb
127
128
  - lib/notion_api/notion_types/quote_block.rb
@@ -139,7 +140,7 @@ licenses:
139
140
  - MIT
140
141
  metadata:
141
142
  allowed_push_host: https://rubygems.org
142
- post_install_message:
143
+ post_install_message:
143
144
  rdoc_options: []
144
145
  require_paths:
145
146
  - lib
@@ -154,8 +155,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
155
  - !ruby/object:Gem::Version
155
156
  version: '0'
156
157
  requirements: []
157
- rubygems_version: 3.1.2
158
- signing_key:
158
+ rubygems_version: 3.0.3
159
+ signing_key:
159
160
  specification_version: 4
160
161
  summary: A lightweight gem that allows you to easily read, write, and update Notion
161
162
  data with Ruby