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
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 16

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



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

def client=(value)
  @client = value
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

Instance Method Details

#append_block_children_block_path(block_id) ⇒ String (frozen)



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

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

#append_block_children_page_path(page_id) ⇒ String (frozen)



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

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

#append_block_children_request(id, payload) ⇒ Hash



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

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

#block(id) ⇒ NotionRubyMapping::Base



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

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

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



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

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

#block_children_request(id, query_string) ⇒ Hash



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

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

#block_path(block_id) ⇒ String (frozen)



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

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

#block_request(block_id) ⇒ Hash



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

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

#clear_object_hashHash



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

def clear_object_hash
  @object_hash = {}
end

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



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

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



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

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

#create_page_request(payload) ⇒ Hash



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

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

#database(id) ⇒ NotionRubyMapping::Base



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

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

#database_path(database_id) ⇒ String (frozen)



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

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

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



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

def database_query(id, query)
  Base.create_from_json database_query_request(id, query.query_json)
end

#database_query_request(database_id, payload) ⇒ Object



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

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

#database_request(database_id) ⇒ Hash



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

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

#hex_id(id) ⇒ String



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

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

#inspectObject



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

def inspect
  "NotionCache"
end

#object_for_key(id) ⇒ NotionRubyMapping::Base



153
154
155
156
157
158
159
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 153

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



163
164
165
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 163

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

#page_path(page_id) ⇒ String (frozen)



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

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

#page_request(page_id) ⇒ Hash



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

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

#pages_pathString (frozen)



180
181
182
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 180

def pages_path
  "v1/pages"
end

#query_database_path(database_id) ⇒ String (frozen)



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

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

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



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 194

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_database(id, payload) ⇒ Object



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

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

#update_database_request(database_id, payload) ⇒ Hash



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

def update_database_request(database_id, payload)
  request :patch, "v1/databases/#{database_id}", payload
end

#update_page_request(page_id, payload) ⇒ Hash



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

def update_page_request(page_id, payload)
  request :patch, "v1/pages/#{page_id}", payload
end