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:



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

Returns List object.

Parameters:

  • id (String)

    page_id / block_id (with or without “-”)

Returns:



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)

Returns page_path.

Parameters:

  • block_id (String)

Returns:

  • (String (frozen))

    page_path



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

Returns response.

Parameters:

  • block_id (String)

Returns:

  • (Hash)

    response



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_hashHash

Returns:

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

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

Returns response.

Parameters:

  • payload (Hash)

Returns:

  • (Hash)

    response



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

Returns Database object or nil.

Parameters:

  • id (String)

    database_id (with or without “-”)

Returns:



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)

Returns page_path.

Parameters:

  • database_id (String)

Returns:

  • (String (frozen))

    page_path



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

Returns List object.

Parameters:

Returns:



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

Returns response.

Parameters:

  • database_id (String)

Returns:

  • (Hash)

    response



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

Returns page_path.

Parameters:

  • database_id (String)

Returns:

  • (String (frozen))

    page_path



47
48
49
# File 'lib/notion_ruby_mapping/notion_cache.rb', line 47

def databases_path
  "v1/databases"
end

#hex_id(id) ⇒ String

Returns id without “-”.

Parameters:

  • id (String)

    id string with “-”

Returns:

  • (String)

    id without “-”



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

Parameters:

  • id (String)

    id (with or without “-”)

Returns:



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

Returns Page object or nil.

Parameters:

  • id (String)

    page_id (with or without “-”)

Returns:



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)

Returns page_path.

Parameters:

  • page_id (String)

Returns:

  • (String (frozen))

    page_path



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

Returns response.

Parameters:

  • page_id (String)

Returns:

  • (Hash)

    response



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

Returns page_path.

Returns:

  • (String (frozen))

    page_path



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)

Returns page_path.

Parameters:

  • database_id (String)

Returns:

  • (String (frozen))

    page_path



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

Returns response hash.

Parameters:

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

Returns:

  • (Hash)

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


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

Returns response.

Parameters:

  • database_id (String)

Returns:

  • (Hash)

    response



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

Returns response.

Parameters:

  • page_id (String)
  • payload (Hash)

Returns:

  • (Hash)

    response



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