diff --git a/README.md b/README.md index 65179c3..1a7a904 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,6 @@ And then execute: $ bundle install -Or install it yourself as: - - $ gem install notion-sdk-ruby - ## Usage Initialize `Notion::Client` with your app's [integration secret](https://developers.notion.com/docs/getting-started#create-a-new-integration). @@ -25,29 +21,34 @@ Initialize `Notion::Client` with your app's [integration secret](https://develop ```rb require "notion-sdk-ruby" client = Notion::Client.new(token: ENV["NOTION_API_SECRET"]) - -# get users -client.get_users ``` +## API reference + ### Databases -#### #get_database +#### databases#retrieve + +[API reference](https://developers.notion.com/reference/get-database) ```rb -client.get_database("668d797c-76fa-4934-9b05-ad288df2d136") +client.databases.retrieve("668d797c-76fa-4934-9b05-ad288df2d136") ``` -#### #get_databases +#### databases#list + +[API reference](https://developers.notion.com/reference/get-databases) ```rb -client.get_databases +client.databases.list ``` -#### #query_database +#### databases#query + +[API reference](https://developers.notion.com/reference/post-database-query) ```rb -client.query_database("668d797c-76fa-4934-9b05-ad288df2d136", { +client.databases.query("668d797c-76fa-4934-9b05-ad288df2d136", { "filter": { "or": [ { @@ -75,16 +76,20 @@ client.query_database("668d797c-76fa-4934-9b05-ad288df2d136", { ### Pages -#### #get_page +#### pages#retrieve + +[API reference](https://developers.notion.com/reference/get-page) ```rb -client.get_page("b55c9c91-384d-452b-81db-d1ef79372b75") +client.pages.retrieve("b55c9c91-384d-452b-81db-d1ef79372b75") ``` -#### #create_page +#### pages#create + +[API reference](https://developers.notion.com/reference/post-page) ```rb -client.create_page({ +client.pages.create({ "parent": { "database_id": "48f8fee9cd794180bc2fec0398253067" }, "properties": { "Name": { @@ -116,10 +121,12 @@ client.create_page({ }) ``` -#### #update_page +#### pages#update + +[API reference](https://developers.notion.com/reference/patch-page) ```rb -client.update_page("b55c9c91-384d-452b-81db-d1ef79372b75", { +client.pages.update("b55c9c91-384d-452b-81db-d1ef79372b75", { "properties": { "In stock": { "checkbox": true } } @@ -128,18 +135,22 @@ client.update_page("b55c9c91-384d-452b-81db-d1ef79372b75", { ### Blocks -#### #get_block_children +#### blocks#children#list + +[API reference](https://developers.notion.com/reference/get-block-children) ```rb -client.get_block_children("b55c9c91-384d-452b-81db-d1ef79372b75", { +client.blocks.children.list("b55c9c91-384d-452b-81db-d1ef79372b75", { page_size: 100 }) ``` -#### #append_block_children +#### blocks#children#append + +[API reference](https://developers.notion.com/reference/patch-block-children) ```rb -client.append_block_children("b54c9c91-384d-452b-81db-d1ef79372b75", { +client.blocks.children.append("b54c9c91-384d-452b-81db-d1ef79372b75", { "children": [ { "object": "block", @@ -169,20 +180,26 @@ client.append_block_children("b54c9c91-384d-452b-81db-d1ef79372b75", { ### Users -#### #get_user +#### users#retrieve + +[API reference](https://developers.notion.com/reference/get-user) ```rb -client.get_user("d40e767c-d7af-4b18-a86d-55c61f1e39a4") +client.users.retrieve("d40e767c-d7af-4b18-a86d-55c61f1e39a4") ``` -#### #get_users +#### users#list + +[API reference](https://developers.notion.com/reference/get-users) ```rb -client.get_users +client.users.list ``` ### Search +[API reference](https://developers.notion.com/reference/post-search) + #### #search ```rb diff --git a/bin/console b/bin/console index f13f35c..5de9d96 100644 --- a/bin/console +++ b/bin/console @@ -7,5 +7,6 @@ require 'dotenv/load' require "notion-sdk-ruby" $client = Notion::Client.new(token: ENV["API_SECRET"]) +$client2 = Notion::Client.new(token: ENV["API_SECRET"]) Pry.start diff --git a/lib/notion-sdk-ruby.rb b/lib/notion-sdk-ruby.rb index 06e035c..ed1183f 100644 --- a/lib/notion-sdk-ruby.rb +++ b/lib/notion-sdk-ruby.rb @@ -1,13 +1,26 @@ require "httparty" +require "forwardable" + require "notion-sdk-ruby/version" + +require "notion-sdk-ruby/config" require "notion-sdk-ruby/resources/blocks" require "notion-sdk-ruby/resources/databases" require "notion-sdk-ruby/resources/pages" -require "notion-sdk-ruby/resources/search" require "notion-sdk-ruby/resources/users" -require "notion-sdk-ruby/resources" +require "notion-sdk-ruby/operations/search" require "notion-sdk-ruby/error" +require "notion-sdk-ruby/request_client" require "notion-sdk-ruby/client" module Notion + @config = Config.new + + class << self + extend Forwardable + + attr_reader :config + + def_delegators :@config, :api_token, :api_token= + end end diff --git a/lib/notion-sdk-ruby/client.rb b/lib/notion-sdk-ruby/client.rb index 60c722d..023c4c4 100644 --- a/lib/notion-sdk-ruby/client.rb +++ b/lib/notion-sdk-ruby/client.rb @@ -1,48 +1,25 @@ module Notion class Client - include HTTParty - include Resources - - base_uri "https://api.notion.com" - headers "Content-Type": "application/json" + include Operations::Search def initialize(token:) - self.class.headers({Authorization: "Bearer #{token}"}) - end - - private - - def get(*args, &block) - response = self.class.get(*args, &block) - raise_on_failure(response) - end - - def post(*args, &block) - response = self.class.post(*args, &block) - raise_on_failure(response) + Notion.api_token = token end - def patch(*args, &block) - response = self.class.patch(*args, &block) - raise_on_failure(response) + def blocks + Blocks.new end - def put(*args, &block) - response = self.class.put(*args, &block) - raise_on_failure(response) + def databases + Databases.new end - def delete(*args, &block) - response = self.class.delete(*args, &block) - raise_on_failure(response) + def pages + Pages.new end - def raise_on_failure(response) - if response.success? - response - else - raise ErrorFactory.create(response) - end + def users + Users.new end end end diff --git a/lib/notion-sdk-ruby/config.rb b/lib/notion-sdk-ruby/config.rb new file mode 100644 index 0000000..3ed8b68 --- /dev/null +++ b/lib/notion-sdk-ruby/config.rb @@ -0,0 +1,5 @@ +module Notion + class Config + attr_accessor :api_token + end +end diff --git a/lib/notion-sdk-ruby/error.rb b/lib/notion-sdk-ruby/error.rb index 879211a..86a2f4d 100644 --- a/lib/notion-sdk-ruby/error.rb +++ b/lib/notion-sdk-ruby/error.rb @@ -16,8 +16,8 @@ module Notion } class ErrorFactory - def self.create(error = nil) - return NotionError.new("Unknown error.") if error.nil? || error["message"].nil? + def self.create(error = {}) + return NotionError.new("Unknown error.") if error["message"].nil? if API_ERROR_CODE.value?(error["code"]) APIResponseError.new(error["message"], body: error) diff --git a/lib/notion-sdk-ruby/operations/search.rb b/lib/notion-sdk-ruby/operations/search.rb new file mode 100644 index 0000000..c84b9f1 --- /dev/null +++ b/lib/notion-sdk-ruby/operations/search.rb @@ -0,0 +1,9 @@ +module Notion + module Operations + module Search + def search(body) + RequestClient.active_client.post("/v1/search", body: body.to_json) + end + end + end +end diff --git a/lib/notion-sdk-ruby/request_client.rb b/lib/notion-sdk-ruby/request_client.rb new file mode 100644 index 0000000..e95b0aa --- /dev/null +++ b/lib/notion-sdk-ruby/request_client.rb @@ -0,0 +1,49 @@ +module Notion + class RequestClient + include HTTParty + + base_uri "https://api.notion.com" + headers "Content-Type": "application/json" + + def self.active_client + RequestClient.new(Notion.config) + end + + def initialize(config) + self.class.headers Authorization: "Bearer #{config.api_token}" + end + + def get(*args, &block) + response = self.class.get(*args, &block) + raise_on_failure(response) + end + + def post(*args, &block) + response = self.class.post(*args, &block) + raise_on_failure(response) + end + + def patch(*args, &block) + response = self.class.patch(*args, &block) + raise_on_failure(response) + end + + def put(*args, &block) + response = self.class.put(*args, &block) + raise_on_failure(response) + end + + def delete(*args, &block) + response = self.class.delete(*args, &block) + raise_on_failure(response) + end + + def raise_on_failure(response) + if response.success? + response + else + raise ErrorFactory.create(response) + end + end + end +end diff --git a/lib/notion-sdk-ruby/resources.rb b/lib/notion-sdk-ruby/resources.rb deleted file mode 100644 index 27abcec..0000000 --- a/lib/notion-sdk-ruby/resources.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Notion - module Resources - include Databases - include Pages - include Blocks - include Users - include Search - end -end diff --git a/lib/notion-sdk-ruby/resources/blocks.rb b/lib/notion-sdk-ruby/resources/blocks.rb index edc3a24..f557d99 100644 --- a/lib/notion-sdk-ruby/resources/blocks.rb +++ b/lib/notion-sdk-ruby/resources/blocks.rb @@ -1,11 +1,17 @@ module Notion - module Blocks - def get_block_children(id, params: {}) - get("/v1/blocks/#{id}/children", query: params) + class Blocks + def children + Children.new + end + end + + class Children + def list(block_id, query: {}) + RequestClient.active_client.get("/v1/blocks/#{block_id}/children", query: query) end - def append_block_children(id, body) - patch("/v1/blocks/#{id}/children", body: body.to_json) + def append(block_id, body) + RequestClient.active_client.patch("/v1/blocks/#{block_id}/children", body: body.to_json) end end end diff --git a/lib/notion-sdk-ruby/resources/databases.rb b/lib/notion-sdk-ruby/resources/databases.rb index 9d5859e..2d97638 100644 --- a/lib/notion-sdk-ruby/resources/databases.rb +++ b/lib/notion-sdk-ruby/resources/databases.rb @@ -1,15 +1,15 @@ module Notion - module Databases - def get_database(id) - get("/v1/databases/#{id}") + class Databases + def retrieve(id) + RequestClient.active_client.get("/v1/databases/#{id}") end - def get_databases - get("/v1/databases") + def list + RequestClient.active_client.get("/v1/databases") end - def query_database(id, body) - post("/v1/databases/#{id}/query", body: body.to_json) + def query(id, body) + RequestClient.active_client.post("/v1/databases/#{id}/query", body: body.to_json) end end end diff --git a/lib/notion-sdk-ruby/resources/pages.rb b/lib/notion-sdk-ruby/resources/pages.rb index 75de322..347a144 100644 --- a/lib/notion-sdk-ruby/resources/pages.rb +++ b/lib/notion-sdk-ruby/resources/pages.rb @@ -1,15 +1,15 @@ module Notion - module Pages - def get_page(id) - get("/v1/pages/#{id}") + class Pages + def retrieve(id) + RequestClient.active_client.get("/v1/pages/#{id}") end - def create_page(body) - post("/v1/pages", body: body.to_json) + def create(body) + RequestClient.active_client.post("/v1/pages", body: body.to_json) end - def update_page(id, body) - patch("/v1/pages/#{id}", body: body.to_json) + def update(id, body) + RequestClient.active_client.patch("/v1/pages/#{id}", body: body.to_json) end end end diff --git a/lib/notion-sdk-ruby/resources/search.rb b/lib/notion-sdk-ruby/resources/search.rb deleted file mode 100644 index 6cd393f..0000000 --- a/lib/notion-sdk-ruby/resources/search.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Notion - module Search - def search(body) - post("/v1/search", body: body.to_json) - end - end -end diff --git a/lib/notion-sdk-ruby/resources/users.rb b/lib/notion-sdk-ruby/resources/users.rb index 32783a9..dd5ae03 100644 --- a/lib/notion-sdk-ruby/resources/users.rb +++ b/lib/notion-sdk-ruby/resources/users.rb @@ -1,11 +1,11 @@ module Notion - module Users - def get_users - get("/v1/users") + class Users + def list + RequestClient.active_client.get("/v1/users") end - def get_user(id) - get("/v1/users/#{id}") + def retrieve(id) + RequestClient.active_client.get("/v1/users/#{id}") end end end diff --git a/spec/notion-sdk-ruby/client_spec.rb b/spec/notion-sdk-ruby/client_spec.rb deleted file mode 100644 index a267613..0000000 --- a/spec/notion-sdk-ruby/client_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -RSpec.describe Notion::Client do - let(:access_token) { "foo-1234" } - - subject(:client) { described_class.new(token: access_token) } - - it "should set BASE_URI to api.notion.com" do - expect(client.class.base_uri).to eq("https://api.notion.com") - end - - it "should add headers" do - expect(client.class.headers).to eq({ - Authorization: "Bearer #{access_token}", - "Content-Type": "application/json" - }) - end -end diff --git a/spec/notion-sdk-ruby/error_spec.rb b/spec/notion-sdk-ruby/error_spec.rb new file mode 100644 index 0000000..898f3a5 --- /dev/null +++ b/spec/notion-sdk-ruby/error_spec.rb @@ -0,0 +1,91 @@ +RSpec.describe Notion::ErrorFactory do + subject(:factory) { described_class } + + describe "#create" do + describe "empty error" do + it "should return Unknown Error" do + result = factory.create + expect(result).to be_a Notion::NotionError + expect(result.message).to eq "Unknown error." + end + end + + describe "code inside API_ERROR_CODE" do + let(:error) do + { + "code" => Notion::API_ERROR_CODE[:object_not_found], + "message" => "object not found, yo" + } + end + + it "should return APIResponseError" do + result = factory.create(error) + expect(result).to be_a Notion::APIResponseError + expect(result.message).to eq error["message"] + expect(result.code).to eq error["code"] + end + end + + describe "code NOT inside API_ERROR_CODE" do + let(:error) do + { + "code" => "some_random_code", + "message" => "some random error message not handled in library" + } + end + + it "should return APIResponseError" do + result = factory.create(error) + expect(result).to be_a Notion::NotionError + expect(result.message).to eq error["message"] + end + end + + describe "error has request, response, and timings" do + let(:error) do + { + "request" => "request info", + "response" => "response info", + "timings" => "timings info", + "message" => "HTTP error!" + } + end + + it "should return APIResponseError" do + result = factory.create(error) + expect(result).to be_a Notion::HTTPResponseError + expect(result.message).to eq error["message"] + end + end + + describe "error has request, timings, NOT response" do + let(:error) do + { + "request" => "request info", + "timings" => "timings info", + "message" => "Timeout!" + } + end + + it "should return APIResponseError" do + result = factory.create(error) + expect(result).to be_a Notion::RequestTimeoutError + expect(result.message).to eq error["message"] + end + end + + describe "error only has message" do + let(:error) do + { + "message" => "very descriptive error message" + } + end + + it "should return APIResponseError" do + result = factory.create(error) + expect(result).to be_a Notion::NotionError + expect(result.message).to eq error["message"] + end + end + end +end diff --git a/spec/notion-sdk-ruby/endpoints/search_spec.rb b/spec/notion-sdk-ruby/operations/search_spec.rb similarity index 100% rename from spec/notion-sdk-ruby/endpoints/search_spec.rb rename to spec/notion-sdk-ruby/operations/search_spec.rb diff --git a/spec/notion-sdk-ruby/request_client_spec.rb b/spec/notion-sdk-ruby/request_client_spec.rb new file mode 100644 index 0000000..c62ea16 --- /dev/null +++ b/spec/notion-sdk-ruby/request_client_spec.rb @@ -0,0 +1,50 @@ +RSpec.describe Notion::RequestClient do + let(:access_token) { "foo-1234" } + let(:config) do + OpenStruct.new({ + api_token: access_token + }) + end + + subject(:client) { described_class.new(config) } + + it "should set BASE_URI to api.notion.com" do + expect(client.class.base_uri).to eq("https://api.notion.com") + end + + it "should add headers" do + expect(client.class.headers).to eq({ + Authorization: "Bearer #{access_token}", + "Content-Type": "application/json" + }) + end + + describe "#raise_on_failure" do + describe "response#success? is true" do + let(:response) do + OpenStruct.new({ + success?: true, + data: {type: "foo-bar"} + }) + end + + it "should return response" do + expect(client.raise_on_failure(response)).to eq(response) + end + end + + describe "response#success? is false" do + let(:response) do + OpenStruct.new({ + success?: false + }) + end + + before { allow(Notion::ErrorFactory).to receive(:create).and_return(Notion::NotionError.new) } + + it "should raise an error created from ErrorFactory" do + expect { client.raise_on_failure(response) }.to raise_error(Notion::NotionError) + end + end + end +end diff --git a/spec/notion-sdk-ruby/endpoints/blocks_spec.rb b/spec/notion-sdk-ruby/resources/blocks_spec.rb similarity index 80% rename from spec/notion-sdk-ruby/endpoints/blocks_spec.rb rename to spec/notion-sdk-ruby/resources/blocks_spec.rb index 35729b0..36a7ae1 100644 --- a/spec/notion-sdk-ruby/endpoints/blocks_spec.rb +++ b/spec/notion-sdk-ruby/resources/blocks_spec.rb @@ -14,20 +14,20 @@ .to_return(body: append_block_children_fixture) end - describe "#get_block_children" do + describe "blocks#children#list" do it "should call GET api.notion /blocks/{id}/children" do - client.get_block_children(block_id) + client.blocks.children.list(block_id) expect(a_request(:get, "https://api.notion.com/v1/blocks/#{block_id}/children")) .to have_been_made.once end it "should match fixture response" do - expect(client.get_block_children(block_id).parsed_response).to eq(get_block_children_fixture) + expect(client.blocks.children.list(block_id).parsed_response).to eq(get_block_children_fixture) end end - describe "#append_block_children" do + describe "blocks#children#append" do let(:child) do { object: "block", @@ -48,7 +48,7 @@ let(:body) { {children: [child]} } it "should call PATCH api.notion /blocks/{id}/children" do - client.append_block_children(block_id, body) + client.blocks.children.append(block_id, body) expect(a_request(:patch, "https://api.notion.com/v1/blocks/#{block_id}/children") .with(body: body, headers: {'Content-Type': "application/json", Authorization: "Bearer #{access_token}"})) @@ -56,7 +56,7 @@ end it "should match fixture response" do - expect(client.append_block_children(block_id, body).parsed_response).to eq(append_block_children_fixture) + expect(client.blocks.children.append(block_id, body).parsed_response).to eq(append_block_children_fixture) end end end diff --git a/spec/notion-sdk-ruby/endpoints/databases_spec.rb b/spec/notion-sdk-ruby/resources/databases_spec.rb similarity index 80% rename from spec/notion-sdk-ruby/endpoints/databases_spec.rb rename to spec/notion-sdk-ruby/resources/databases_spec.rb index 9eb3ac4..570cfaf 100644 --- a/spec/notion-sdk-ruby/endpoints/databases_spec.rb +++ b/spec/notion-sdk-ruby/resources/databases_spec.rb @@ -18,33 +18,33 @@ .to_return(body: query_database_fixture) end - describe "#get_database" do + describe "databases#retrieve" do it "should call GET api.notion /databases" do - client.get_database(database_id) + client.databases.retrieve(database_id) expect(a_request(:get, "https://api.notion.com/v1/databases/#{database_id}")) .to have_been_made.once end it "should match fixture response" do - expect(client.get_database(database_id).parsed_response).to eq(get_database_fixture) + expect(client.databases.retrieve(database_id).parsed_response).to eq(get_database_fixture) end end - describe "#get_databases" do + describe "databases#list" do it "should call GET api.notion /databases" do - client.get_databases + client.databases.list expect(a_request(:get, "https://api.notion.com/v1/databases")) .to have_been_made.once end it "should match fixture response" do - expect(client.get_databases.parsed_response).to eq(get_databases_fixture) + expect(client.databases.list.parsed_response).to eq(get_databases_fixture) end end - describe "#query_database" do + describe "databases#query" do let(:body) do { filter: { @@ -73,7 +73,7 @@ end it "should call POST api.notion /databases/{id}/query" do - client.query_database(database_id, body) + client.databases.query(database_id, body) expect(a_request(:post, "https://api.notion.com/v1/databases/#{database_id}/query") .with(body: body)) @@ -81,7 +81,7 @@ end it "should match fixture response" do - expect(client.query_database(database_id, body).parsed_response).to eq(query_database_fixture) + expect(client.databases.query(database_id, body).parsed_response).to eq(query_database_fixture) end end end diff --git a/spec/notion-sdk-ruby/endpoints/pages_spec.rb b/spec/notion-sdk-ruby/resources/pages_spec.rb similarity index 78% rename from spec/notion-sdk-ruby/endpoints/pages_spec.rb rename to spec/notion-sdk-ruby/resources/pages_spec.rb index be76385..ec68c70 100644 --- a/spec/notion-sdk-ruby/endpoints/pages_spec.rb +++ b/spec/notion-sdk-ruby/resources/pages_spec.rb @@ -18,20 +18,20 @@ .to_return(body: update_page_fixture) end - describe "#get_page" do + describe "pages#retrieve" do it "should call GET api.notion /pages/{id}" do - client.get_page(page_id) + client.pages.retrieve(page_id) expect(a_request(:get, "https://api.notion.com/v1/pages/#{page_id}")) .to have_been_made.once end it "should match fixture response" do - expect(client.get_page(page_id).parsed_response).to eq(get_page_fixture) + expect(client.pages.retrieve(page_id).parsed_response).to eq(get_page_fixture) end end - describe "#create_page" do + describe "pages#create" do let(:body) do { parent: { @@ -42,7 +42,7 @@ end it "should call POST api.notion /pages/" do - client.create_page(body) + client.pages.create(body) expect(a_request(:post, "https://api.notion.com/v1/pages") .with(body: body)) @@ -50,11 +50,11 @@ end it "should match fixture response" do - expect(client.create_page(body).parsed_response).to eq(create_page_fixture) + expect(client.pages.create(body).parsed_response).to eq(create_page_fixture) end end - describe "#update_page" do + describe "pages#update" do let(:body) do { properties: { @@ -64,7 +64,7 @@ end it "should call PATCH api.notion /pages/{id}" do - client.update_page(page_id, body) + client.pages.update(page_id, body) expect(a_request(:patch, "https://api.notion.com/v1/pages/#{page_id}") .with(body: body)) @@ -72,7 +72,7 @@ end it "should match fixture response" do - expect(client.update_page(page_id, body).parsed_response).to eq(update_page_fixture) + expect(client.pages.update(page_id, body).parsed_response).to eq(update_page_fixture) end end end diff --git a/spec/notion-sdk-ruby/endpoints/users_spec.rb b/spec/notion-sdk-ruby/resources/users_spec.rb similarity index 54% rename from spec/notion-sdk-ruby/endpoints/users_spec.rb rename to spec/notion-sdk-ruby/resources/users_spec.rb index 5529325..9404d41 100644 --- a/spec/notion-sdk-ruby/endpoints/users_spec.rb +++ b/spec/notion-sdk-ruby/resources/users_spec.rb @@ -5,7 +5,6 @@ let(:user_id) { "abcd-1234" } let(:get_users_fixture) { load_fixture("users/200_get_users") } let(:get_user_fixture) { load_fixture("users/200_get_user") } - let(:get_user_failure_fixture) { load_fixture("users/404_get_user") } before do stub_request(:get, "https://api.notion.com/v1/users") @@ -17,43 +16,46 @@ describe "#get_users" do it "should call api.notion /users/" do - client.get_users + client.users.list expect(a_request(:get, "https://api.notion.com/v1/users")) .to have_been_made.once end it "should match fixture response" do - expect(client.get_users.parsed_response).to eq(get_users_fixture) + expect(client.users.list.parsed_response).to eq(get_users_fixture) end end describe "#get_user" do it "should call api.notion /users/{id}" do - client.get_user(user_id) + client.users.retrieve(user_id) expect(a_request(:get, "https://api.notion.com/v1/users/#{user_id}")) .to have_been_made.once end it "should match fixture response" do - expect(client.get_user(user_id).parsed_response).to eq(get_user_fixture) + expect(client.users.retrieve(user_id).parsed_response).to eq(get_user_fixture) + end + end + + describe "user_id not found" do + let(:user_id) { "1234" } # match fixture + let(:get_user_failure_fixture) { load_fixture("users/404_get_user") } + + before do + stub_request(:get, "https://api.notion.com/v1/users/#{user_id}") + .to_return(body: get_user_failure_fixture, status: 404, headers: { + 'Content-Type': "application/json" + }) end - describe "user_id not found" do - before do - stub_request(:get, "https://api.notion.com/v1/users/#{user_id}") - .to_return(body: get_user_failure_fixture, status: 404, headers: { - 'Content-Type': "application/json" - }) - end - - it "should raise Notion::APIResponseError" do - expect { - client.get_user(user_id) - }.to raise_error(an_instance_of(Notion::APIResponseError) - .and(having_attributes(message: "Could not find user with ID: 1234."))) - end + it "should raise Notion::APIResponseError" do + expect { + client.users.retrieve(user_id) + }.to raise_error(an_instance_of(Notion::APIResponseError) + .and(having_attributes(message: "Could not find user with ID: #{user_id}."))) end end end