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)

Returns block_path.

Parameters:

  • block_id (String)

Returns:

  • (String (frozen))

    block_path



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)

Returns page_path.

Parameters:

  • page_id (String)

Returns:

  • (String (frozen))

    page_path



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

Parameters:

  • id (String)
  • payload (Hash)

Returns:

  • (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

#append_comment_request(json) ⇒ Object



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

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:



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

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



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

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)


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

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



73
74
75
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 73

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

#block_request(block_id) ⇒ Hash

Returns response.

Parameters:

  • block_id (String)

Returns:

  • (Hash)

    response



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

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

#clear_object_hashHash

Returns:

  • (Hash)


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

def clear_object_hash
  @object_hash = {}
end

#comments_pathString (frozen)

Returns:

  • (String (frozen))


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

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:



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

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



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

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

#create_page_request(payload) ⇒ Hash

Returns response.

Parameters:

  • payload (Hash)

Returns:

  • (Hash)

    response



110
111
112
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 110

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:



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

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



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

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

#database_query_request(database_id, query) ⇒ Object

Parameters:



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

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

#database_request(database_id) ⇒ Hash

Returns response.

Parameters:

  • database_id (String)

Returns:

  • (Hash)

    response



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

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

#databases_pathString (frozen)

Returns page_path.

Returns:

  • (String (frozen))

    page_path



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

def databases_path
  "v1/databases"
end

#destroy_block(id) ⇒ NotionRubyMapping::Base

Parameters:

  • id (String)

Returns:



152
153
154
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 152

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

#destroy_block_request(id) ⇒ Hash

Parameters:

  • id (String)

Returns:

  • (Hash)


158
159
160
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 158

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:



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

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

#inspectObject



169
170
171
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 169

def inspect
  "NotionCache"
end

#object_for_key(id) ⇒ NotionRubyMapping::Base

Parameters:

  • id (String)

    id (with or without “-”)

Returns:



175
176
177
178
179
180
181
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 175

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

Returns Page object or nil.

Parameters:

  • id (String)

    page_id (with or without “-”)

Returns:



185
186
187
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 185

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



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

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



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

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



205
206
207
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 205

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



211
212
213
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 211

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

#pages_pathString (frozen)

Returns page_path.

Returns:

  • (String (frozen))

    page_path



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

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



222
223
224
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 222

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



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 241

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

#retrieve_comments_path(block_id) ⇒ Object

Parameters:

  • block_id (String)


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

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

#retrieve_comments_request(block_id, query) ⇒ Object

Parameters:



233
234
235
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 233

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

#update_block_request(block_id, payload) ⇒ Object



264
265
266
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 264

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



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

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



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

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