Class: NotionAPI::CollectionView

Inherits:
Core
  • Object
show all
Defined in:
lib/notion_api/notion_types/collection_view_blocks.rb

Overview

collection views such as tables and timelines.

Direct Known Subclasses

CollectionViewPage

Constant Summary

Constants included from Utils

Utils::URLS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Core

#clean_id, #cookies, #headers

Instance Method Summary collapse

Methods inherited from Core

#children, #children_ids, #get_page

Methods included from Utils

#build_payload

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_typeObject (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

.typeObject (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_idObject (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

#idObject (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_idObject (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

#titleObject (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_idObject (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



79
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
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 79

def add_property(name, type)
  # ! add a property (column) to the table.
  # ! name -> name of the property : ``str``
  # ! type -> type of the property : ``str``
  cookies = Core.options['cookies']
  headers = Core.options['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: 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
# 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``
  
  cookies = Core.options['cookies']
  headers = Core.options['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|
    child_component = Utils::CollectionViewComponents.insert_data(new_block_id, j.zero? ? 'title' : col_map[col_name], data[col_name], j.zero? ? schema['title']["type"] : schema[col_map[col_name]]['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: 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

#row(row_id) ⇒ Object



126
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
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 126

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)
  schema = extract_collection_schema(@collection_id, @view_id)
  keys = schema.keys
  column_names = keys.map { |key| schema[key]['name'] }
  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
  row_jsonified_response = jsonified_record_response['block'][clean_id]['value']['properties']
  row_data = {}
  keys.each_with_index { |key, idx| row_data[column_names[idx]] = row_jsonified_response[key] ? row_jsonified_response[key].flatten : [] }
  row_data
end

#row_idsObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 154

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

#rowsObject



177
178
179
180
181
182
183
184
185
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 177

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
  
  row_id_array.map { |row_id| NotionAPI::CollectionViewRow.new(row_id, parent_id, collection_id, view_id) }
end

#typeObject



9
10
11
# File 'lib/notion_api/notion_types/collection_view_blocks.rb', line 9

def type
  NotionAPI::CollectionView.notion_type
end