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



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



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



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

def clear
  @json = {}
end

#merge_property(json) ⇒ Object



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

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

#property_schema_json(*others) ⇒ Hash



35
36
37
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 35

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

#property_values_json(*others) ⇒ Hash



29
30
31
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 29

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



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 42

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



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

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



64
65
66
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 64

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