Class: NotionAPI::CollectionView

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

Overview

collection views such as tables and timelines.

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.



769
770
771
772
773
774
775
# File 'lib/notion_api/blocks.rb', line 769

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.



766
767
768
# File 'lib/notion_api/blocks.rb', line 766

def notion_type
  @notion_type
end

.typeObject (readonly)

Returns the value of attribute type.



766
767
768
# File 'lib/notion_api/blocks.rb', line 766

def type
  @type
end

Instance Attribute Details

#collection_idObject (readonly)

Returns the value of attribute collection_id.



756
757
758
# File 'lib/notion_api/blocks.rb', line 756

def collection_id
  @collection_id
end

#idObject (readonly)

Returns the value of attribute id.



756
757
758
# File 'lib/notion_api/blocks.rb', line 756

def id
  @id
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



756
757
758
# File 'lib/notion_api/blocks.rb', line 756

def parent_id
  @parent_id
end

#titleObject (readonly)

Returns the value of attribute title.



756
757
758
# File 'lib/notion_api/blocks.rb', line 756

def title
  @title
end

#view_idObject (readonly)

Returns the value of attribute view_id.



756
757
758
# File 'lib/notion_api/blocks.rb', line 756

def view_id
  @view_id
end

Instance Method Details

#add_property(name, type) ⇒ Object



831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
# File 'lib/notion_api/blocks.rb', line 831

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



777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# File 'lib/notion_api/blocks.rb', line 777

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



878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
# File 'lib/notion_api/blocks.rb', line 878

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



906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
# File 'lib/notion_api/blocks.rb', line 906

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



929
930
931
932
933
934
935
936
937
# File 'lib/notion_api/blocks.rb', line 929

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



761
762
763
# File 'lib/notion_api/blocks.rb', line 761

def type
  NotionAPI::CollectionView.notion_type
end