Class: NotionRubyMapping::CommentObject

Inherits:
Object
  • Object
show all
Defined in:
lib/notion_ruby_mapping/objects/comment_object.rb

Overview

CommentObject

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, text_objects: nil, json: {}) ⇒ CommentObject

Returns a new instance of CommentObject.

Parameters:

  • text_objects (String) (defaults to: nil)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/notion_ruby_mapping/objects/comment_object.rb', line 7

def initialize(id: nil, text_objects: nil, json: {})
  nc = NotionCache.instance
  if text_objects
    @id = nc.hex_id id
    @text_objects = RichTextArray.new "rich_text", text_objects: text_objects
    @json = {}
  elsif json
    @json = json
    @id = nc.hex_id json["id"]
    @text_objects = RichTextArray.new "rich_text", json: json["rich_text"]
  else
    raise StandardError, "Either text_objects or json is required CommentObject"
  end
  @will_update = false
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



22
23
24
# File 'lib/notion_ruby_mapping/objects/comment_object.rb', line 22

def id
  @id
end

#jsonObject (readonly)

Returns the value of attribute json.



22
23
24
# File 'lib/notion_ruby_mapping/objects/comment_object.rb', line 22

def json
  @json
end

#text_objectsObject (readonly)

Returns the value of attribute text_objects.



22
23
24
# File 'lib/notion_ruby_mapping/objects/comment_object.rb', line 22

def text_objects
  @text_objects
end

#will_updateObject (readonly)

Returns the value of attribute will_update.



22
23
24
# File 'lib/notion_ruby_mapping/objects/comment_object.rb', line 22

def will_update
  @will_update
end

Class Method Details

.find(comment_id) ⇒ Object



24
25
26
# File 'lib/notion_ruby_mapping/objects/comment_object.rb', line 24

def self.find(comment_id)
  new json: NotionCache.instance.comment_request(comment_id)
end

Instance Method Details

#destroy(dry_run: false) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/notion_ruby_mapping/objects/comment_object.rb', line 28

def destroy(dry_run: false)
  if dry_run
    Base.dry_run_script :delete, NotionCache.instance.comment_path(id)
  else
    json = NotionCache.instance.destroy_comment_request id
    @text_objects = RichTextArray.new "rich_text", json: json["rich_text"]
    self
  end
end

#discussion_idObject



38
39
40
# File 'lib/notion_ruby_mapping/objects/comment_object.rb', line 38

def discussion_id
  NotionCache.instance.hex_id @json["discussion_id"]
end

#full_textString

Returns full_text.

Returns:

  • (String)

    full_text



43
44
45
# File 'lib/notion_ruby_mapping/objects/comment_object.rb', line 43

def full_text
  @text_objects.full_text
end

#markdown=(markdown) ⇒ Object



47
48
49
50
# File 'lib/notion_ruby_mapping/objects/comment_object.rb', line 47

def markdown=(markdown)
  @markdown = markdown
  @will_update = true
end

#rich_text_objects=(text_objects) ⇒ Object



52
53
54
55
# File 'lib/notion_ruby_mapping/objects/comment_object.rb', line 52

def rich_text_objects=(text_objects)
  @text_objects.rich_text_objects = text_objects
  @will_update = true
end

#save(dry_run: false) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/notion_ruby_mapping/objects/comment_object.rb', line 57

def save(dry_run: false)
  return unless @will_update

  if dry_run
    json = @markdown ? {"markdown" => @markdown} : {"rich_text" => @text_objects.property_values_json}
    @markdown = nil
    Base.dry_run_script :patch, NotionCache.instance.comment_path(id), json
  else
    json = if @markdown
             NotionCache.instance.update_comment_request id, markdown: @markdown
           else
             NotionCache.instance.update_comment_request id, rich_text: @text_objects.property_values_json
           end
    @markdown = nil
    @text_objects = RichTextArray.new "rich_text", json: json["rich_text"]
    @will_update = false
  end
end