Class: NotionAPI::BlockTemplate

Inherits:
Core
  • Object
show all
Includes:
Utils
Defined in:
lib/notion_api/notion_types/template.rb

Overview

Base Template for all blocks. Inherits core methods from the Block class defined in block.rb

Constant Summary

Constants included from Utils

Utils::URLS

Instance Attribute Summary collapse

Attributes inherited from Core

#clean_id, #cookies, #headers

Instance Method Summary collapse

Methods included from Utils

#build_payload

Methods inherited from Core

#children, #children_ids, #get_page

Constructor Details

#initialize(id, title, parent_id) ⇒ BlockTemplate

Returns a new instance of BlockTemplate.



11
12
13
14
15
# File 'lib/notion_api/notion_types/template.rb', line 11

def initialize(id, title, parent_id)
  @id = id
  @title = title
  @parent_id = parent_id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/notion_api/notion_types/template.rb', line 9

def id
  @id
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



9
10
11
# File 'lib/notion_api/notion_types/template.rb', line 9

def parent_id
  @parent_id
end

#titleObject

Returns the value of attribute title.



9
10
11
# File 'lib/notion_api/notion_types/template.rb', line 9

def title
  @title
end

Instance Method Details

#convert(block_class_to_convert_to) ⇒ Object



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/notion_api/notion_types/template.rb', line 27

def convert(block_class_to_convert_to)
  # ! convert a block from its current type to another.
  # ! block_class_to_convert_to -> the type of block to convert to : ``cls``
  if type == block_class_to_convert_to.notion_type
    # if converting to same type, skip and return self
    self
  else
    # setup cookies, headers, and grab/create static vars for request
    cookies = Core.options["cookies"]
    headers = Core.options["headers"]
    request_url = URLS[:UPDATE_BLOCK]

    # set random IDs for request
    request_id = extract_id(SecureRandom.hex(16))
    transaction_id = extract_id(SecureRandom.hex(16))
    space_id = extract_id(SecureRandom.hex(16))
    request_ids = {
      request_id: request_id,
      transaction_id: transaction_id,
      space_id: space_id,
    }

    # build hash's that contain the operations to send to Notion
    convert_type_hash = Utils::BlockComponents.convert_type(@id, block_class_to_convert_to)
    last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(@parent_id)
    last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)

    operations = [
      convert_type_hash,
      last_edited_time_parent_hash,
      last_edited_time_child_hash,
    ]

    request_body = build_payload(operations, request_ids)
    response = HTTParty.post(
      request_url,
      body: request_body.to_json,
      cookies: cookies,
      headers: headers,
    )
    unless response.code == 200; raise "There was an issue completing your request. Here is the response from Notion: #{response.body}, and here is the payload that was sent: #{operations}.
         Please try again, and if issues persist open an issue in GitHub.";         end

    block_class_to_convert_to.new(@id, @title, @parent_id)
  end
end

#create(block_type, block_title, target = nil, position = "after", options: {}) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/notion_api/notion_types/template.rb', line 207

def create(block_type, block_title, target = nil, position = "after", options: {})
  # ! create a new block
  # ! block_type -> the type of block to create : ``cls``
  # ! block_title -> the title of the new block : ``str``
  # ! target -> the block_id that the new block should be placed after. ``str``
  # ! position -> 'after' or 'before'

  positions_hash = {
    "after" => "listAfter",
    "before" => "listBefore",
  }
  unless positions_hash.keys.include?(position); raise "Invalid position. You said: #{position}, valid options are: #{positions_hash.keys.join(", ")}"; end

  position_command = positions_hash[position]

  new_block_id = extract_id(SecureRandom.hex(16))
  request_id = extract_id(SecureRandom.hex(16))
  transaction_id = extract_id(SecureRandom.hex(16))
  space_id = extract_id(SecureRandom.hex(16))

  request_ids = {
    request_id: request_id,
    transaction_id: transaction_id,
    space_id: space_id,
  }

  block_type.create(@id, new_block_id, block_title, target, position_command, request_ids, options)
end

#duplicate(target_block = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/notion_api/notion_types/template.rb', line 74

def duplicate(target_block = nil)
  # ! duplicate the block that this method is invoked upon.
  # ! target_block -> the block to place the duplicated block after. Can be any valid Block ID! : ``str``
  cookies = Core.options["cookies"]
  headers = Core.options["headers"]
  request_url = URLS[:UPDATE_BLOCK]

  new_block_id = extract_id(SecureRandom.hex(16))
  request_id = extract_id(SecureRandom.hex(16))
  transaction_id = extract_id(SecureRandom.hex(16))
  space_id = extract_id(SecureRandom.hex(16))

  root_children = children_ids(@id)
  sub_children = []
  root_children.each { |root_id| sub_children.push(children_ids(root_id)) }

  request_ids = {
    request_id: request_id,
    transaction_id: transaction_id,
    space_id: space_id,
  }
  body = {
    pageId: @id,
    chunkNumber: 0,
    limit: 100,
    verticalColumns: false,
  }

  user_notion_id = get_notion_id(body)

  block = target_block ? get(target_block) : self # allows dev to place block anywhere!

  props_and_formatting = get_block_props_and_format(@id, @title)
  props = props_and_formatting[:properties]
  formats = props_and_formatting[:format]
  duplicate_hash = Utils::BlockComponents.duplicate(type, @title, block.id, new_block_id, user_notion_id, root_children, props, formats)
  set_parent_alive_hash = Utils::BlockComponents.set_parent_to_alive(block.parent_id, new_block_id)
  block_location_hash = Utils::BlockComponents.block_location_add(block.parent_id, block.id, new_block_id, target_block, "listAfter")
  last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(block.parent_id)
  last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(block.id)

  operations = [
    duplicate_hash,
    set_parent_alive_hash,
    block_location_hash,
    last_edited_time_parent_hash,
    last_edited_time_child_hash,
  ]

  request_body = build_payload(operations, request_ids)
  response = HTTParty.post(
    request_url,
    body: request_body.to_json,
    cookies: cookies,
    headers: headers,
  )
  unless response.code == 200; raise "There was an issue completing your request. Here is the response from Notion: #{response.body}, and here is the payload that was sent: #{operations}.
       Please try again, and if issues persist open an issue in GitHub.";       end

  class_to_return = NotionAPI.const_get(Classes.select { |cls| NotionAPI.const_get(cls).notion_type == type }.join.to_s)
  class_to_return.new(new_block_id, @title, block.parent_id)
end

#move(target_block, position = "after") ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/notion_api/notion_types/template.rb', line 137

def move(target_block, position = "after")
  # ! move the block to a new location.
  # ! target_block -> the targetted block to move to. : ``str``
  # ! position -> where the block should be listed, in positions relative to the target_block [before, after, top-child, last-child]
  positions_hash = {
    "after" => "listAfter",
    "before" => "listBefore",
  }

  unless positions_hash.keys.include?(position); raise ArgumentError, "Invalid position. You said: #{position}, valid options are: #{positions_hash.keys.join(", ")}"; end

  position_command = positions_hash[position]
  cookies = Core.options["cookies"]
  headers = Core.options["headers"]
  request_url = URLS[:UPDATE_BLOCK]

  request_id = extract_id(SecureRandom.hex(16))
  transaction_id = extract_id(SecureRandom.hex(16))
  space_id = extract_id(SecureRandom.hex(16))

  request_ids = {
    request_id: request_id,
    transaction_id: transaction_id,
    space_id: space_id,
  }

  is_same_parent = (@parent_id == target_block.parent_id)
  set_block_dead_hash = Utils::BlockComponents.set_block_to_dead(@id) # kill the block this method is invoked on...
  block_location_remove_hash = Utils::BlockComponents.block_location_remove(@parent_id, @id) # remove the block this method is invoked on...
  parent_location_hash = Utils::BlockComponents.parent_location_add(is_same_parent ? @parent_id : target_block.parent_id, @id) # set parent location to alive
  block_location_add_hash = Utils::BlockComponents.block_location_add(is_same_parent ? @parent_id : target_block.parent_id, @id, target_block.id, position_command)
  last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(@parent_id)

  if is_same_parent
    last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)
    operations = [
      set_block_dead_hash,
      block_location_remove_hash,
      parent_location_hash,
      block_location_add_hash,
      last_edited_time_parent_hash,
      last_edited_time_child_hash,
    ]
  else
    last_edited_time_new_parent_hash = Utils::BlockComponents.last_edited_time(target_block.parent_id)
    last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)
    @parent_id = target_block.parent_id
    operations = [
      set_block_dead_hash,
      block_location_remove_hash,
      parent_location_hash,
      block_location_add_hash,
      last_edited_time_parent_hash,
      last_edited_time_new_parent_hash,
      last_edited_time_child_hash,
    ]
  end
  request_body = build_payload(operations, request_ids)
  response = HTTParty.post(
    request_url,
    body: request_body.to_json,
    cookies: cookies,
    headers: headers,
  )
  unless response.code == 200; raise "There was an issue completing your request. Here is the response from Notion: #{response.body}, and here is the payload that was sent: #{operations}.
       Please try again, and if issues persist open an issue in GitHub.";       end

  self
end