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
26
# 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.3333
  @debug = false
  @use_cache = true
end

Instance Attribute Details

#client=(value) ⇒ Object (writeonly)

for test only



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

def client=(value)
  @client = value
end

#debugObject

Returns the value of attribute debug.



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

def debug
  @debug
end

#notion_tokenObject

Returns the value of attribute notion_token.



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

def notion_token
  @notion_token
end

#object_hashObject (readonly)

Returns the value of attribute object_hash.



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

def object_hash
  @object_hash
end

#use_cacheObject

Returns the value of attribute use_cache.



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

def use_cache
  @use_cache
end

#waitObject

Returns the value of attribute wait.



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

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



33
34
35
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 33

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



39
40
41
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 39

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)


46
47
48
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 46

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

#append_comment_request(json) ⇒ Object



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

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:



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

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



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

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)


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

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



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

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

#block_request(block_id) ⇒ Hash

Returns response.

Parameters:

  • block_id (String)

Returns:

  • (Hash)

    response



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

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

#clear_object_hashHash

Returns:

  • (Hash)


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

def clear_object_hash
  @object_hash = {}
end

#comments_pathString (frozen)

Returns:

  • (String (frozen))


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

def comments_path
  "v1/comments"
end

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

Returns self (NotionCache.instance).

Parameters:

  • notion_token (String)

Returns:



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

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



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

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

#create_page_request(payload) ⇒ Hash

Returns response.

Parameters:

  • payload (Hash)

Returns:

  • (Hash)

    response



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

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:



118
119
120
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 118

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



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

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

#database_query_request(database_id, query) ⇒ Object

Parameters:



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

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



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

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

#databases_pathString (frozen)

Returns page_path.

Returns:

  • (String (frozen))

    page_path



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

def databases_path
  "v1/databases"
end

#destroy_block(id) ⇒ NotionRubyMapping::Base

Parameters:

  • id (String)

Returns:



154
155
156
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 154

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

#destroy_block_request(id) ⇒ Hash

Parameters:

  • id (String)

Returns:

  • (Hash)


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

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

#hex_id(id) ⇒ String

Returns id without “-”.

Parameters:

  • id (String)

    id string with “-”

Returns:

  • (String)

    id without “-”

See Also:



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

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

#inspectObject



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

def inspect
  "NotionCache"
end

#object_for_key(id) ⇒ NotionRubyMapping::Base

Parameters:

  • id (String)

    id (with or without “-”)

Returns:



177
178
179
180
181
182
183
184
185
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 177

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:



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

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



195
196
197
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 195

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



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

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



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

def page_property_request(page_id, property_id, query = {})
  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



215
216
217
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 215

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

#pages_pathString (frozen)

Returns page_path.

Returns:

  • (String (frozen))

    page_path



220
221
222
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 220

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



226
227
228
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 226

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



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 245

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}"
    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

#retrieve_comments_path(block_id) ⇒ Object

Parameters:

  • block_id (String)


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

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

#retrieve_comments_request(block_id, query) ⇒ Object

Parameters:



237
238
239
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 237

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

#search(query) ⇒ Object



268
269
270
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 268

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

#search_pathObject



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

def search_path
  "v1/search"
end

#token=(token) ⇒ Object

Parameters:

  • token (String)


277
278
279
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 277

def token=(token)
  @notion_token = token
end

#update_block_request(block_id, payload) ⇒ Object



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

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



294
295
296
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 294

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



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

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:



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

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



313
314
315
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 313

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

#user_request(user_id) ⇒ Hash

Returns response.

Parameters:

  • user_id (String)

Returns:

  • (Hash)

    response



319
320
321
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 319

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

#usersArray<NotionRubyMapping::UserObject>

Returns UserObject array.

Returns:



324
325
326
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 324

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



329
330
331
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 329

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

#users_request(query = Query.new) ⇒ Hash

Returns response.

Parameters:

Returns:

  • (Hash)

    response



336
337
338
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 336

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