Class: NotionRubyMapping::NotionCache

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

Overview

singleton class of caching Notion objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNotionCache

Returns a new instance of NotionCache.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 15

def initialize
  @object_hash = {}
  @client = Faraday::Connection.new "https://api.notion.com" do |builder|
    builder.use FaradayMiddleware::EncodeJson
    builder.use FaradayMiddleware::ParseJson
    builder.adapter Faraday.default_adapter
  end
  @notion_token = nil
  @wait = 0
  @debug = false
end

Instance Attribute Details

#client=(value) ⇒ Object (writeonly)

for test only



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

def client=(value)
  @client = value
end

#object_hashObject (readonly)

Returns the value of attribute object_hash.



26
27
28
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 26

def object_hash
  @object_hash
end

Instance Method Details

#append_block_children_block_path(block_id) ⇒ String (frozen)



31
32
33
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 31

def append_block_children_block_path(block_id)
  "v1/blocks/#{block_id}/children"
end

#append_block_children_page_path(page_id) ⇒ String (frozen)



37
38
39
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 37

def append_block_children_page_path(page_id)
  "v1/blocks/#{page_id}/children"
end

#append_block_children_request(id, payload) ⇒ Hash



44
45
46
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 44

def append_block_children_request(id, payload)
  request :patch, append_block_children_block_path(id), payload
end

#block(id) ⇒ NotionRubyMapping::Base



50
51
52
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 50

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

#block_children_page_path(page_id, query_string = "") ⇒ String (frozen)



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

def block_children_page_path(page_id, query_string = "")
  "v1/blocks/#{page_id}/children#{query_string}"
end

#block_children_request(id, query_string) ⇒ Hash



63
64
65
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 63

def block_children_request(id, query_string)
  request :get, block_children_page_path(id, query_string)
end

#block_path(block_id) ⇒ String (frozen)



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

def block_path(block_id)
  "v1/blocks/#{block_id}"
end

#block_request(block_id) ⇒ Hash



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

def block_request(block_id)
  request :get, block_path(block_id)
end

#clear_object_hashHash



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

def clear_object_hash
  @object_hash = {}
end

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



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

def create_client(notion_token, wait: 0.3333, debug: false)
  @notion_token = notion_token
  @wait = wait
  @debug = debug
  self
end

#create_database_request(payload) ⇒ Hash



95
96
97
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 95

def create_database_request(payload)
  request :post, databases_path, payload
end

#create_page_request(payload) ⇒ Hash



101
102
103
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 101

def create_page_request(payload)
  request :post, "v1/pages", payload
end

#database(id) ⇒ NotionRubyMapping::Base



107
108
109
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 107

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

#database_path(database_id) ⇒ String (frozen)



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

def database_path(database_id)
  "v1/databases/#{database_id}"
end

#database_query_request(database_id, query) ⇒ Object



126
127
128
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 126

def database_query_request(database_id, query)
  request :post, "v1/databases/#{database_id}/query", query.query_json
end

#database_request(database_id) ⇒ Hash



132
133
134
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 132

def database_request(database_id)
  request :get, database_path(database_id)
end

#databases_pathString (frozen)



137
138
139
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 137

def databases_path
  "v1/databases"
end

#destroy_block(id) ⇒ NotionRubyMapping::Base



143
144
145
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 143

def destroy_block(id)
  Base.create_from_json destroy_block_request(id)
end

#destroy_block_request(id) ⇒ Hash



149
150
151
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 149

def destroy_block_request(id)
  request :delete, block_path(id)
end

#hex_id(id) ⇒ String

Returns id without “-”.



156
157
158
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 156

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

#inspectObject



160
161
162
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 160

def inspect
  "NotionCache"
end

#object_for_key(id) ⇒ NotionRubyMapping::Base



166
167
168
169
170
171
172
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 166

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

  json = yield(@client)
  @object_hash[key] = Base.create_from_json json
end

#page(id) ⇒ NotionRubyMapping::Base



176
177
178
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 176

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

#page_path(page_id) ⇒ String (frozen)



182
183
184
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 182

def page_path(page_id)
  "v1/pages/#{page_id}"
end

#page_request(page_id) ⇒ Hash



188
189
190
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 188

def page_request(page_id)
  request :get, page_path(page_id)
end

#pages_pathString (frozen)



193
194
195
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 193

def pages_path
  "v1/pages"
end

#query_database_path(database_id) ⇒ String (frozen)



199
200
201
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 199

def query_database_path(database_id)
  "v1/databases/#{database_id}/query"
end

#request(method, path, options = {}) ⇒ Hash



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 207

def request(method, path, options = {})
  raise "Please call `NotionCache.create_client' before using other methods" unless @notion_token

  sleep @wait
  response = @client.send(method) do |request|
    request.headers["Authorization"] = "Bearer #{@notion_token}"
    request.headers["Notion-Version"] = NotionRubyMapping::NOTION_VERSION
    case method
    when :get, :delete
      request.url path, options
    when :post, :put, :patch
      request.headers["Content-Type"] = "application/json"
      request.path = path
      request.body = options.to_json unless options.empty?
    else
      raise StandardError, "Unknown method: #{method}"
    end
    request.options.merge!(options.delete(:request)) if options.key? :request
  end
  p response.body if @debug
  response.body
end

#update_block_request(block_id, payload) ⇒ Object



230
231
232
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 230

def update_block_request(block_id, payload)
  request :patch, block_path(block_id), payload
end

#update_database_request(database_id, payload) ⇒ Hash



243
244
245
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 243

def update_database_request(database_id, payload)
  request :patch, database_path(database_id), payload
end

#update_page_request(page_id, payload) ⇒ Hash



250
251
252
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 250

def update_page_request(page_id, payload)
  request :patch, page_path(page_id), payload
end