Skip to content

Commit

Permalink
Update resources to return models
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmarlow committed Sep 29, 2021
1 parent 137e472 commit 9bcf642
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 30 deletions.
3 changes: 2 additions & 1 deletion lib/notion-sdk-ruby/operations/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module Search
include RequestClient

def search(body)
post("/v1/search", body.to_json)
response = post("/v1/search", body.to_json)
List.new(response.body)
end
end
end
Expand Down
12 changes: 8 additions & 4 deletions lib/notion-sdk-ruby/resources/blocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@ def children
end

def retrieve(block_id)
get("/v1/blocks/#{block_id}")
response = get("/v1/blocks/#{block_id}")
Block.new(response.body)
end

def update(block_id, body)
patch("/v1/blocks/#{block_id}", body.to_json)
response = patch("/v1/blocks/#{block_id}", body.to_json)
Block.new(response.body)
end
end

class Children
include RequestClient

def list(block_id, query: {})
get("/v1/blocks/#{block_id}/children", query: query)
response = get("/v1/blocks/#{block_id}/children", query: query)
List.new(response.body)
end

def append(block_id, body)
patch("/v1/blocks/#{block_id}/children", body.to_json)
response = patch("/v1/blocks/#{block_id}/children", body.to_json)
Block.new(response.body)
end
end
end
15 changes: 10 additions & 5 deletions lib/notion-sdk-ruby/resources/databases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@ class Databases
include RequestClient

def retrieve(id)
get("/v1/databases/#{id}")
response = get("/v1/databases/#{id}")
Database.new(response.body)
end

# DEPRECATED
def list
warn "DEPRECATED: client.databases.list is deprecated."
get("/v1/databases")
response = get("/v1/databases")
List.new(response.body)
end

def query(id, body)
post("/v1/databases/#{id}/query", body.to_json)
response = post("/v1/databases/#{id}/query", body.to_json)
List.new(response.body)
end

def create(body)
post("/v1/databases", body.to_json)
response = post("/v1/databases", body.to_json)
Database.new(response.body)
end

def update(id, body)
patch("/v1/databases/#{id}", body.to_json)
response = patch("/v1/databases/#{id}", body.to_json)
Database.new(response.body)
end
end
end
9 changes: 6 additions & 3 deletions lib/notion-sdk-ruby/resources/pages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ class Pages
include RequestClient

def retrieve(id)
get("/v1/pages/#{id}")
response = get("/v1/pages/#{id}")
Page.new(response.body)
end

def create(body)
post("/v1/pages", body.to_json)
response = post("/v1/pages", body.to_json)
Page.new(response.body)
end

def update(id, body)
patch("/v1/pages/#{id}", body.to_json)
response = patch("/v1/pages/#{id}", body.to_json)
Page.new(response.body)
end
end
end
6 changes: 4 additions & 2 deletions lib/notion-sdk-ruby/resources/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ class Users
include RequestClient

def list
get("/v1/users")
response = get("/v1/users")
List.new(response.body)
end

def retrieve(id)
get("/v1/users/#{id}")
response = get("/v1/users/#{id}")
User.new(response.body)
end
end
end
7 changes: 6 additions & 1 deletion spec/notion-sdk-ruby/operations/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
end

it "should match fixture response" do
expect(client.search(body).body).to eq(JSON.parse(search_fixture))
items = client.search(body).data.map(&:to_h)
expected = JSON.parse(search_fixture)["results"]

items.each_with_index do |item, i|
expect(item.to_json).to eq(expected[i].to_json)
end
end
end
end
29 changes: 25 additions & 4 deletions spec/notion-sdk-ruby/resources/blocks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@
.to have_been_made.once
end

it "should return an instance of Notion::Block" do
expect(client.blocks.retrieve(block_id)).to be_an_instance_of(Notion::Block)
end

it "should match fixture response" do
expect(client.blocks.retrieve(block_id).body).to eq(JSON.parse(get_block_fixture))
expect(client.blocks.retrieve(block_id)).to be_like_fixture(get_block_fixture)
end
end

Expand All @@ -54,8 +58,12 @@
.to have_been_made.once
end

it "should return an instance of Notion::Block" do
expect(client.blocks.update(block_id, body)).to be_an_instance_of(Notion::Block)
end

it "should match fixture response" do
expect(client.blocks.update(block_id, body).body).to eq(JSON.parse(update_block_fixture))
expect(client.blocks.update(block_id, body)).to be_like_fixture(update_block_fixture)
end
end

Expand All @@ -67,8 +75,17 @@
.to have_been_made.once
end

it "should include block models" do
expect(client.blocks.children.list(block_id).data).to all(be_an_instance_of(Notion::Block))
end

it "should match fixture response" do
expect(client.blocks.children.list(block_id).body).to eq(JSON.parse(get_block_children_fixture))
items = client.blocks.children.list(block_id).data.map(&:to_h)
expected = JSON.parse(get_block_children_fixture)["results"]

items.each_with_index do |item, i|
expect(item.to_json).to eq(expected[i].to_json)
end
end
end

Expand Down Expand Up @@ -100,8 +117,12 @@
.to have_been_made.once
end

it "should return an instance of Notion::Block" do
expect(client.blocks.children.append(block_id, body)).to be_an_instance_of(Notion::Block)
end

it "should match fixture response" do
expect(client.blocks.children.append(block_id, body).body).to eq(JSON.parse(append_block_children_fixture))
expect(client.blocks.children.append(block_id, body)).to be_like_fixture(append_block_children_fixture)
end
end
end
40 changes: 35 additions & 5 deletions spec/notion-sdk-ruby/resources/databases_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@
.to have_been_made.once
end

it "should return an instance of Notion::Database" do
expect(client.databases.retrieve(database_id)).to be_an_instance_of(Notion::Database)
end

it "should match fixture response" do
expect(client.databases.retrieve(database_id).body).to eq(JSON.parse(get_database_fixture))
expect(client.databases.retrieve(database_id)).to be_like_fixture(get_database_fixture)
end
end

Expand All @@ -47,8 +51,17 @@
.to have_been_made.once
end

it "should include database models" do
expect(client.databases.list.data).to all(be_an_instance_of(Notion::Database))
end

it "should match fixture response" do
expect(client.databases.list.body).to eq(JSON.parse(get_databases_fixture))
items = client.databases.list.data.map(&:to_h)
expected = JSON.parse(get_databases_fixture)["results"]

items.each_with_index do |item, i|
expect(item.to_json).to eq(expected[i].to_json)
end
end
end

Expand Down Expand Up @@ -88,8 +101,17 @@
.to have_been_made.once
end

it "should include user models" do
expect(client.databases.query(database_id, body).data).to all(be_an_instance_of(Notion::Page))
end

it "should match fixture response" do
expect(client.databases.query(database_id, body).body).to eq(JSON.parse(query_database_fixture))
items = client.databases.query(database_id, body).data.map(&:to_h)
expected = JSON.parse(query_database_fixture)["results"]

items.each_with_index do |item, i|
expect(item.to_json).to eq(expected[i].to_json)
end
end
end

Expand Down Expand Up @@ -125,8 +147,12 @@
.to have_been_made.once
end

it "should return an instance of Notion::Database" do
expect(client.databases.create(body)).to be_an_instance_of(Notion::Database)
end

it "should match fixture response" do
expect(client.databases.create(body).body).to eq(JSON.parse(create_database_fixture))
expect(client.databases.create(body)).to be_like_fixture(create_database_fixture)
end
end

Expand Down Expand Up @@ -158,8 +184,12 @@
.to have_been_made.once
end

it "should return an instance of Notion::Database" do
expect(client.databases.update(database_id, body)).to be_an_instance_of(Notion::Database)
end

it "should match fixture response" do
expect(client.databases.update(database_id, body).body).to eq(JSON.parse(update_database_fixture))
expect(client.databases.update(database_id, body)).to be_like_fixture(update_database_fixture)
end
end
end
14 changes: 11 additions & 3 deletions spec/notion-sdk-ruby/resources/pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
.to have_been_made.once
end

it "should return an instance of Notion::Page" do
expect(client.pages.retrieve(page_id)).to be_an_instance_of(Notion::Page)
end

it "should match fixture response" do
expect(client.pages.retrieve(page_id).body).to eq(JSON.parse(get_page_fixture))
expect(client.pages.retrieve(page_id)).to be_like_fixture(get_page_fixture)
end
end

Expand All @@ -50,7 +54,7 @@
end

it "should match fixture response" do
expect(client.pages.create(body).body).to eq(JSON.parse(create_page_fixture))
expect(client.pages.create(body)).to be_like_fixture(create_page_fixture)
end
end

Expand All @@ -71,8 +75,12 @@
.to have_been_made.once
end

it "should return an instance of Notion::Page" do
expect(client.pages.update(page_id, body)).to be_an_instance_of(Notion::Page)
end

it "should match fixture response" do
expect(client.pages.update(page_id, body).body).to eq(JSON.parse(update_page_fixture))
expect(client.pages.update(page_id, body)).to be_like_fixture(update_page_fixture)
end
end
end
17 changes: 15 additions & 2 deletions spec/notion-sdk-ruby/resources/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@
.to have_been_made.once
end

it "should include user models" do
expect(client.users.list.data).to all(be_an_instance_of(Notion::User))
end

it "should match fixture response" do
expect(client.users.list.body).to eq(JSON.parse(get_users_fixture))
items = client.users.list.data.map(&:to_h)
expected = JSON.parse(get_users_fixture)["results"]

items.each_with_index do |item, i|
expect(item.to_json).to eq(expected[i].to_json)
end
end
end

Expand All @@ -35,8 +44,12 @@
.to have_been_made.once
end

it "should return an instance of Notion::User" do
expect(client.users.retrieve(user_id)).to be_an_instance_of(Notion::User)
end

it "should match fixture response" do
expect(client.users.retrieve(user_id).body).to eq(JSON.parse(get_user_fixture))
expect(client.users.retrieve(user_id)).to eq(Notion::User.new(JSON.parse(get_user_fixture)))
end
end

Expand Down

0 comments on commit 9bcf642

Please sign in to comment.