class NotionRb::Block

Attributes

uuid[RW]

Public Class Methods

new(url_or_uuid) click to toggle source
# File lib/notion_rb/block.rb, line 10
def initialize(url_or_uuid)
  @block_container = NotionRb::Utils::BlockCache.instance
  @uuid = parse_uuid url_or_uuid
  get_resource
end

Public Instance Methods

children() click to toggle source
# File lib/notion_rb/block.rb, line 44
def children
  if type == 'collection_view_page'
    @children ||= [self.class.new(metadata[:collection_id])]
  else
    @children ||= @block_container.children(@uuid).map { |child_uuid| self.class.new(child_uuid) }
  end
end
create_child() click to toggle source
# File lib/notion_rb/block.rb, line 52
def create_child
  creator = NotionRb::Api::Create.new(parent_id: @uuid)
  return nil unless creator.success?

  @block[:children] << creator.block_uuid
  self.class.new(creator.block_uuid)
end
destroy() click to toggle source
# File lib/notion_rb/block.rb, line 65
def destroy
  NotionRb::Api::Destroy.new(notion_id: @uuid, parent_id: @block[:parent_id]).success?
end
metadata() click to toggle source
# File lib/notion_rb/block.rb, line 36
def metadata
  @block[:metadata].except(:properties)
end
parent() click to toggle source
# File lib/notion_rb/block.rb, line 40
def parent
  @parent ||= self.class.new(@block[:parent_id])
end
restore() click to toggle source
# File lib/notion_rb/block.rb, line 69
def restore
  NotionRb::Api::Restore.new(notion_id: @uuid, parent_id: @block[:parent_id]).success?
end
save() click to toggle source
# File lib/notion_rb/block.rb, line 60
def save
  # TODO: add validations if needed
  post_resource
end
title() click to toggle source
# File lib/notion_rb/block.rb, line 16
def title
  @block[:title]
end
title=(title) click to toggle source
# File lib/notion_rb/block.rb, line 20
def title=(title)
  @block[:title] = title
  save
end
type() click to toggle source
# File lib/notion_rb/block.rb, line 25
def type
  @block[:block_type]
end
type=(type) click to toggle source
# File lib/notion_rb/block.rb, line 29
def type=(type)
  return unless valid_block_type?(type)

  @block[:block_type] = type
  save
end

Private Instance Methods

get_resource() click to toggle source
# File lib/notion_rb/block.rb, line 75
def get_resource
  @block = @block_container.find(@uuid)
  @block_container.add_collection_view_blocks(@block)
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/notion_rb/block.rb, line 85
def method_missing(method, *args, &block)
  if schema_methods.include?(method)
    send_schema_method(method)
  else
    super
  end
end
post_resource() click to toggle source
# File lib/notion_rb/block.rb, line 80
def post_resource
  # TODO: detect changes if any before post
  NotionRb::Api::Update.new(notion_id: @uuid, title: @block[:title], block_type: @block[:block_type]).success?
end
respond_to_missing?(method_name, *args) click to toggle source
Calls superclass method
# File lib/notion_rb/block.rb, line 93
def respond_to_missing?(method_name, *args)
  schema_methods.include?(method_name) || super
end
schema_methods() click to toggle source
# File lib/notion_rb/block.rb, line 97
def schema_methods
  return [] unless parent.type == 'collection'

  parent.metadata[:schema].values.map { |value| value['name'].tableize.to_sym }
end
send_schema_method(method) click to toggle source
# File lib/notion_rb/block.rb, line 103
def send_schema_method(method)
  parent_schema = parent.metadata[:schema]
  @block.dig(:metadata, :properties).each do |key, value|
    schema = parent_schema[key]
    next unless schema['name'].tableize.to_sym == method

    return value[0][0]
  end
end