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.
-
#object_hash ⇒ Object
readonly
Returns the value of attribute object_hash.
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
-
#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
-
#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_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.
- #update_block_request(block_id, payload) ⇒ Object
-
#update_database_request(database_id, payload) ⇒ Hash
Response.
-
#update_page_request(page_id, payload) ⇒ 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 @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_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 |
Instance Method Details
#append_block_children_block_path(block_id) ⇒ String (frozen)
Returns 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.
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
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 |
#block(id) ⇒ NotionRubyMapping::Base
Returns Block object or nil.
50 51 52 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 50 def block(id) object_for_key(id) { block_request id } end |
#block_children_page_path(page_id, query_string = "") ⇒ String (frozen)
Returns page_path.
56 57 58 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 56 def block_children_page_path(page_id, query_string = "") "v1/blocks/#{page_id}/children#{query_string}" end |
#block_children_request(id, query_string) ⇒ Hash
63 64 65 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 63 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.
69 70 71 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 69 def block_path(block_id) "v1/blocks/#{block_id}" end |
#block_request(block_id) ⇒ Hash
Returns response.
75 76 77 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 75 def block_request(block_id) request :get, block_path(block_id) end |
#clear_object_hash ⇒ Hash
80 81 82 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 80 def clear_object_hash @object_hash = {} end |
#create_client(notion_token, wait: 0.3333, debug: false) ⇒ NotionRubyMapping::NotionCache
Returns self (NotionCache.instance).
86 87 88 89 90 91 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 86 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.
95 96 97 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 95 def create_database_request(payload) request :post, databases_path, payload end |
#create_page_request(payload) ⇒ Hash
Returns response.
101 102 103 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 101 def create_page_request(payload) request :post, "v1/pages", payload end |
#database(id) ⇒ NotionRubyMapping::Base
Returns Database object or nil.
107 108 109 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 107 def database(id) object_for_key(id) { database_request id } end |
#database_path(database_id) ⇒ String (frozen)
Returns page_path.
113 114 115 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 113 def database_path(database_id) "v1/databases/#{database_id}" end |
#database_query_request(database_id, query) ⇒ Object
126 127 128 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 126 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.
132 133 134 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 132 def database_request(database_id) request :get, database_path(database_id) end |
#databases_path ⇒ String (frozen)
Returns page_path.
137 138 139 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 137 def databases_path "v1/databases" end |
#destroy_block(id) ⇒ NotionRubyMapping::Base
143 144 145 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 143 def destroy_block(id) Base.create_from_json destroy_block_request(id) end |
#destroy_block_request(id) ⇒ Hash
149 150 151 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 149 def destroy_block_request(id) request :delete, block_path(id) end |
#hex_id(id) ⇒ String
Returns id without “-”.
156 157 158 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 156 def hex_id(id) id&.gsub "-", "" end |
#inspect ⇒ Object
160 161 162 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 160 def inspect "NotionCache" end |
#object_for_key(id) ⇒ NotionRubyMapping::Base
166 167 168 169 170 171 172 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 166 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.
176 177 178 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 176 def page(id) object_for_key(id) { page_request id } end |
#page_path(page_id) ⇒ String (frozen)
Returns page_path.
182 183 184 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 182 def page_path(page_id) "v1/pages/#{page_id}" end |
#page_request(page_id) ⇒ Hash
Returns response.
188 189 190 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 188 def page_request(page_id) request :get, page_path(page_id) end |
#pages_path ⇒ String (frozen)
Returns page_path.
193 194 195 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 193 def pages_path "v1/pages" end |
#query_database_path(database_id) ⇒ String (frozen)
Returns page_path.
199 200 201 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 199 def query_database_path(database_id) "v1/databases/#{database_id}/query" end |
#request(method, path, options = {}) ⇒ Hash
Returns response hash.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 207 def request(method, path, = {}) 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, 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 |
#update_block_request(block_id, payload) ⇒ Object
230 231 232 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 230 def update_block_request(block_id, payload) request :patch, block_path(block_id), payload end |
#update_database_request(database_id, payload) ⇒ Hash
Returns response.
243 244 245 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 243 def update_database_request(database_id, payload) request :patch, database_path(database_id), payload end |
#update_page_request(page_id, payload) ⇒ Hash
Returns response.
250 251 252 |
# File 'lib/notion_ruby_mapping/controllers/notion_cache.rb', line 250 def update_page_request(page_id, payload) request :patch, page_path(page_id), payload end |