Skip to content

Commit

Permalink
Merge pull request #46 from TABeauchat/allow-page-parent-id-to-also-b…
Browse files Browse the repository at this point in the history
…e-page

Added page_id as a legal parent when creating a page.  Updated NOTION…
  • Loading branch information
phacks authored Feb 26, 2023
2 parents 160feb2 + 03c828f commit 74321e4
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 1 deletion.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ If the parent is a page, the only valid property is `title`.

The new page may include page content, described as [blocks](https://developers.notion.com/reference-link/block) in the children parameter.

The following example creates a new page within the specified database:

```ruby
properties = {
'Name': {
Expand Down Expand Up @@ -346,6 +348,54 @@ client.create_page(
)
```

This example creates a new page as a child of an existing page.

```ruby
properties = {
title: [
{
"type": "text",
"text": {
"content": "My favorite food",
"link": null
}
}
]
}
children = [
{
'object': 'block',
'type': 'heading_2',
'heading_2': {
'rich_text': [{
'type': 'text',
'text': { 'content': 'Lacinato kale' }
}]
}
},
{
'object': 'block',
'type': 'paragraph',
'paragraph': {
'rich_text': [
{
'type': 'text',
'text': {
'content': 'Lacinato kale is a variety of kale with a long tradition in Italian cuisine, especially that of Tuscany. It is also known as Tuscan kale, Italian kale, dinosaur kale, kale, flat back kale, palm tree kale, or black Tuscan palm.',
'link': { 'url': 'https://en.wikipedia.org/wiki/Lacinato_kale' }
}
}
]
}
}
]
client.create_page(
parent: { page_id: 'feb1cdfaab6a43cea4ecbc9e8de63ef7'},
properties: properties,
children: children
)
```

See the full endpoint documentation on [Notion Developers](https://developers.notion.com/reference/post-page).

#### Update page
Expand Down
5 changes: 4 additions & 1 deletion lib/notion/api/endpoints/pages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ def page(options = {})
# @option options [Object] :children
# An optional array of Block objects representing the Page’s content
def create_page(options = {})
throw ArgumentError.new('Required argument :parent.database_id missing') if options.dig(:parent, :database_id).nil?
if options.dig(:parent, :database_id).nil? && options.dig(:parent, :page_id).nil?
throw ArgumentError.new('Required argument :parent.database_id or :parent.page_id required')
end

post("pages", options)
end

Expand Down
128 changes: 128 additions & 0 deletions spec/fixtures/notion/create_page_with_parent_page.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions spec/notion/api/endpoints/pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,47 @@
expect(response.properties.Name.title.first.plain_text).to eql 'Another Notion page'
end

context 'when creating under parent page' do
let(:parent_page) { '0593a719-ff2e-44aa-a14a-2bf169429284' }
let(:properties) do
{
"title": [
{
"text": {
"content": 'Another Notion page'
}
}
]
}
end
let(:children) do
[
{
"object": 'block',
"type": 'heading_2',
"heading_2": {
rich_text: [
{
type: 'text',
text: { content: 'My Heading 2' }
}
]
}
}
]
end

it 'creates', vcr: { cassette_name: 'create_page_with_parent_page' } do
response = client.create_page(
parent: { page_id: parent_page },
properties: properties,
children: children
)
expect(response.parent.page_id).to eql parent_page
expect(response.properties.title.title.first.plain_text).to eql 'Another Notion page'
end
end

it 'updates', vcr: { cassette_name: 'update_page' } do
properties = {
"Name": [
Expand Down

0 comments on commit 74321e4

Please sign in to comment.