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
20
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 17

def clear
  @update_block_key = []
  @json = {}
end

#description=(text_objects) ⇒ Object



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

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)


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

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

#merge_property(json) ⇒ Object

Parameters:

  • json (Hash)


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

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



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

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



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

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

#set_cover(url: nil, file_upload_object: nil) ⇒ NotionRubyMapping::Payload

Parameters:

  • url (String, nil) (defaults to: nil)
  • file_upload_object (String, nil) (defaults to: nil)

Returns:



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

def set_cover(url: nil, file_upload_object: nil)
  payload = if url
              {"type" => "external", "external" => {"url" => url}}
            elsif file_upload_object
              {"type" => "file_upload", "file_upload" => {"id" => file_upload_object.id}}
            else
              {}
            end
  @json["cover"] = payload
  self
end

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

Returns updated Payload.

Parameters:

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

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 69

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

#update_block_json(type, json) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 83

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



93
94
95
# File 'lib/notion_ruby_mapping/controllers/payload.rb', line 93

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