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.



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

def initialize
  @object_hash = {}
  @client = Faraday.new "https://api.notion.com" do |conn|
    conn.request :json
    conn.response :json
    conn.headers["Notion-Version"] = NotionRubyMapping::NOTION_VERSION
  end
  @notion_token = nil
  @wait = 0.3333
  @debug = false
  @use_cache = true
end

Instance Attribute Details

#client=(value) ⇒ Object (writeonly)

for test only



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

def client=(value)
  @client = value
end

#debugObject

Returns the value of attribute debug.



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

def debug
  @debug
end

#notion_tokenObject

Returns the value of attribute notion_token.



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

def notion_token
  @notion_token
end

#object_hashObject (readonly)

Returns the value of attribute object_hash.



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

def object_hash
  @object_hash
end

#use_cacheObject

Returns the value of attribute use_cache.



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

def use_cache
  @use_cache
end

#waitObject

Returns the value of attribute wait.



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

def wait
  @wait
end

Instance Method Details

#append_block_children_block_path(block_id) ⇒ String (frozen)

Returns block_path.

Parameters:

  • block_id (String)

Returns:

  • (String (frozen))

    block_path



42
43
44
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 42

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

#append_block_children_page_path(page_id) ⇒ String (frozen)

Returns page_path.

Parameters:

  • page_id (String)

Returns:

  • (String (frozen))

    page_path



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

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

#append_block_children_request(id, payload) ⇒ Hash

Parameters:

  • id (String)
  • payload (Hash)

Returns:

  • (Hash)


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

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

#append_comment_request(json) ⇒ Object



59
60
61
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 59

def append_comment_request(json)
  request :post, comments_path, json
end

#block(id) ⇒ NotionRubyMapping::Base

Returns Block object or nil.

Parameters:

  • id (String)

    block_id (with or without “-”)

Returns:



65
66
67
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 65

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

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

Returns page_path.

Parameters:

  • page_id (String)

Returns:

  • (String (frozen))

    page_path



71
72
73
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 71

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

#block_children_request(id, query_string) ⇒ Hash

Parameters:

  • id (String)
  • query_string (String)

Returns:

  • (Hash)


78
79
80
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 78

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

#block_path(block_id) ⇒ String (frozen)

Returns page_path.

Parameters:

  • block_id (String)

Returns:

  • (String (frozen))

    page_path



84
85
86
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 84

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

#block_request(block_id) ⇒ Hash

Returns response.

Parameters:

  • block_id (String)

Returns:

  • (Hash)

    response



90
91
92
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 90

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

#clear_object_hashHash

Returns:

  • (Hash)


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

def clear_object_hash
  @object_hash = {}
end

#comments_pathString (frozen)

Returns:

  • (String (frozen))


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

def comments_path
  "v1/comments"
end

#complete_a_file_upload_path(file_id) ⇒ Object



104
105
106
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 104

def complete_a_file_upload_path(file_id)
  "v1/file_uploads/#{file_id}/complete"
end

#complete_a_file_upload_request(file_id) ⇒ Object



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

def complete_a_file_upload_request(file_id)
  request :post, complete_a_file_upload_path(file_id)
end

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

Returns self (NotionCache.instance).

Parameters:

  • notion_token (String)

Returns:



114
115
116
117
118
119
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 114

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

Returns response.

Parameters:

  • payload (Hash)

Returns:

  • (Hash)

    response



123
124
125
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 123

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

#create_file_upload_request(payload = {}) ⇒ Hash

Returns response.

Parameters:

  • payload (Hash) (defaults to: {})

Returns:

  • (Hash)

    response



129
130
131
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 129

def create_file_upload_request(payload = {})
  request :post, file_uploads_path, payload
end

#create_page_request(payload) ⇒ Hash

Returns response.

Parameters:

  • payload (Hash)

Returns:

  • (Hash)

    response



135
136
137
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 135

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

#database(id) ⇒ NotionRubyMapping::Base

Returns Database object or nil.

Parameters:

  • id (String)

    database_id (with or without “-”)

Returns:



141
142
143
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 141

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

#database_path(database_id) ⇒ String (frozen)

Returns page_path.

Parameters:

  • database_id (String)

Returns:

  • (String (frozen))

    page_path



147
148
149
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 147

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

#database_query_request(database_id, query) ⇒ Object

Parameters:



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

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

#database_request(database_id) ⇒ Hash

Returns response.

Parameters:

  • database_id (String)

Returns:

  • (Hash)

    response



166
167
168
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 166

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

#databases_pathString (frozen)

Returns page_path.

Returns:

  • (String (frozen))

    page_path



171
172
173
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 171

def databases_path
  "v1/databases"
end

#destroy_block(id) ⇒ NotionRubyMapping::Base

Parameters:

  • id (String)

Returns:



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

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

#destroy_block_request(id) ⇒ Hash

Parameters:

  • id (String)

Returns:

  • (Hash)


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

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

#file_upload_path(file_id) ⇒ Object



310
311
312
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 310

def file_upload_path(file_id)
  "v1/file_uploads/#{file_id}"
end

#file_upload_request(file_id) ⇒ Object



306
307
308
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 306

def file_upload_request(file_id)
  request :get, file_upload_path(file_id)
end

#file_uploads_pathString

Returns:

  • (String)


202
203
204
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 202

def file_uploads_path
  "v1/file_uploads"
end

#hex_id(id) ⇒ String

Returns id without “-”.

Parameters:

  • id (String)

    id string with “-”

Returns:

  • (String)

    id without “-”

See Also:



209
210
211
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 209

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

#inspectObject



213
214
215
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 213

def inspect
  "NotionCache"
end

#multipart_clientObject



32
33
34
35
36
37
38
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 32

def multipart_client
  @multipart_client ||= Faraday.new "https://api.notion.com" do |conn|
    conn.request :multipart, flat_encode: true
    conn.response :json
    conn.headers["Notion-Version"] = NotionRubyMapping::NOTION_VERSION
  end
end

#multipart_request(path, fname, options = {}) ⇒ Hash

Returns response hash.

Parameters:

  • path (String)
  • fname (String)
  • options (Hash) (defaults to: {})

Returns:

  • (Hash)

    response hash



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 221

def multipart_request(path, fname, options = {})
  raise "Please call `NotionRubyMapping.configure' before using other methods" unless @notion_token

  content_type = MIME::Types.type_for(fname).first.to_s

  sleep @wait
  body = options.map { |k, v| [k, Faraday::Multipart::ParamPart.new(v, "text/plain")] }.to_h
  file_part = Faraday::Multipart::FilePart.new(fname, content_type, File.basename(fname))
  response = multipart_client.send(:post) do |request|
    request.headers["Authorization"] = "Bearer #{@notion_token}"
    request.headers["content-Type"] = "multipart/form-data"
    request.path = path
    request.body = {file: file_part}.merge body
  end
  p response.body if @debug
  response.body
end

#object_for_key(id) ⇒ NotionRubyMapping::Base

Parameters:

  • id (String)

    id (with or without “-”)

Returns:



241
242
243
244
245
246
247
248
249
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 241

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

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

#page(id) ⇒ NotionRubyMapping::Base

Returns Page object or nil.

Parameters:

  • id (String)

    page_id (with or without “-”)

Returns:



253
254
255
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 253

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

#page_path(page_id) ⇒ String (frozen)

Returns page_path.

Parameters:

  • page_id (String)

Returns:

  • (String (frozen))

    page_path



259
260
261
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 259

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

#page_property_path(page_id, property_id) ⇒ String (frozen)

Returns page_property_path.

Parameters:

  • page_id (String)
  • property_id (String)

Returns:

  • (String (frozen))

    page_property_path



266
267
268
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 266

def page_property_path(page_id, property_id)
  [page_path(page_id), "properties/#{property_id}"].join "/"
end

#page_property_request(page_id, property_id, query = {}) ⇒ Hash

Returns response.

Parameters:

  • page_id (String)
  • property_id (String)

Returns:

  • (Hash)

    response



273
274
275
276
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 273

def page_property_request(page_id, property_id, query = {})
  p "page_id = #{page_id}, property_id = #{property_id}, query = #{query}" if @debug
  request :get, page_property_path(page_id, property_id), query
end

#page_request(page_id) ⇒ Hash

Returns response.

Parameters:

  • page_id (String)

Returns:

  • (Hash)

    response



280
281
282
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 280

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

#pages_pathString (frozen)

Returns page_path.

Returns:

  • (String (frozen))

    page_path



285
286
287
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 285

def pages_path
  "v1/pages"
end

#query_database_path(database_id) ⇒ String (frozen)

Returns page_path.

Parameters:

  • database_id (String)

Returns:

  • (String (frozen))

    page_path



291
292
293
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 291

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

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

Returns response hash.

Parameters:

  • method (Symbol)
  • path (String)
  • options (Hash) (defaults to: {})

Returns:

  • (Hash)

    response hash



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 318

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

  sleep @wait
  response = @client.send(method) do |request|
    request.headers["Authorization"] = "Bearer #{@notion_token}"
    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

#retrieve_comments_path(block_id) ⇒ Object

Parameters:

  • block_id (String)


296
297
298
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 296

def retrieve_comments_path(block_id)
  "v1/comments?block_id=#{block_id}"
end

#retrieve_comments_request(block_id, query) ⇒ Object

Parameters:



302
303
304
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 302

def retrieve_comments_request(block_id, query)
  request :get, retrieve_comments_path(block_id), (query&.query_json || {})
end

#search(query) ⇒ Object



340
341
342
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 340

def search(query)
  request(:post, search_path, query)
end

#search_pathObject



344
345
346
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 344

def search_path
  "v1/search"
end

#send_file_upload_path(id) ⇒ String

Parameters:

  • id (String)

Returns:

  • (String)


197
198
199
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 197

def send_file_upload_path(id)
  "v1/file_uploads/#{id}/send"
end

#send_file_upload_request(fname, id, options = {}) ⇒ Hash

Parameters:

  • fname (String)
  • id (String)
  • options (Hash) (defaults to: {})

Returns:

  • (Hash)


191
192
193
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 191

def send_file_upload_request(fname, id, options = {})
  multipart_request(send_file_upload_path(id), fname, options)
end

#token=(token) ⇒ Object

Parameters:

  • token (String)


349
350
351
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 349

def token=(token)
  @notion_token = token
end

#update_block_request(block_id, payload) ⇒ Object



353
354
355
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 353

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

#update_database_request(database_id, payload) ⇒ Hash

Returns response.

Parameters:

  • database_id (String)

Returns:

  • (Hash)

    response



366
367
368
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 366

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

#update_page_request(page_id, payload) ⇒ Hash

Returns response.

Parameters:

  • page_id (String)
  • payload (Hash)

Returns:

  • (Hash)

    response



373
374
375
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 373

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

#user(id) ⇒ NotionRubyMapping::UserObject

Returns UserObject object or nil.

Parameters:

  • id (String)

    user_id (with or without “-”)

Returns:



379
380
381
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 379

def user(id)
  UserObject.new json: user_request(id)
end

#user_path(user_id) ⇒ String (frozen)

Returns user_path.

Parameters:

  • user_id (String)

Returns:

  • (String (frozen))

    user_path



385
386
387
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 385

def user_path(user_id)
  "v1/users/#{user_id}"
end

#user_request(user_id) ⇒ Hash

Returns response.

Parameters:

  • user_id (String)

Returns:

  • (Hash)

    response



391
392
393
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 391

def user_request(user_id)
  request :get, user_path(user_id)
end

#usersArray<NotionRubyMapping::UserObject>

Returns UserObject array.

Returns:



396
397
398
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 396

def users
  List.new json: users_request, type: "user_object", value: true
end

#users_path(option = "") ⇒ String (frozen)

Returns user_path.

Returns:

  • (String (frozen))

    user_path



401
402
403
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 401

def users_path(option = "")
  "v1/users#{option}"
end

#users_request(query = Query.new) ⇒ Hash

Returns response.

Parameters:

Returns:

  • (Hash)

    response



408
409
410
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 408

def users_request(query = Query.new)
  request :get, users_path, query.query_json
end