Class: NotionRb::Utils::BlockCache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/notion_rb/utils/block_cache.rb

Instance Method Summary collapse

Constructor Details

#initializeBlockCache

Returns a new instance of BlockCache.



8
9
10
# File 'lib/notion_rb/utils/block_cache.rb', line 8

def initialize
  @blocks = []
end

Instance Method Details

#add_collection_view_blocks(block) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/notion_rb/utils/block_cache.rb', line 34

def add_collection_view_blocks(block)
  return unless block[:block_type] == 'collection_view_page'

   = block[:metadata]
  query_collection = NotionRb::Api::QueryCollection.new(collection_id: [:collection_id], view_id: [:view_ids][0])
  block[:children] = [[:collection_id]]
  add_blocks(query_collection.blocks)
end

#children(notion_id) ⇒ Object



24
25
26
27
28
# File 'lib/notion_rb/utils/block_cache.rb', line 24

def children(notion_id)
  return nil unless find(notion_id)

  find(notion_id)[:children]
end

#clearObject



30
31
32
# File 'lib/notion_rb/utils/block_cache.rb', line 30

def clear
  @blocks = []
end

#contains?(notion_id) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/notion_rb/utils/block_cache.rb', line 12

def contains?(notion_id)
  @blocks.any? { |block| block[:notion_id] == notion_id }
end

#find(notion_id) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/notion_rb/utils/block_cache.rb', line 16

def find(notion_id)
  add_block(notion_id) unless contains?(notion_id)

  @blocks.find { |block| block[:notion_id] == notion_id }
rescue Mechanize::ResponseCodeError
  nil
end