Skip to content

Commit d18b3c9

Browse files
Merge pull request #27 from danmurphy1217/add-advanced-property-support
Add Advanced Property Type Support: Relations
2 parents b89b905 + 96a6f4a commit d18b3c9

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
language: ruby
2-
cache: bundler
32
dist: bionic
43
rvm:
54
# using .filter, so must be >= 2.6... will update to be .select later...

lib/notion_api/notion_types/collection_view_blocks.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def create_singleton_methods_and_instance_variables(row, row_data)
273273
space_id: space_id,
274274
}
275275

276-
update_property_value_hash = Utils::CollectionViewComponents.update_property_value(@id, column_hash.key(parsed_method), new_value)
276+
update_property_value_hash = Utils::CollectionViewComponents.update_property_value(@id, column_hash.key(parsed_method), new_value, schema[column_hash.key(parsed_method)]["type"])
277277

278278
operations = [
279279
update_property_value_hash,

lib/notion_api/utils.rb

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,14 @@ def self.row_location_add(last_row_id, block_id, view_id)
224224
"table": "collection_view",
225225
"id": view_id,
226226
"path": [
227-
"page_sort"
227+
"page_sort",
228228
],
229229
"command": "listAfter",
230230
"args": {
231-
"after": last_row_id,
232-
"id": block_id
233-
}
234-
}
231+
"after": last_row_id,
232+
"id": block_id,
233+
},
234+
}
235235
end
236236

237237
def self.block_location_remove(block_parent_id, block_id)
@@ -459,6 +459,7 @@ def self.insert_data(block_id, column, value, mapping)
459459
datetime_mappings = ["date"]
460460
media_mappings = ["file"]
461461
person_mappings = ["person"]
462+
page_mappings = ["relation"]
462463

463464
table = "block"
464465
path = [
@@ -475,10 +476,12 @@ def self.insert_data(block_id, column, value, mapping)
475476
args = [["‣", [["d", { "type": "date", "start_date": value }]]]]
476477
elsif person_mappings.include?(mapping)
477478
args = [["‣",
478-
[["u", value]]
479-
]]
480-
else
481-
raise SchemaTypeError, "Invalid property type: #{mapping}"
479+
[["u", value]]]]
480+
elsif page_mappings.include?(mapping)
481+
args = [["‣",
482+
[["p", value]]]]
483+
else
484+
raise SchemaTypeError, "Invalid property type: #{mapping}"
482485
end
483486

484487
{
@@ -499,10 +502,10 @@ def self.add_new_option(column, value, collection_id)
499502

500503
args = {
501504
"value": {
502-
"id": SecureRandom.hex(16),
503-
"value": value,
504-
"color": random_color
505-
}
505+
"id": SecureRandom.hex(16),
506+
"value": value,
507+
"color": random_color,
508+
},
506509
}
507510

508511
{
@@ -593,10 +596,10 @@ def self.add_collection_property(collection_id, args)
593596
"width" => 200,
594597
},
595598
{
596-
"property" => "phone",
597-
"visible" => true,
598-
"width" => 200,
599-
},
599+
"property" => "phone",
600+
"visible" => true,
601+
"width" => 200,
602+
},
600603
{
601604
"property" => "unicode_version",
602605
"visible" => true,
@@ -613,20 +616,28 @@ def self.add_collection_property(collection_id, args)
613616
}
614617
end
615618

616-
def self.update_property_value(page_id, column_name, new_value)
619+
def self.update_property_value(page_id, column_name, new_value, column_type)
617620
# ! update the specified column_name to new_value
618621
# ! page_id -> the ID of the page: ``str``
619622
# ! column_name -> the name of the column ["property"] to update: ``str``
620623
# ! new_value -> the new value to assign to that column ["property"]: ``str``
624+
# ! column_type -> the type of the property: ``str``
621625
table = "block"
622626
path = [
623627
"properties",
624628
column_name,
625629
]
626630
command = "set"
627-
args = [[
628-
new_value,
629-
]]
631+
632+
if column_type == "relation"
633+
args = [["‣", [[
634+
"p", new_value,
635+
]]]]
636+
else
637+
args = [[
638+
new_value,
639+
]]
640+
end
630641

631642
{
632643
id: page_id,
@@ -639,7 +650,7 @@ def self.update_property_value(page_id, column_name, new_value)
639650
end
640651

641652
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")
653+
def initialize(msg = "Custom exception that is raised when an invalid property type is passed as a mapping.", exception_type = "schema_type")
643654
@exception_type = exception_type
644655
super(msg)
645656
end
@@ -665,4 +676,4 @@ def build_payload(operations, request_ids)
665676
}
666677
payload
667678
end
668-
end
679+
end

notion.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
3232
spec.add_runtime_dependency('httparty', '~> 0.17')
3333
spec.add_runtime_dependency('json', '~> 2.2')
3434

35-
spec.add_development_dependency("bundler", "~> 2.1.0")
35+
spec.add_development_dependency("bundler")
3636
spec.add_development_dependency("rake", "~> 13.0.0")
3737
spec.add_development_dependency("rspec", "~> 3.9.0")
3838
spec.add_development_dependency("rubocop", "~> 1.4.0")

0 commit comments

Comments
 (0)