Class: NotionAPI::CollectionView
- Defined in:
- lib/notion_api/notion_types/collection_view_blocks.rb
Overview
collection views such as tables and timelines.
Direct Known Subclasses
Constant Summary
Constants included from Utils
Class Attribute Summary collapse
-
.notion_type ⇒ Object
readonly
Returns the value of attribute notion_type.
-
.type ⇒ Object
readonly
Returns the value of attribute type.
Instance Attribute Summary collapse
-
#collection_id ⇒ Object
readonly
Returns the value of attribute collection_id.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#parent_id ⇒ Object
readonly
Returns the value of attribute parent_id.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#view_id ⇒ Object
readonly
Returns the value of attribute view_id.
Attributes inherited from Core
Instance Method Summary collapse
- #add_property(name, type) ⇒ Object
- #add_row(data) ⇒ Object
- #create_singleton_methods_and_instance_variables(row, row_data) ⇒ Object
-
#initialize(id, title, parent_id, collection_id, view_id) ⇒ CollectionView
constructor
A new instance of CollectionView.
- #row(row_id) ⇒ Object
- #row_ids ⇒ Object
- #rows ⇒ Object
- #type ⇒ Object
Methods inherited from Core
#children, #children_ids, #get_page
Methods included from Utils
Constructor Details
#initialize(id, title, parent_id, collection_id, view_id) ⇒ CollectionView
Returns a new instance of CollectionView.
17 18 19 20 21 22 23 |
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 17 def initialize(id, title, parent_id, collection_id, view_id) @id = id @title = title @parent_id = parent_id @collection_id = collection_id @view_id = view_id end |
Class Attribute Details
.notion_type ⇒ Object (readonly)
Returns the value of attribute notion_type.
14 15 16 |
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 14 def notion_type @notion_type end |
.type ⇒ Object (readonly)
Returns the value of attribute type.
14 15 16 |
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 14 def type @type end |
Instance Attribute Details
#collection_id ⇒ Object (readonly)
Returns the value of attribute collection_id.
4 5 6 |
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 4 def collection_id @collection_id end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
4 5 6 |
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 4 def id @id end |
#parent_id ⇒ Object (readonly)
Returns the value of attribute parent_id.
4 5 6 |
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 4 def parent_id @parent_id end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
4 5 6 |
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 4 def title @title end |
#view_id ⇒ Object (readonly)
Returns the value of attribute view_id.
4 5 6 |
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 4 def view_id @view_id end |
Instance Method Details
#add_property(name, type) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 80 def add_property(name, type) # ! add a property (column) to the table. # ! name -> name of the property : ``str`` # ! type -> type of the property : ``str`` = Core.["cookies"] headers = Core.["headers"] request_id = extract_id(SecureRandom.hex(16)) transaction_id = extract_id(SecureRandom.hex(16)) space_id = extract_id(SecureRandom.hex(16)) request_ids = { request_id: request_id, transaction_id: transaction_id, space_id: space_id, } # create updated schema schema = extract_collection_schema(@collection_id, @view_id) schema[name] = { name: name, type: type, } new_schema = { schema: schema, } add_collection_property = Utils::CollectionViewComponents.add_collection_property(@collection_id, new_schema) operations = [ add_collection_property, ] request_url = URLS[:UPDATE_BLOCK] request_body = build_payload(operations, request_ids) response = HTTParty.post( request_url, body: request_body.to_json, cookies: , headers: headers, ) 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}. Please try again, and if issues persist open an issue in GitHub."; end true end |
#add_row(data) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 25 def add_row(data) # ! add new row to Collection View table. # ! data -> data to add to table : ``hash`` = Core.["cookies"] headers = Core.["headers"] request_id = extract_id(SecureRandom.hex(16)) transaction_id = extract_id(SecureRandom.hex(16)) space_id = extract_id(SecureRandom.hex(16)) new_block_id = extract_id(SecureRandom.hex(16)) schema = extract_collection_schema(@collection_id, @view_id) keys = schema.keys col_map = {} keys.map { |key| col_map[schema[key]["name"]] = key } request_ids = { request_id: request_id, transaction_id: transaction_id, space_id: space_id, } instantiate_row = Utils::CollectionViewComponents.add_new_row(new_block_id) set_block_alive = Utils::CollectionViewComponents.set_collection_blocks_alive(new_block_id, @collection_id) new_block_edited_time = Utils::BlockComponents.last_edited_time(new_block_id) parent_edited_time = Utils::BlockComponents.last_edited_time(@parent_id) operations = [ instantiate_row, set_block_alive, new_block_edited_time, parent_edited_time, ] data.keys.each_with_index do |col_name, j| unless col_map.keys.include?(col_name.to_s); raise ArgumentError, "Column '#{col_name.to_s}' does not exist." end 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"]) operations.push(child_component) end request_url = URLS[:UPDATE_BLOCK] request_body = build_payload(operations, request_ids) response = HTTParty.post( request_url, body: request_body.to_json, cookies: , headers: headers, ) 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}. Please try again, and if issues persist open an issue in GitHub."; end NotionAPI::CollectionViewRow.new(new_block_id, @parent_id, @collection_id, @view_id) end |
#create_singleton_methods_and_instance_variables(row, row_data) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 208 def create_singleton_methods_and_instance_variables(row, row_data) # ! creates singleton methods for each property in a CollectionView. # ! row -> the block ID of the 'row' to retrieve: ``str`` # ! row_data -> the data corresponding to that row, should be key-value pairs where the keys are the columns: ``hash`` collection_data = extract_collection_data(@collection_id, @view_id) schema = collection_data["collection"][collection_id]["value"]["schema"] column_mappings = schema.keys column_hash = {} column_names = column_mappings.map { |mapping| column_hash[mapping] = schema[mapping]["name"].downcase } column_hash.keys.each_with_index do |column, i| # loop over the column names... # set instance variables for each column, allowing the dev to 'read' the column value cleaned_column = column_hash[column].split(" ").join("_").downcase.to_sym # p row_data["value"]["properties"][column_mappings[i]], !(row_data["value"]["properties"][column] or row_data["value"]["properties"][column_mappings[i]]) if row_data["value"]["properties"].nil? or row_data["value"]["properties"][column].nil? value = "" else value = row_data["value"]["properties"][column][0][0] end row.instance_variable_set("@#{cleaned_column}", value) CollectionViewRow.class_eval { attr_reader cleaned_column } # then, define singleton methods for each column that are used to update the table cell row.define_singleton_method("#{cleaned_column}=") do |new_value| # neat way to get the name of the currently invoked method... parsed_method = __method__.to_s[0...-1].split("_").join(" ") = Core.["cookies"] headers = Core.["headers"] request_id = extract_id(SecureRandom.hex(16)) transaction_id = extract_id(SecureRandom.hex(16)) space_id = extract_id(SecureRandom.hex(16)) request_ids = { request_id: request_id, transaction_id: transaction_id, space_id: space_id, } update_property_value = Utils::CollectionViewComponents.update_property_value(@id, column_hash.key(parsed_method), new_value) operations = [ update_property_value, ] request_url = URLS[:UPDATE_BLOCK] request_body = build_payload(operations, request_ids) response = HTTParty.post( request_url, body: request_body.to_json, cookies: , headers: headers, ) 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}. Please try again, and if issues persist open an issue in GitHub.";end # set the instance variable to the updated value! _ = row.instance_variable_set("@#{__method__.to_s[0...-1]}", new_value) row end end end |
#row(row_id) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 127 def row(row_id) # ! retrieve a row from a CollectionView Table. # ! row_id -> the ID for the row to retrieve: ``str`` clean_id = extract_id(row_id) request_body = { pageId: clean_id, chunkNumber: 0, limit: 100, verticalColumns: false, } jsonified_record_response = get_all_block_info(clean_id, request_body) i = 0 while jsonified_record_response.empty? || jsonified_record_response["block"].empty? return {} if i >= 10 jsonified_record_response = get_all_block_info(clean_id, request_body) i += 1 end collection_data = extract_collection_data(@collection_id, @view_id) schema = collection_data["collection"][collection_id]["value"]["schema"] column_mappings = schema.keys column_names = column_mappings.map { |mapping| schema[mapping]["name"] } collection_row = CollectionViewRow.new(row_id, @parent_id, @collection_id, @view_id) collection_row.instance_variable_set(:@column_names, column_names) CollectionViewRow.class_eval { attr_reader :column_names } row_data = collection_data["block"][collection_row.id] create_singleton_methods_and_instance_variables(collection_row, row_data) collection_row end |
#row_ids ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 163 def row_ids # ! retrieve all Collection View table rows. clean_id = extract_id(@id) request_body = { pageId: clean_id, chunkNumber: 0, limit: 100, verticalColumns: false, } jsonified_record_response = get_all_block_info(clean_id, request_body) i = 0 while jsonified_record_response.empty? || jsonified_record_response["block"].empty? return {} if i >= 10 jsonified_record_response = get_all_block_info(clean_id, request_body) i += 1 end jsonified_record_response["collection_view"][@view_id]["value"]["page_sort"] end |
#rows ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 186 def rows # ! returns all rows as instantiated class instances. row_id_array = row_ids parent_id = @parent_id collection_id = @collection_id view_id = @view_id collection_data = extract_collection_data(@collection_id, @view_id) schema = collection_data["collection"][collection_id]["value"]["schema"] column_mappings = schema.keys column_names = column_mappings.map { |mapping| schema[mapping]["name"] } row_instances = row_id_array.map { |row_id| NotionAPI::CollectionViewRow.new(row_id, parent_id, collection_id, view_id) } clean_row_instances = row_instances.filter { |row| collection_data["block"][row.id] } clean_row_instances.each { |row| row.instance_variable_set(:@column_names, column_names) } CollectionViewRow.class_eval { attr_reader :column_names } clean_row_instances.each do |collection_row| row_data = collection_data["block"][collection_row.id] create_singleton_methods_and_instance_variables(collection_row, row_data) end clean_row_instances end |
#type ⇒ Object
9 10 11 |
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 9 def type NotionAPI::CollectionView.notion_type end |