Class: NotionRb::Block

Inherits:
Object
  • Object
show all
Includes:
Utils::Types, Utils::UuidValidator
Defined in:
lib/notion_rb/block.rb

Constant Summary

Constants included from Utils::Types

Utils::Types::TYPE_MAPPER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Types

#select_parser, #valid_block_type?

Methods included from Utils::UuidValidator

#parse_from_hex, #parse_from_url, #parse_uuid

Constructor Details

#initialize(url_or_uuid) ⇒ Block

Returns a new instance of Block.



10
11
12
13
14
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



85
86
87
88
89
90
91
# 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

Instance Attribute Details

#uuidObject

Returns the value of attribute uuid.



8
9
10
# File 'lib/notion_rb/block.rb', line 8

def uuid
  @uuid
end

Instance Method Details

#childrenObject



44
45
46
47
48
49
50
# File 'lib/notion_rb/block.rb', line 44

def children
  if type == 'collection_view_page'
    @children ||= [self.class.new([:collection_id])]
  else
    @children ||= @block_container.children(@uuid).map { |child_uuid| self.class.new(child_uuid) }
  end
end

#create_childObject



52
53
54
55
56
57
58
# 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

#destroyObject



65
66
67
# File 'lib/notion_rb/block.rb', line 65

def destroy
  NotionRb::Api::Destroy.new(notion_id: @uuid, parent_id: @block[:parent_id]).success?
end

#metadataObject



36
37
38
# File 'lib/notion_rb/block.rb', line 36

def 
  @block[:metadata].except(:properties)
end

#parentObject



40
41
42
# File 'lib/notion_rb/block.rb', line 40

def parent
  @parent ||= self.class.new(@block[:parent_id])
end

#restoreObject



69
70
71
# File 'lib/notion_rb/block.rb', line 69

def restore
  NotionRb::Api::Restore.new(notion_id: @uuid, parent_id: @block[:parent_id]).success?
end

#saveObject



60
61
62
63
# File 'lib/notion_rb/block.rb', line 60

def save
  # TODO: add validations if needed
  post_resource
end

#titleObject



16
17
18
# File 'lib/notion_rb/block.rb', line 16

def title
  @block[:title]
end

#title=(title) ⇒ Object



20
21
22
23
# File 'lib/notion_rb/block.rb', line 20

def title=(title)
  @block[:title] = title
  save
end

#typeObject



25
26
27
# File 'lib/notion_rb/block.rb', line 25

def type
  @block[:block_type]
end

#type=(type) ⇒ Object



29
30
31
32
33
34
# File 'lib/notion_rb/block.rb', line 29

def type=(type)
  return unless valid_block_type?(type)

  @block[:block_type] = type
  save
end