Skip to content

Commit f74602c

Browse files
Merge pull request #25 from danmurphy1217/clean-column-names
Better Parsing for Property Names
2 parents 673de5f + d187a48 commit f74602c

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

lib/notion_api/notion_types/collection_view_blocks.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def add_row(data)
9393
properties[col_map[col.to_s]] = [[data[col]]]
9494
end
9595

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"}}
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" } }
9797
row_data = collection_data["block"][collection_row.id]
9898
create_singleton_methods_and_instance_variables(collection_row, row_data)
9999

@@ -242,7 +242,7 @@ def create_singleton_methods_and_instance_variables(row, row_data)
242242
column_hash.keys.each_with_index do |column, i|
243243
# loop over the column names...
244244
# set instance variables for each column, allowing the dev to 'read' the column value
245-
cleaned_column = column_hash[column].split(" ").join("_").downcase.to_sym
245+
cleaned_column = clean_property_names(column_hash, column)
246246

247247
# p row_data["value"]["properties"][column_mappings[i]], !(row_data["value"]["properties"][column] or row_data["value"]["properties"][column_mappings[i]])
248248
if row_data["value"]["properties"].nil? or row_data["value"]["properties"][column].nil?
@@ -262,9 +262,7 @@ def create_singleton_methods_and_instance_variables(row, row_data)
262262
parsed_method = __method__.to_s[0...-1].split("_").join(" ")
263263
cookies = Core.options["cookies"]
264264
headers = Core.options["headers"]
265-
266-
p new_value, column_hash.key(parsed_method), column_names, schema
267-
265+
268266
request_id = extract_id(SecureRandom.hex(16))
269267
transaction_id = extract_id(SecureRandom.hex(16))
270268
space_id = extract_id(SecureRandom.hex(16))
@@ -282,7 +280,6 @@ def create_singleton_methods_and_instance_variables(row, row_data)
282280
]
283281

284282
if %q[select multi_select].include?(schema[column_hash.key(parsed_method)]["type"])
285-
p "ENTERED THE ABYSS"
286283
options = schema[column_hash.key(parsed_method)]["options"].nil? ? [] : schema[column_hash.key(parsed_method)]["options"].map { |option| option["value"] }
287284
multi_select_multi_options = new_value.split(",")
288285
multi_select_multi_options.each do |option|
@@ -310,6 +307,15 @@ def create_singleton_methods_and_instance_variables(row, row_data)
310307
end
311308
end
312309
end
310+
311+
def clean_property_names(prop_hash, prop_notion_name)
312+
# ! standardize property names by splitting the words in the property name into an array, removing non-alphanumeric
313+
# ! characters, downcasing, and then re-joining the array with underscores.
314+
# ! prop_hash -> hash of property notion names and property textual names: ``str``
315+
# ! prop_notion_name -> the four-character long name of the notion property: ``str``
316+
317+
prop_hash[prop_notion_name].split(" ").map { |word| word.gsub(/[^a-z0-9]/i, "").downcase }.join("_").to_sym
318+
end
313319
end
314320

315321
# class that represents each row in a CollectionView

lib/notion_api/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module NotionAPI
2-
VERSION = '1.1.1'
2+
VERSION = '1.1.2'
33
end

spec/blocks_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,16 @@
146146
end
147147
end
148148
end
149+
context "cleaning the property names of a Notion Collection View." do
150+
it "FULL_PAGE: should parse the column name, replace non-alphanumeric characters, and then return the cleaned column name" do
151+
test_prop_hash = {"1234"=> "Col Name (one) (two) 1234?!>"}
152+
expect($Full_page_cv.clean_property_names(test_prop_hash, "1234")).to eq(:col_name_one_two_1234)
153+
end
154+
it "INLINE: should parse the column name, replace non-alphanumeric characters, and thenr eturn the cleaned column name" do
155+
inline_cv = $Block_spec_page.get_collection("3224c94f-e660-4092-9dba-d26b69b68d40")
156+
test_prop_hash = {"1234"=> "Col Name (#$^&^^*one) (@!#$!two) 1!@#2$%@3!@#@?4!>"}
157+
expect(inline_cv.clean_property_names(test_prop_hash, "1234")).to eq(:col_name_one_two_1234)
158+
end
159+
end
149160
end
150161

spec/spec_variables.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,7 @@ module Helpers
5454
$Block_spec_get_collection_id = "3224c94f-e660-4092-9dba-d26b69b68d40"
5555
$Block_spec_add_row_id = "eaa144ab-d91b-4640-b13a-a0ba1c8dd450"
5656
$Vehicle_data_csv = File.read("./spec/fixtures/vauto_inventory.csv")
57+
$Full_page_cv_url = "https://www.notion.so/danmurphy/3a8c0397841748b08da76f188b7fb381?v=a68811a8c18b433b9787b17dcd83527e"
58+
$Full_page_cv = $Client.get_page($Full_page_cv_url)
5759
$Json = JSON.parse(File.read("./spec/fixtures/emoji_data.json"))
5860
end

0 commit comments

Comments
 (0)