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_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.
-
#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
Returns Block object or nil.
168 169 170 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 168 def block(id) object_for_key(id) { block_request id } end |
#block_children(id) ⇒ NotionRubyMapping::Base
Returns List object.
174 175 176 177 178 179 180 181 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 174 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)
Returns page_path.
58 59 60 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 58 def block_path(block_id) "v1/blocks/#{block_id}" end |
#block_request(block_id) ⇒ Hash
Returns response.
115 116 117 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 115 def block_request(block_id) request :get, block_path(block_id) end |
#clear_object_hash ⇒ Hash
204 205 206 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 204 def clear_object_hash @object_hash = {} end |
#create_client(notion_token, wait: 0.3333, debug: false) ⇒ NotionRubyMapping::NotionCache
Returns self (NotionCache.instance).
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_page_request(payload) ⇒ Hash
Returns response.
134 135 136 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 134 def create_page_request(payload) request :post, "v1/pages", payload end |
#database(id) ⇒ NotionRubyMapping::Base
Returns Database object or nil.
162 163 164 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 162 def database(id) object_for_key(id) { database_request id } end |
#database_path(database_id) ⇒ String (frozen)
Returns page_path.
52 53 54 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 52 def database_path(database_id) "v1/databases/#{database_id}" end |
#database_query(id, query) ⇒ NotionRubyMapping::Base
Returns List object.
186 187 188 189 190 191 192 193 194 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 186 def database_query(id, query) parameters = {} parameters[:filter] = query.filter unless query.filter.empty? parameters[:sorts] = query.sort unless query.sort.empty? parameters[:start_cursor] = query.start_cursor if query.start_cursor parameters[:page_size] = query.page_size if query.page_size Base.create_from_json database_query_request(id, parameters) end |
#database_query_request(database_id, payload) ⇒ Object
109 110 111 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 109 def database_query_request(database_id, payload) request :post, "v1/databases/#{database_id}/query", payload end |
#database_request(database_id) ⇒ Hash
Returns response.
105 106 107 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 105 def database_request(database_id) request :get, database_path(database_id) end |
#hex_id(id) ⇒ String
Returns id without “-”.
140 141 142 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 140 def hex_id(id) id&.gsub "-", "" end |
#object_for_key(id) ⇒ NotionRubyMapping::Base
146 147 148 149 150 151 152 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 146 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.
156 157 158 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 156 def page(id) object_for_key(id) { page_request id } end |
#page_path(page_id) ⇒ String (frozen)
Returns page_path.
41 42 43 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 41 def page_path(page_id) "v1/pages/#{page_id}" end |
#page_request(page_id) ⇒ Hash
Returns response.
99 100 101 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 99 def page_request(page_id) request :get, page_path(page_id) end |
#pages_path ⇒ String (frozen)
Returns page_path.
46 47 48 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 46 def pages_path "v1/pages" end |
#query_database_path(database_id) ⇒ String (frozen)
Returns page_path.
64 65 66 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 64 def query_database_path(database_id) "v1/databases/#{database_id}/query" end |
#request(method, path, options = {}) ⇒ Hash
Returns response hash.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 74 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
198 199 200 201 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 198 def update_database(id, payload) sleep @wait @client.update_database payload.merge({database_id: id}) end |
#update_database_request(database_id, payload) ⇒ Hash
Returns response.
121 122 123 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 121 def update_database_request(database_id, payload) request :patch, "v1/databases/#{database_id}", payload end |
#update_page_request(page_id, payload) ⇒ Hash
Returns response.
128 129 130 |
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 128 def update_page_request(page_id, payload) request :patch, "v1/pages/#{page_id}", payload end |