Class: CollavreNotion::NotionClient
- Inherits:
-
Object
- Object
- CollavreNotion::NotionClient
- Defined in:
- app/services/collavre_notion/notion_client.rb
Constant Summary collapse
- BASE_URL =
"https://api.notion.com/v1"- API_VERSION =
"2022-06-28"
Instance Method Summary collapse
- #append_blocks(page_id, blocks) ⇒ Object
- #create_page(parent_id:, title:, blocks: []) ⇒ Object
- #delete_block(block_id) ⇒ Object
- #get_page(page_id) ⇒ Object
- #get_page_blocks(page_id, start_cursor: nil, page_size: 100) ⇒ Object
- #get_workspace ⇒ Object
-
#initialize(account) ⇒ NotionClient
constructor
A new instance of NotionClient.
- #replace_page_blocks(page_id, blocks) ⇒ Object
- #search_pages(query: nil, start_cursor: nil, page_size: 10) ⇒ Object
- #update_page(page_id, properties: {}, blocks: nil) ⇒ Object
Constructor Details
#initialize(account) ⇒ NotionClient
Returns a new instance of NotionClient.
6 7 8 9 |
# File 'app/services/collavre_notion/notion_client.rb', line 6 def initialize(account) @account = account @token = account.token end |
Instance Method Details
#append_blocks(page_id, blocks) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/services/collavre_notion/notion_client.rb', line 105 def append_blocks(page_id, blocks) # Notion also limits append operations to 100 blocks if blocks.length > 100 Rails.logger.info("NotionClient: Appending #{blocks.length} blocks in batches") aggregated_results = [] blocks.each_slice(100).with_index do |block_batch, index| Rails.logger.info("NotionClient: Appending batch #{index + 1} with #{block_batch.length} blocks") response = patch("blocks/#{format_id(page_id)}/children", { children: block_batch }) if response.is_a?(Hash) batch_results = response.fetch("results", []) aggregated_results.concat(batch_results) if batch_results.any? end end aggregated_results.any? ? { "results" => aggregated_results } : nil else patch("blocks/#{format_id(page_id)}/children", { children: blocks }) end end |
#create_page(parent_id:, title:, blocks: []) ⇒ Object
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 |
# File 'app/services/collavre_notion/notion_client.rb', line 31 def create_page(parent_id:, title:, blocks: []) Rails.logger.info("NotionClient: Creating page with #{blocks.length} blocks") # Notion limits page creation to 100 blocks, so create with minimal content first body = { parent: { page_id: parent_id }, properties: { title: { title: [ { text: { content: title } } ] } }, children: blocks.length > 100 ? [] : blocks } response = post("pages", body) # If we have more than 100 blocks, add them in batches after page creation if blocks.length > 100 Rails.logger.info("NotionClient: Adding #{blocks.length} blocks in batches (100 per batch)") page_id = response["id"] Rails.logger.info("NotionClient: Created page ID: #{page_id}") # Give Notion a moment to fully create the page sleep(1) # Add blocks in batches of 100 blocks.each_slice(100).with_index do |block_batch, index| Rails.logger.info("NotionClient: Adding batch #{index + 1} with #{block_batch.length} blocks") append_blocks(page_id, block_batch) Rails.logger.info("NotionClient: Successfully added batch #{index + 1}") end end response end |
#delete_block(block_id) ⇒ Object
127 128 129 |
# File 'app/services/collavre_notion/notion_client.rb', line 127 def delete_block(block_id) delete("blocks/#{format_id(block_id)}") end |
#get_page(page_id) ⇒ Object
27 28 29 |
# File 'app/services/collavre_notion/notion_client.rb', line 27 def get_page(page_id) get("pages/#{format_id(page_id)}") end |
#get_page_blocks(page_id, start_cursor: nil, page_size: 100) ⇒ Object
78 79 80 81 82 83 |
# File 'app/services/collavre_notion/notion_client.rb', line 78 def get_page_blocks(page_id, start_cursor: nil, page_size: 100) params = { page_size: page_size } params[:start_cursor] = start_cursor if start_cursor.present? get("blocks/#{format_id(page_id)}/children", params) end |
#get_workspace ⇒ Object
131 132 133 |
# File 'app/services/collavre_notion/notion_client.rb', line 131 def get_workspace get("users/me") end |
#replace_page_blocks(page_id, blocks) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/services/collavre_notion/notion_client.rb', line 85 def replace_page_blocks(page_id, blocks) Rails.logger.info("NotionClient: Replacing page blocks with #{blocks.length} blocks") # First, get existing blocks existing_blocks = get_page_blocks(page_id) # Delete existing blocks existing_blocks.dig("results")&.each do |block| delete_block(block["id"]) end # Add new blocks in batches of 100 if blocks.any? blocks.each_slice(100).with_index do |block_batch, index| Rails.logger.info("NotionClient: Adding replacement batch #{index + 1} with #{block_batch.length} blocks") append_blocks(page_id, block_batch) end end end |
#search_pages(query: nil, start_cursor: nil, page_size: 10) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/services/collavre_notion/notion_client.rb', line 11 def search_pages(query: nil, start_cursor: nil, page_size: 10) Rails.logger.info("NotionClient: Searching for pages with query: #{query}, page_size: #{page_size}") body = { filter: { property: "object", value: "page" }, page_size: page_size } body[:query] = query if query.present? body[:start_cursor] = start_cursor if start_cursor.present? Rails.logger.info("NotionClient: Search request body: #{body.to_json}") result = post("search", body) Rails.logger.info("NotionClient: Search returned #{result["results"]&.length || 0} results") result end |
#update_page(page_id, properties: {}, blocks: nil) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'app/services/collavre_notion/notion_client.rb', line 67 def update_page(page_id, properties: {}, blocks: nil) body = { properties: properties } response = patch("pages/#{format_id(page_id)}", body) if blocks.present? replace_page_blocks(page_id, blocks) end response end |