Class: NotionRubyMapping::NotionCache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/notion_ruby_mapping/notion_cache.rb

Overview

singleton class of caching Notion objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNotionCache



11
12
13
14
15
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 11

def initialize
  @object_hash = {}
  @client = nil
  @wait = 0
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



16
17
18
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 16

def client
  @client
end

#object_hashObject (readonly)

Returns the value of attribute object_hash.



16
17
18
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 16

def object_hash
  @object_hash
end

Instance Method Details

#block(id) ⇒ NotionRubyMapping::Block?



80
81
82
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 80

def block(id)
  object_for_key(id) { block_json id }
end

#block_children(id) ⇒ NotionRubyMapping::List



86
87
88
89
90
91
92
93
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 86

def block_children(id)
  array = []
  sleep @wait
  @client.block_children(block_id: id, sleep_interval: @wait, max_retries: 20) do |page|
    array.concat page.results
  end
  Base.create_from_json({"object" => "list", "results" => array})
end

#block_json(id) ⇒ Hash



74
75
76
77
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 74

def block_json(id)
  sleep @wait
  @client.block block_id: id
end

#create_client(notion_token, wait = 0.3333) ⇒ NotionRubyMapping::NotionCache



20
21
22
23
24
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 20

def create_client(notion_token, wait = 0.3333)
  @client ||= Notion::Client.new token: notion_token
  @wait = wait
  self
end

#database(id) ⇒ NotionRubyMapping::Database?



68
69
70
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 68

def database(id)
  object_for_key(id) { database_json id }
end

#database_json(id) ⇒ Hash



61
62
63
64
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 61

def database_json(id)
  sleep @wait
  @client.database database_id: id
end

#database_query(id, query) ⇒ NotionRubyMapping::List



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 98

def database_query(id, query)
  array = []
  parameters = {database_id: id, sleep_interval: @wait, max_retries: 20}
  parameters[:filter] = query.filter unless query.filter.empty?
  parameters[:sorts] = query.sort unless query.sort.empty?

  @client.database_query(parameters) do |page|
    array.concat page.results
  end
  Base.create_from_json({"object" => "list", "results" => array})
end

#hex_id(id) ⇒ String



28
29
30
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 28

def hex_id(id)
  id&.gsub "-", ""
end

#object_for_key(id) ⇒ NotionRubyMapping::Block, ...



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 34

def object_for_key(id)
  key = hex_id(id)
  return @object_hash[key] if @object_hash.key? key

  begin
    json = yield(@client)
    @object_hash[key] = Base.create_from_json json
  rescue StandardError
    nil
  end
end

#page(id) ⇒ NotionRubyMapping::Page?



55
56
57
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 55

def page(id)
  object_for_key(id) { page_json id }
end

#page_json(id) ⇒ Hash



48
49
50
51
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 48

def page_json(id)
  sleep @wait
  @client.page page_id: id
end

#update_database(id, payload) ⇒ Object



119
120
121
122
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 119

def update_database(id, payload)
  sleep @wait
  @client.update_database payload.merge({database_id: id})
end

#update_page(id, payload) ⇒ Object



112
113
114
115
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 112

def update_page(id, payload)
  sleep @wait
  @client.update_page payload.merge({page_id: id})
end