Class: NotionRubyMapping::NotionCache
- Inherits:
-
Object
- Object
- NotionRubyMapping::NotionCache
- Includes:
- Singleton
- Defined in:
- lib/notion_ruby_mapping/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
-
#block(id) ⇒ NotionRubyMapping::Base
Block object or nil.
-
#block_children(id) ⇒ NotionRubyMapping::Base
List object.
-
#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) ⇒ Object
-
#create_page_request(payload) ⇒ Hash
Response.
-
#database(id) ⇒ NotionRubyMapping::Base
Database object or nil.
-
#database_path(database_id) ⇒ String (frozen)
Page_path.
-
#database_query(id, query) ⇒ NotionRubyMapping::Base
List object.
- #database_query_request(database_id, payload) ⇒ Object
-
#database_request(database_id) ⇒ Hash
Response.
-
#databases_path ⇒ String (frozen)
Page_path.
-
#hex_id(id) ⇒ String
Id without “-”.
-
#initialize ⇒ NotionCache
constructor
initialize.
- #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_database(id, payload) ⇒ Object
-
#update_database_request(database_id, payload) ⇒ Hash
Response.
-
#update_page_request(page_id, payload) ⇒ Hash
Response.
Constructor Details
#initialize ⇒ NotionCache
initialize
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 14 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
26 27 28 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 26 def client=(value) @client = value end |
#object_hash ⇒ Object (readonly)
Returns the value of attribute object_hash.
25 26 27 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 25 def object_hash @object_hash end |
Instance Method Details
#block(id) ⇒ NotionRubyMapping::Base
178 179 180 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 178 def block(id) object_for_key(id) { block_request id } end |
#block_children(id) ⇒ NotionRubyMapping::Base
184 185 186 187 188 189 190 191 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 184 def block_children(id) array = [] sleep @wait @client.block_children(block_id: id, sleep_interval: @wait, max_retries: 20) do |page| array.concat page.results end Base.create_from_json({"object" => "list", "results" => array}) end |
#block_path(block_id) ⇒ String (frozen)
64 65 66 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 64 def block_path(block_id) "v1/blocks/#{block_id}" end |
#block_request(block_id) ⇒ Hash
121 122 123 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 121 def block_request(block_id) request :get, block_path(block_id) end |
#clear_object_hash ⇒ Hash
208 209 210 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 208 def clear_object_hash @object_hash = {} end |
#create_client(notion_token, wait: 0.3333, debug: false) ⇒ NotionRubyMapping::NotionCache
30 31 32 33 34 35 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 30 def create_client(notion_token, wait: 0.3333, debug: false) @notion_token = notion_token @wait = wait @debug = debug self end |
#create_database_request(payload) ⇒ Object
144 145 146 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 144 def create_database_request(payload) request :post, databases_path, payload end |
#create_page_request(payload) ⇒ Hash
140 141 142 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 140 def create_page_request(payload) request :post, "v1/pages", payload end |
#database(id) ⇒ NotionRubyMapping::Base
172 173 174 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 172 def database(id) object_for_key(id) { database_request id } end |
#database_path(database_id) ⇒ String (frozen)
41 42 43 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 41 def database_path(database_id) "v1/databases/#{database_id}" end |
#database_query(id, query) ⇒ NotionRubyMapping::Base
196 197 198 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 196 def database_query(id, query) Base.create_from_json database_query_request(id, query.query_json) end |
#database_query_request(database_id, payload) ⇒ Object
115 116 117 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 115 def database_query_request(database_id, payload) request :post, "v1/databases/#{database_id}/query", payload end |
#database_request(database_id) ⇒ Hash
111 112 113 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 111 def database_request(database_id) request :get, database_path(database_id) end |
#databases_path ⇒ String (frozen)
47 48 49 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 47 def databases_path "v1/databases" end |
#hex_id(id) ⇒ String
150 151 152 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 150 def hex_id(id) id&.gsub "-", "" end |
#object_for_key(id) ⇒ NotionRubyMapping::Base
156 157 158 159 160 161 162 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 156 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
166 167 168 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 166 def page(id) object_for_key(id) { page_request id } end |
#page_path(page_id) ⇒ String (frozen)
53 54 55 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 53 def page_path(page_id) "v1/pages/#{page_id}" end |
#page_request(page_id) ⇒ Hash
105 106 107 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 105 def page_request(page_id) request :get, page_path(page_id) end |
#pages_path ⇒ String (frozen)
58 59 60 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 58 def pages_path "v1/pages" end |
#query_database_path(database_id) ⇒ String (frozen)
70 71 72 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 70 def query_database_path(database_id) "v1/databases/#{database_id}/query" end |
#request(method, path, options = {}) ⇒ Hash
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 80 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_database(id, payload) ⇒ Object
202 203 204 205 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 202 def update_database(id, payload) sleep @wait @client.update_database payload.merge({database_id: id}) end |
#update_database_request(database_id, payload) ⇒ Hash
127 128 129 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 127 def update_database_request(database_id, payload) request :patch, "v1/databases/#{database_id}", payload end |
#update_page_request(page_id, payload) ⇒ Hash
134 135 136 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 134 def update_page_request(page_id, payload) request :patch, "v1/pages/#{page_id}", payload end |