Method: NotionAPI::CollectionView#add_property

Defined in:
lib/notion_api/notion_types/collection_view_blocks.rb

#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``
  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