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

Defined Under Namespace

Classes: InvalidClientInstantiationError

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



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/notion_api/core.rb', line 45

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/notion_api/core.rb', line 59

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(request_body)

  # if no content, returns empty list
  jsonified_record_response["block"][clean_id]["value"]["content"] || []
end

#extract_id(url_or_id) ⇒ Object



75
76
77
78
79
80
81
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 75

def extract_id(url_or_id)
  # ! parse and clean the URL or ID object provided.
  # ! url_or_id -> the block ID or URL : ``str``
  http_or_https = url_or_id.match(/^(http|https)/) # true if http or https in url_or_id...
  collection_view_match = url_or_id.match(/(\?v=)/)

  if (url_or_id.length == 36) && ((url_or_id.split("-").length == 5) && !http_or_https)
    # passes if url_or_id is perfectly formatted already...
    url_or_id
  elsif (http_or_https && (url_or_id.split("-").last.length == 32)) || (!http_or_https && (url_or_id.length == 32)) || (collection_view_match)
    # passes if either:
    # 1. a URL is passed as url_or_id and the ID at the end is 32 characters long or
    # 2. a URL is not passed and the ID length is 32 [aka unformatted]
    pattern = [8, 13, 18, 23]
    if collection_view_match
      id_without_view = url_or_id.split("?")[0]
      clean_id = id_without_view.split("/").last
      pattern.each { |index| clean_id.insert(index, "-") }
      clean_id
    else
      id = url_or_id.split("-").last
      pattern.each { |index| id.insert(index, "-") }
      id
    end
  else
    raise ArgumentError, "Expected a Notion page URL or a page ID. Please consult the documentation for further information."
  end
end

#get_page(url_or_id) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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(request_body)

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

  raise ArgumentError, "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)

  get_instantiated_instance_for(block_type, clean_id, block_parent_id, jsonified_record_response)
end