Class: NotionRubyMapping::NotionCache
- Inherits:
-
Object
- Object
- NotionRubyMapping::NotionCache
- Includes:
- Singleton
- Defined in:
- lib/notion_ruby_mapping/controllers/notion_cache.rb
Overview
singleton class of caching Notion objects
Instance Attribute Summary collapse
-
#client ⇒ Object
writeonly
for test only.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#notion_token ⇒ Object
Returns the value of attribute notion_token.
-
#object_hash ⇒ Object
readonly
Returns the value of attribute object_hash.
-
#wait ⇒ Object
Returns the value of attribute wait.
Instance Method Summary collapse
-
#append_block_children_block_path(block_id) ⇒ String (frozen)
Block_path.
-
#append_block_children_page_path(page_id) ⇒ String (frozen)
Page_path.
- #append_block_children_request(id, payload) ⇒ Hash
- #append_comment_request(json) ⇒ Object
-
#block(id) ⇒ NotionRubyMapping::Base
Block object or nil.
-
#block_children_page_path(page_id, query_string = "") ⇒ String (frozen)
Page_path.
- #block_children_request(id, query_string) ⇒ Hash
-
#block_path(block_id) ⇒ String (frozen)
Page_path.
-
#block_request(block_id) ⇒ Hash
Response.
- #clear_object_hash ⇒ Hash
- #comments_path ⇒ String (frozen)
-
#create_client(notion_token, wait: 0.3333, debug: false) ⇒ NotionRubyMapping::NotionCache
Self (NotionCache.instance).
-
#create_database_request(payload) ⇒ Hash
Response.
-
#create_page_request(payload) ⇒ Hash
Response.
-
#database(id) ⇒ NotionRubyMapping::Base
Database object or nil.
-
#database_path(database_id) ⇒ String (frozen)
Page_path.
- #database_query_request(database_id, query) ⇒ Object
-
#database_request(database_id) ⇒ Hash
Response.
-
#databases_path ⇒ String (frozen)
Page_path.
- #destroy_block(id) ⇒ NotionRubyMapping::Base
- #destroy_block_request(id) ⇒ Hash
-
#hex_id(id) ⇒ String
Id without “-”.
-
#initialize ⇒ NotionCache
constructor
A new instance of NotionCache.
- #inspect ⇒ Object
- #object_for_key(id) ⇒ NotionRubyMapping::Base
-
#page(id) ⇒ NotionRubyMapping::Base
Page object or nil.
-
#page_path(page_id) ⇒ String (frozen)
Page_path.
-
#page_property_path(page_id, property_id) ⇒ String (frozen)
Page_property_path.
-
#page_property_request(page_id, property_id, query = {}) ⇒ Hash
Response.
-
#page_request(page_id) ⇒ Hash
Response.
-
#pages_path ⇒ String (frozen)
Page_path.
-
#query_database_path(database_id) ⇒ String (frozen)
Page_path.
-
#request(method, path, options = {}) ⇒ Hash
Response hash.
- #retrieve_comments_path(block_id) ⇒ Object
- #retrieve_comments_request(block_id, query) ⇒ Object
- #search(query) ⇒ Object
- #search_path ⇒ Object
- #update_block_request(block_id, payload) ⇒ Object
-
#update_database_request(database_id, payload) ⇒ Hash
Response.
-
#update_page_request(page_id, payload) ⇒ Hash
Response.
-
#user(id) ⇒ NotionRubyMapping::UserObject
UserObject object or nil.
-
#user_path(user_id) ⇒ String (frozen)
User_path.
-
#user_request(user_id) ⇒ Hash
Response.
-
#users ⇒ Array<NotionRubyMapping::UserObject>
UserObject array.
-
#users_path(option = "") ⇒ String (frozen)
User_path.
-
#users_request(query = Query.new) ⇒ Hash
Response.
Constructor Details
#initialize ⇒ NotionCache
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.3333 @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 |
#debug ⇒ Object
Returns the value of attribute debug.
28 29 30 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 28 def debug @debug end |
#notion_token ⇒ Object
Returns the value of attribute notion_token.
28 29 30 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 28 def notion_token @notion_token end |
#object_hash ⇒ Object (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 |
#wait ⇒ Object
Returns the value of attribute wait.
28 29 30 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 28 def wait @wait 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 |
#append_comment_request(json) ⇒ Object
49 50 51 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 49 def append_comment_request(json) request :post, comments_path, json end |
#block(id) ⇒ NotionRubyMapping::Base
55 56 57 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 55 def block(id) object_for_key(id) { block_request id } end |
#block_children_page_path(page_id, query_string = "") ⇒ String (frozen)
61 62 63 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 61 def block_children_page_path(page_id, query_string = "") "v1/blocks/#{page_id}/children#{query_string}" end |
#block_children_request(id, query_string) ⇒ Hash
68 69 70 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 68 def block_children_request(id, query_string) request :get, block_children_page_path(id, query_string) end |
#block_path(block_id) ⇒ String (frozen)
74 75 76 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 74 def block_path(block_id) "v1/blocks/#{block_id}" end |
#block_request(block_id) ⇒ Hash
80 81 82 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 80 def block_request(block_id) request :get, block_path(block_id) end |
#clear_object_hash ⇒ Hash
85 86 87 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 85 def clear_object_hash @object_hash = {} end |
#comments_path ⇒ String (frozen)
90 91 92 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 90 def comments_path "v1/comments" end |
#create_client(notion_token, wait: 0.3333, debug: false) ⇒ NotionRubyMapping::NotionCache
96 97 98 99 100 101 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 96 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
105 106 107 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 105 def create_database_request(payload) request :post, databases_path, payload end |
#create_page_request(payload) ⇒ Hash
111 112 113 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 111 def create_page_request(payload) request :post, "v1/pages", payload end |
#database(id) ⇒ NotionRubyMapping::Base
117 118 119 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 117 def database(id) object_for_key(id) { database_request id } end |
#database_path(database_id) ⇒ String (frozen)
123 124 125 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 123 def database_path(database_id) "v1/databases/#{database_id}" end |
#database_query_request(database_id, query) ⇒ Object
136 137 138 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 136 def database_query_request(database_id, query) request :post, "v1/databases/#{database_id}/query", query.query_json end |
#database_request(database_id) ⇒ Hash
142 143 144 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 142 def database_request(database_id) request :get, database_path(database_id) end |
#databases_path ⇒ String (frozen)
147 148 149 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 147 def databases_path "v1/databases" end |
#destroy_block(id) ⇒ NotionRubyMapping::Base
153 154 155 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 153 def destroy_block(id) Base.create_from_json destroy_block_request(id) end |
#destroy_block_request(id) ⇒ Hash
159 160 161 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 159 def destroy_block_request(id) request :delete, block_path(id) end |
#hex_id(id) ⇒ String
Returns id without “-”.
166 167 168 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 166 def hex_id(id) id&.gsub "-", "" end |
#inspect ⇒ Object
170 171 172 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 170 def inspect "NotionCache" end |
#object_for_key(id) ⇒ NotionRubyMapping::Base
176 177 178 179 180 181 182 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 176 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
186 187 188 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 186 def page(id) object_for_key(id) { page_request id } end |
#page_path(page_id) ⇒ String (frozen)
192 193 194 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 192 def page_path(page_id) "v1/pages/#{page_id}" end |
#page_property_path(page_id, property_id) ⇒ String (frozen)
199 200 201 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 199 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
206 207 208 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 206 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
212 213 214 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 212 def page_request(page_id) request :get, page_path(page_id) end |
#pages_path ⇒ String (frozen)
217 218 219 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 217 def pages_path "v1/pages" end |
#query_database_path(database_id) ⇒ String (frozen)
223 224 225 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 223 def query_database_path(database_id) "v1/databases/#{database_id}/query" end |
#request(method, path, options = {}) ⇒ Hash
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 242 def request(method, path, = {}) 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, when :post, :put, :patch request.headers["Content-Type"] = "application/json" request.path = path request.body = .to_json unless .empty? else raise StandardError, "Unknown method: #{method}" end request..merge!(.delete(:request)) if .key? :request end p response.body if @debug response.body end |
#retrieve_comments_path(block_id) ⇒ Object
228 229 230 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 228 def retrieve_comments_path(block_id) "v1/comments?block_id=#{block_id}" end |
#retrieve_comments_request(block_id, query) ⇒ Object
234 235 236 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 234 def retrieve_comments_request(block_id, query) request :get, retrieve_comments_path(block_id), (query&.query_json || {}) end |
#search(query) ⇒ Object
265 266 267 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 265 def search(query) request(:post, search_path, = query) end |
#search_path ⇒ Object
269 270 271 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 269 def search_path "v1/search" end |
#update_block_request(block_id, payload) ⇒ Object
273 274 275 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 273 def update_block_request(block_id, payload) request :patch, block_path(block_id), payload end |
#update_database_request(database_id, payload) ⇒ Hash
286 287 288 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 286 def update_database_request(database_id, payload) request :patch, database_path(database_id), payload end |
#update_page_request(page_id, payload) ⇒ Hash
293 294 295 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 293 def update_page_request(page_id, payload) request :patch, page_path(page_id), payload end |
#user(id) ⇒ NotionRubyMapping::UserObject
299 300 301 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 299 def user(id) UserObject.new json: user_request(id) end |
#user_path(user_id) ⇒ String (frozen)
305 306 307 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 305 def user_path(user_id) "v1/users/#{user_id}" end |
#user_request(user_id) ⇒ Hash
311 312 313 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 311 def user_request(user_id) request :get, user_path(user_id) end |
#users ⇒ Array<NotionRubyMapping::UserObject>
316 317 318 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 316 def users List.new json: users_request, type: :user_object, value: true end |
#users_path(option = "") ⇒ String (frozen)
321 322 323 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 321 def users_path(option = "") "v1/users#{option}" end |