Class: NotionRubyMapping::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/notion_ruby_mapping/controllers/payload.rb

Overview

Payload class

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ Payload

Returns a new instance of Payload.



6
7
8
9
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 6

def initialize(json)
  @json = json || {}
  @update_block_key = []
end

Instance Method Details

#add_update_block_key(key) ⇒ Object

Parameters:

  • key (String)


12
13
14
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 12

def add_update_block_key(key)
  @update_block_key << key
end

#clearHash

Returns {}.

Returns:

  • (Hash)

    {}



17
18
19
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 17

def clear
  @json = {}
end

#description=(text_objects) ⇒ Object



21
22
23
24
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 21

def description=(text_objects)
  rta = RichTextArray.rich_text_array "description", text_objects
  @json.merge!(rta.update_property_schema_json true)
end

#is_inline=(flag) ⇒ Object

Parameters:

  • flag (Boolean)


27
28
29
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 27

def is_inline=(flag)
  @json["is_inline"] = flag
end

#merge_property(json) ⇒ Object

Parameters:

  • json (Hash)


32
33
34
35
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 32

def merge_property(json)
  @json["properties"] ||= {}
  @json["properties"].merge!(json)
end

#property_schema_json(*others) ⇒ Hash

Returns created json.

Parameters:

  • others (Object)

Returns:

  • (Hash)

    created json



45
46
47
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 45

def property_schema_json(*others)
  others.compact.reduce({}) { |hash, o| hash.merge o.property_schema_json }.merge @json
end

#property_values_json(*others) ⇒ Hash

Returns created json.

Parameters:

  • others (Object)

Returns:

  • (Hash)

    created json



39
40
41
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 39

def property_values_json(*others)
  others.compact.reduce({}) { |hash, o| hash.merge o.property_values_json }.merge @json
end

#set_icon(emoji: nil, url: nil) ⇒ NotionRubyMapping::Payload

Returns updated Payload.

Parameters:

  • emoji (String) (defaults to: nil)
  • url (String) (defaults to: nil)

Returns:



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 52

def set_icon(emoji: nil, url: nil)
  payload = if emoji
              {"type" => "emoji", "emoji" => emoji}
            elsif url
              {"type" => "external", "external" => {"url" => url}}
            else
              {}
            end
  @json["icon"] = payload
  self
end

#update_block_json(type, json) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 64

def update_block_json(type, json)
  sub_json = json[type]
  ans = {type => sub_json.slice(*@update_block_key)}
  ans[type]["caption"] = sub_json["caption"] if sub_json["caption"]
  ans[type]["rich_text"] = sub_json["rich_text"] if sub_json["rich_text"]
  ans
end

#update_property_schema_json(*others) ⇒ Hash

Returns created json.

Parameters:

  • others (Object)

Returns:

  • (Hash)

    created json



74
75
76
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 74

def update_property_schema_json(*others)
  others.compact.reduce({}) { |hash, o| hash.merge o.update_property_schema_json }.merge @json
end