Class: Synotion::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/synotion/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.



7
8
9
# File 'lib/synotion/client.rb', line 7

def initialize(api_key)
  @api_client = Notion::Client.new(token: api_key)
end

Instance Attribute Details

#api_clientObject (readonly)

Returns the value of attribute api_client.



5
6
7
# File 'lib/synotion/client.rb', line 5

def api_client
  @api_client
end

Instance Method Details

#append_blocks(page_id, children) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/synotion/client.rb', line 54

def append_blocks(page_id, children)
  return if children.empty?

  children.each_slice(100) do |chunk|
    api_client.block_append_children(
      block_id: page_id,
      children: chunk
    )
  end
rescue Notion::Api::Errors::NotionError => e
  raise NotionAPIError, "Failed to append blocks: #{e.message}"
end

#create_page(database_id:, properties:, children: []) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/synotion/client.rb', line 29

def create_page(database_id:, properties:, children: [])
  response = api_client.create_page(
    parent: { database_id: database_id },
    properties: properties,
    children: children
  )
  response
rescue Notion::Api::Errors::NotionError => e
  raise NotionAPIError, "Failed to create page: #{e.message}"
end

#delete_blocks(page_id) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/synotion/client.rb', line 67

def delete_blocks(page_id)
  blocks = get_page_blocks(page_id)
  blocks.each do |block|
    api_client.delete_block(block_id: block.id)
  end
rescue Notion::Api::Errors::NotionError => e
  raise NotionAPIError, "Failed to delete blocks: #{e.message}"
end

#find_page(database_id:, property:, value:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/synotion/client.rb', line 11

def find_page(database_id:, property:, value:)
  response = api_client.database_query(
    database_id: database_id,
    filter: {
      property: property,
      rich_text: {
        equals: value
      }
    }
  )

  return nil if response.results.empty?

  response.results.first
rescue Notion::Api::Errors::NotionError => e
  raise NotionAPIError, "Failed to find page: #{e.message}"
end

#get_page_blocks(page_id) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/synotion/client.rb', line 76

def get_page_blocks(page_id)
  blocks = []
  cursor = nil

  loop do
    params = { block_id: page_id }
    params[:start_cursor] = cursor if cursor

    response = api_client.block_children(**params)

    blocks.concat(response.results)

    break unless response.has_more
    cursor = response.next_cursor
  end

  blocks
rescue Notion::Api::Errors::NotionError => e
  raise NotionAPIError, "Failed to get page blocks: #{e.message}"
end

#update_page(page_id:, children:, mode:) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/synotion/client.rb', line 40

def update_page(page_id:, children:, mode:)
  case mode
  when UpdateMode::REPLACE
    delete_blocks(page_id)
    append_blocks(page_id, children)
  when UpdateMode::APPEND
    append_blocks(page_id, children)
  else
    raise ArgumentError, "Invalid update mode for update_page: #{mode}"
  end
rescue Notion::Api::Errors::NotionError => e
  raise NotionAPIError, "Failed to update page: #{e.message}"
end

#update_page_properties(page_id:, properties:) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/synotion/client.rb', line 97

def update_page_properties(page_id:, properties:)
  api_client.update_page(
    page_id: page_id,
    properties: properties
  )
rescue Notion::Api::Errors::NotionError => e
  raise NotionAPIError, "Failed to update page properties: #{e.message}"
end