Class: NotionRubyMapping::Base
- Inherits:
-
Object
- Object
- NotionRubyMapping::Base
- Defined in:
- lib/notion_ruby_mapping/base.rb
Overview
Notion Base object (Parent of Page / Database / List)
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#json ⇒ Object
readonly
Returns the value of attribute json.
Class Method Summary collapse
- .create_from_json(json) ⇒ NotionRubyMapping::Base
- .dry_run_script(method, path, json = nil) ⇒ Object
Instance Method Summary collapse
-
#[](key) ⇒ NotionRubyMapping::PropertyCache, Hash
Obtained Page value or PropertyCache.
-
#assign_property(klass, title) ⇒ NotionRubyMapping::Property
Generated property.
- #children ⇒ NotionRubyMapping::List
- #created_time ⇒ NotionRubyMapping::CreatedTimeProperty
-
#database? ⇒ TrueClass, FalseClass
True if Database object.
-
#icon ⇒ Hash?
Obtained Hash.
-
#initialize(json: nil, id: nil, assign: [], parent: nil) ⇒ Base
constructor
A new instance of Base.
-
#json_properties ⇒ Hash
protected.
- #last_edited_time ⇒ NotionRubyMapping::LastEditedTimeProperty
-
#new_record? ⇒ Boolean
True if new record.
-
#page? ⇒ TrueClass, FalseClass
True if Page object.
-
#properties ⇒ NotionRubyMapping::PropertyCache
Get or created PropertyCache object.
-
#property_values_json ⇒ Hash
Created json for update page.
-
#reload ⇒ NotionRubyMapping::Base
Reloaded self.
- #restore_from_json ⇒ NotionRubyMapping::Base
- #save(dry_run: false) ⇒ NotionRubyMapping::Base, String
- #set_icon(emoji: nil, url: nil) ⇒ NotionRubyMapping::Base
-
#title ⇒ String
Title.
- #update_json(json) ⇒ NotionRubyMapping::Base
Constructor Details
#initialize(json: nil, id: nil, assign: [], parent: nil) ⇒ Base
Returns a new instance of Base.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/notion_ruby_mapping/base.rb', line 11 def initialize(json: nil, id: nil, assign: [], parent: nil) @nc = NotionCache.instance @json = json @id = @nc.hex_id(id || json && @json["id"]) @new_record = true unless parent.nil? raise StandardError, "Unknown id" if !is_a?(List) && @id.nil? && parent.nil? @payload = Payload.new(parent && {"parent" => parent}) @property_cache = nil @created_time = nil @last_edited_time = nil assign.each_slice(2) { |(klass, key)| assign_property(klass, key) } end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
24 25 26 |
# File 'lib/notion_ruby_mapping/base.rb', line 24 def id @id end |
#json ⇒ Object (readonly)
Returns the value of attribute json.
24 25 26 |
# File 'lib/notion_ruby_mapping/base.rb', line 24 def json @json end |
Class Method Details
.create_from_json(json) ⇒ NotionRubyMapping::Base
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/notion_ruby_mapping/base.rb', line 114 def self.create_from_json(json) case json["object"] when "page" Page.new json: json when "database" Database.new json: json when "list" List.new json: json when "block" Block.new json: json else raise StandardError, json.inspect end end |
.dry_run_script(method, path, json = nil) ⇒ Object
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/notion_ruby_mapping/base.rb', line 193 def self.dry_run_script(method, path, json = nil) shell = [ "#!/bin/sh\ncurl #{method == :get ? "" : "-X #{method.to_s.upcase}"} 'https://api.notion.com/#{path}'", " -H 'Notion-Version: 2022-02-22'", " -H 'Authorization: Bearer '\"$NOTION_API_KEY\"''", ] shell << " -H 'Content-Type: application/json'" unless path == :get shell << " --data '#{JSON.generate json}'" if json shell.join(" \\\n") end |
Instance Method Details
#[](key) ⇒ NotionRubyMapping::PropertyCache, Hash
Returns obtained Page value or PropertyCache.
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/notion_ruby_mapping/base.rb', line 28 def [](key) unless @json raise StandardError, "Unknown id" if @id.nil? reload end case key when "properties" properties else @json[key] end end |
#assign_property(klass, title) ⇒ NotionRubyMapping::Property
Returns generated property.
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/notion_ruby_mapping/base.rb', line 132 def assign_property(klass, title) @property_cache ||= PropertyCache.new({}, base_type: database? ? :database : :page) property = if database? klass.new(title, will_update: new_record?, base_type: :database) else klass.new(title, will_update: true, base_type: :page) end @property_cache.add_property property property end |
#children ⇒ NotionRubyMapping::List
145 146 147 |
# File 'lib/notion_ruby_mapping/base.rb', line 145 def children @children ||= @nc.block_children(id) end |
#created_time ⇒ NotionRubyMapping::CreatedTimeProperty
43 44 45 |
# File 'lib/notion_ruby_mapping/base.rb', line 43 def created_time @created_time ||= CreatedTimeProperty.new("__timestamp__") end |
#database? ⇒ TrueClass, FalseClass
Returns true if Database object.
48 49 50 |
# File 'lib/notion_ruby_mapping/base.rb', line 48 def database? is_a? Database end |
#icon ⇒ Hash?
Returns obtained Hash.
53 54 55 |
# File 'lib/notion_ruby_mapping/base.rb', line 53 def icon self["icon"] end |
#json_properties ⇒ Hash
protected
186 187 188 |
# File 'lib/notion_ruby_mapping/base.rb', line 186 def json_properties @json && @json["properties"] end |
#last_edited_time ⇒ NotionRubyMapping::LastEditedTimeProperty
58 59 60 |
# File 'lib/notion_ruby_mapping/base.rb', line 58 def last_edited_time @last_edited_time ||= LastEditedTimeProperty.new("__timestamp__") end |
#new_record? ⇒ Boolean
Returns true if new record.
63 64 65 |
# File 'lib/notion_ruby_mapping/base.rb', line 63 def new_record? @new_record end |
#page? ⇒ TrueClass, FalseClass
Returns true if Page object.
68 69 70 |
# File 'lib/notion_ruby_mapping/base.rb', line 68 def page? is_a? Page end |
#properties ⇒ NotionRubyMapping::PropertyCache
Returns get or created PropertyCache object.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/notion_ruby_mapping/base.rb', line 73 def properties unless @property_cache unless @json raise StandardError, "Unknown id" if @id.nil? reload end @property_cache = PropertyCache.new json_properties, base_type: database? ? :database : :page end @property_cache end |
#property_values_json ⇒ Hash
Returns created json for update page.
150 151 152 |
# File 'lib/notion_ruby_mapping/base.rb', line 150 def property_values_json @payload.property_values_json @property_cache end |
#reload ⇒ NotionRubyMapping::Base
Returns reloaded self.
155 156 157 158 |
# File 'lib/notion_ruby_mapping/base.rb', line 155 def reload update_json reload_json self end |
#restore_from_json ⇒ NotionRubyMapping::Base
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/notion_ruby_mapping/base.rb', line 161 def restore_from_json return if (ps = @json["properties"]).nil? properties.json = json_properties return unless is_a?(Page) || is_a?(Database) ps.each do |key, json| properties[key].update_from_json json end self end |
#save(dry_run: false) ⇒ NotionRubyMapping::Base, String
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/notion_ruby_mapping/base.rb', line 86 def save(dry_run: false) if dry_run @new_record ? create(dry_run: true) : update(dry_run: true) else @new_record ? create : update @property_cache.clear_will_update @payload.clear self end end |
#set_icon(emoji: nil, url: nil) ⇒ NotionRubyMapping::Base
100 101 102 103 |
# File 'lib/notion_ruby_mapping/base.rb', line 100 def set_icon(emoji: nil, url: nil) @payload.set_icon(emoji: emoji, url: url) if page? || database? self end |
#title ⇒ String
Returns title.
106 107 108 |
# File 'lib/notion_ruby_mapping/base.rb', line 106 def title properties.select { |p| p.is_a? TitleProperty }.map(&:full_text).join "" end |
#update_json(json) ⇒ NotionRubyMapping::Base
175 176 177 178 179 180 181 182 |
# File 'lib/notion_ruby_mapping/base.rb', line 175 def update_json(json) raise StandardError, json.inspect unless json["object"] != "error" && @json.nil? || @json["type"] == json["type"] @json = json @id = @nc.hex_id(@json["id"]) restore_from_json self end |