Class: NotionRubyMapping::RichTextObject

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

Overview

RichTextObject

Direct Known Subclasses

EquationObject, MentionObject, TextObject

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, options = {}) ⇒ TextObject

Parameters:

  • type (String)

8
9
10
11
12
13
14
15
16
# File 'lib/notion_ruby_mapping/objects/rich_text_object.rb', line 8

def initialize(type, options = {})
  if instance_of?(RichTextObject)
    raise StandardError,
          "RichTextObject is abstract class.  Please use TextObject."
  end

  @type = type
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.


17
18
19
# File 'lib/notion_ruby_mapping/objects/rich_text_object.rb', line 17

def options
  @options
end

#will_updateObject (readonly)

Returns the value of attribute will_update.


17
18
19
# File 'lib/notion_ruby_mapping/objects/rich_text_object.rb', line 17

def will_update
  @will_update
end

Class Method Details

.create_from_json(json) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/notion_ruby_mapping/objects/rich_text_object.rb', line 19

def self.create_from_json(json)
  type = json["type"]
  options = (json["annotations"] || {}).merge(json.slice("plain_text", "href"))
  case type
  when "text"
    TextObject.new json["plain_text"], options
  when "equation"
    EquationObject.new json["equation"]["expression"], options
  when "mention"
    mention = json["mention"]
    case mention["type"]
    when "user"
      MentionObject.new options.merge({"user_id" => mention["user"]["id"]})
    when "page"
      MentionObject.new options.merge({"page_id" => mention["page"]["id"]})
    when "database"
      MentionObject.new options.merge({"database_id" => mention["database"]["id"]})
    when "date"
      MentionObject.new options.merge(mention["date"].slice("start", "end", "time_zone"))
    when "template_mention"
      template_mention = mention["template_mention"]
      case template_mention["type"]
      when "template_mention_date"
        MentionObject.new options.merge({"template_mention" => template_mention["template_mention_date"]})
      else
        MentionObject.new options.merge({"template_mention" => template_mention["template_mention_user"]})
      end
    when "link_preview"
      MentionObject.new options.merge({"link_preview" => mention["link_preview"]["url"]})
    when "link_mention"
      lm_keys = %w[href icon_url link_provider thumbnail_url title]
      MentionObject.new options.merge(mention["link_mention"].slice(*lm_keys))
    else
      raise StandardError, "Unknown mention type: #{mention["type"]}"
    end
  else
    raise StandardError, json
  end
end

.text_object(to) ⇒ NotionRubyMapping::RichTextObject

Returns RichTextObject.

Parameters:

Returns:


61
62
63
64
65
66
67
# File 'lib/notion_ruby_mapping/objects/rich_text_object.rb', line 61

def self.text_object(to)
  if to.is_a? RichTextObject
    to
  else
    TextObject.new to
  end
end

Instance Method Details

#bold=(flag) ⇒ Boolean

Returns input flag.

Parameters:

  • flag (Boolean)

Returns:

  • (Boolean)

    input flag


99
100
101
102
# File 'lib/notion_ruby_mapping/objects/rich_text_object.rb', line 99

def bold=(flag)
  @will_update = true
  @options["bold"] = flag
end

#clear_will_updateFalseClass

Returns:

  • (FalseClass)

80
81
82
# File 'lib/notion_ruby_mapping/objects/rich_text_object.rb', line 80

def clear_will_update
  @will_update = false
end

#code=(flag) ⇒ Boolean

Returns input flag.

Parameters:

  • flag (Boolean)

Returns:

  • (Boolean)

    input flag


127
128
129
130
# File 'lib/notion_ruby_mapping/objects/rich_text_object.rb', line 127

def code=(flag)
  @will_update = true
  @options["code"] = flag
end

#color=(color) ⇒ String

Returns input color.

Parameters:

  • color (String)

Returns:

  • (String)

    input color


134
135
136
137
# File 'lib/notion_ruby_mapping/objects/rich_text_object.rb', line 134

def color=(color)
  @will_update = true
  @options["color"] = color
end

#href=(url) ⇒ String

Returns input text.

Parameters:

  • url (String)

Returns:

  • (String)

    input text


86
87
88
89
# File 'lib/notion_ruby_mapping/objects/rich_text_object.rb', line 86

def href=(url)
  @will_update = true
  @options["href"] = url
end

#italic=(flag) ⇒ Boolean

Returns input flag.

Parameters:

  • flag (Boolean)

Returns:

  • (Boolean)

    input flag


106
107
108
109
# File 'lib/notion_ruby_mapping/objects/rich_text_object.rb', line 106

def italic=(flag)
  @will_update = true
  @options["italic"] = flag
end

#plain_text=(value) ⇒ String

Returns input text.

Parameters:

Returns:

  • (String)

    input text


93
94
95
# File 'lib/notion_ruby_mapping/objects/rich_text_object.rb', line 93

def plain_text=(value)
  text(value)
end

#property_values_jsonHash{String (frozen)->Object

Returns ].

Returns:

  • (Hash{String (frozen)->Object)

    ]


70
71
72
73
74
75
76
77
# File 'lib/notion_ruby_mapping/objects/rich_text_object.rb', line 70

def property_values_json
  {
    "type" => @type,
    @type => partial_property_values_json,
    "plain_text" => @options["plain_text"],
    "href" => @options["href"],
  }.merge annotations_json
end

#strikethrough=(flag) ⇒ Boolean

Returns input flag.

Parameters:

  • flag (Boolean)

Returns:

  • (Boolean)

    input flag


113
114
115
116
# File 'lib/notion_ruby_mapping/objects/rich_text_object.rb', line 113

def strikethrough=(flag)
  @will_update = true
  @options["strikethrough"] = flag
end

#underline=(flag) ⇒ Boolean

Returns input flag.

Parameters:

  • flag (Boolean)

Returns:

  • (Boolean)

    input flag


120
121
122
123
# File 'lib/notion_ruby_mapping/objects/rich_text_object.rb', line 120

def underline=(flag)
  @will_update = true
  @options["underline"] = flag
end