Class: NotionRubyMapping::NotionCache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/notion_ruby_mapping/notion_cache.rb

Overview

singleton class of caching Notion objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNotionCache

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_hashObject (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.

Parameters:

  • id (String)

    block_id (with or without “-”)

Returns:



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.

Parameters:

  • id (String)

    page_id / block_id (with or without “-”)

Returns:



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.

Parameters:

  • block_id (String)

Returns:

  • (String (frozen))

    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.

Parameters:

  • block_id (String)

Returns:

  • (Hash)

    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_hashHash

Returns:

  • (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).

Parameters:

  • notion_token (String)

Returns:



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.

Parameters:

  • payload (Hash)

Returns:

  • (Hash)

    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.

Parameters:

  • id (String)

    database_id (with or without “-”)

Returns:



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.

Parameters:

  • database_id (String)

Returns:

  • (String (frozen))

    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.

Parameters:

Returns:



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.

Parameters:

  • database_id (String)

Returns:

  • (Hash)

    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 “-”.

Parameters:

  • id (String)

    id string with “-”

Returns:

  • (String)

    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

Parameters:

  • id (String)

    id (with or without “-”)

Returns:



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.

Parameters:

  • id (String)

    page_id (with or without “-”)

Returns:



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.

Parameters:

  • page_id (String)

Returns:

  • (String (frozen))

    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.

Parameters:

  • page_id (String)

Returns:

  • (Hash)

    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_pathString (frozen)

Returns page_path.

Returns:

  • (String (frozen))

    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.

Parameters:

  • database_id (String)

Returns:

  • (String (frozen))

    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.

Parameters:

  • method (Symbol)
  • path (String)
  • options (Hash) (defaults to: {})

Returns:

  • (Hash)

    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, 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

Parameters:

  • id (String)

    page_id (with or without “-”)

  • payload (Hash)


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.

Parameters:

  • database_id (String)

Returns:

  • (Hash)

    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.

Parameters:

  • page_id (String)
  • payload (Hash)

Returns:

  • (Hash)

    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