Class: NotionAPI::Core

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/notion_api/core.rb

Overview

the initial methods available to an instantiated Cloent object are defined

Constant Summary

Constants included from Utils

Utils::URLS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#build_payload

Constructor Details

#initialize(token_v2, active_user_header) ⇒ Core

Returns a new instance of Core.



19
20
21
22
# File 'lib/notion_api/core.rb', line 19

def initialize(token_v2, active_user_header)
  @@token_v2 = token_v2
  @@active_user_header = active_user_header
end

Class Attribute Details

.active_user_headerObject (readonly)

Returns the value of attribute active_user_header.



14
15
16
# File 'lib/notion_api/core.rb', line 14

def active_user_header
  @active_user_header
end

.optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/notion_api/core.rb', line 14

def options
  @options
end

.token_v2Object (readonly)

Returns the value of attribute token_v2.



14
15
16
# File 'lib/notion_api/core.rb', line 14

def token_v2
  @token_v2
end

.type_whitelistObject (readonly)

Returns the value of attribute type_whitelist.



14
15
16
# File 'lib/notion_api/core.rb', line 14

def type_whitelist
  @type_whitelist
end

Instance Attribute Details

#clean_idObject (readonly)

Returns the value of attribute clean_id.



17
18
19
# File 'lib/notion_api/core.rb', line 17

def clean_id
  @clean_id
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



17
18
19
# File 'lib/notion_api/core.rb', line 17

def cookies
  @cookies
end

#headersObject (readonly)

Returns the value of attribute headers.



17
18
19
# File 'lib/notion_api/core.rb', line 17

def headers
  @headers
end

Instance Method Details

#children(url_or_id = @id) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/notion_api/core.rb', line 68

def children(url_or_id = @id)
  # ! retrieve the children of a block. If the block has no children, return []. If it does, return the instantiated class objects associated with each child.
  # ! url_or_id -> the block ID or URL : ``str``

  children_ids = children_ids(url_or_id)
  if children_ids.empty?
    []
  else
    children_class_instances = []
    children_ids.each { |child| children_class_instances.push(get(child)) }
    children_class_instances
  end
end

#children_ids(url_or_id = @id) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/notion_api/core.rb', line 82

def children_ids(url_or_id = @id)
  # ! retrieve the children IDs of a block.
  # ! url_or_id -> the block ID or URL : ``str``
  clean_id = extract_id(url_or_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?
    return {} if i >= 10

    jsonified_record_response = get_all_block_info(clean_id, request_body)
    i += 1
  end

  jsonified_record_response['block'][clean_id]['value']['content'] || []
end

#get_page(url_or_id) ⇒ Object



24
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
# File 'lib/notion_api/core.rb', line 24

def get_page(url_or_id)
  # ! retrieve a Notion Page Block and return its instantiated class object.
  # ! url_or_id -> the block ID or URL : ``str``
  clean_id = extract_id(url_or_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

  block_id = clean_id
  block_type = extract_type(clean_id, jsonified_record_response)
  block_parent_id = extract_parent_id(clean_id, jsonified_record_response)

  raise 'the URL or ID passed to the get_page method must be that of a Page Block.' if !['collection_view_page', 'page'].include?(block_type)

  if block_type == "page"
    block_title = extract_title(clean_id, jsonified_record_response)
    PageBlock.new(block_id, block_title, block_parent_id)
  elsif block_type == "collection_view_page"
    collection_id = extract_collection_id(block_id, jsonified_record_response)
    block_title = extract_collection_title(clean_id, collection_id, jsonified_record_response)
    view_id = extract_view_ids(block_id, jsonified_record_response)[0]
    schema = extract_collection_schema(collection_id, view_id, jsonified_record_response)
    column_mappings = schema.keys
    column_names = column_mappings.map { |mapping| schema[mapping]['name']}

    collection_view_page = CollectionViewPage.new(block_id, block_title, block_parent_id, collection_id, view_id)
    collection_view_page.instance_variable_set(:@column_names, column_names)
    CollectionView.class_eval{attr_reader :column_names}
    collection_view_page
  end
end