notion-sdk-ruby 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,7 +1,12 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
- require "standard/rake"
4
-
5
- RSpec::Core::RakeTask.new(:spec)
6
-
7
- task default: [:spec, :standard]
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "standard/rake"
4
+ require "yard"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ YARD::Rake::YardocTask.new do |t|
9
+ t.files = ["lib/**/*.rb"]
10
+ end
11
+
12
+ task default: [:spec, :standard]
data/bin/console CHANGED
@@ -1,11 +1,11 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "pry"
5
- require 'dotenv/load'
6
-
7
- require "notion-sdk-ruby"
8
-
9
- $client = Notion::Client.new(token: ENV["API_SECRET"])
10
-
11
- Pry.start
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pry"
5
+ require 'dotenv/load'
6
+
7
+ require "notion-sdk-ruby"
8
+
9
+ $client = Notion::Client.new(token: ENV["API_SECRET"])
10
+
11
+ Pry.start
data/bin/setup CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,56 @@
1
+ module Notion
2
+ module Api
3
+ class BlocksMethods
4
+ include RequestClient
5
+
6
+ # @return [Notion::Api::BlocksChildrenMethods]
7
+ def children
8
+ BlocksChildrenMethods.new
9
+ end
10
+
11
+ # Retrieves a Block object using the ID specified.
12
+ # https://developers.notion.com/reference/retrieve-a-block
13
+ # @param [String] id block_id
14
+ # @return [Notion::Block]
15
+ def retrieve(id)
16
+ response = get("/v1/blocks/#{id}")
17
+ Block.new(response.body)
18
+ end
19
+
20
+ # Updates the content for the specified block_id based on the block type.
21
+ # https://developers.notion.com/reference/update-a-block
22
+ # @param [String] id block_id
23
+ # @param [Hash] body
24
+ # @return [Notion::Block]
25
+ def update(id, body)
26
+ response = patch("/v1/blocks/#{id}", body.to_json)
27
+ Block.new(response.body)
28
+ end
29
+ end
30
+
31
+ class BlocksChildrenMethods
32
+ include RequestClient
33
+
34
+ # Returns a paginated array of child block objects contained in the block
35
+ # using the ID specified.
36
+ # https://developers.notion.com/reference/get-block-children
37
+ # @param [String] id block_id
38
+ # @param [Hash] query
39
+ # @return [Array<Notion::Block>]
40
+ def list(id, query = {})
41
+ response = get("/v1/blocks/#{id}/children", query)
42
+ List.new(response.body)
43
+ end
44
+
45
+ # Creates and appends new children blocks to the parent block_id specified.
46
+ # https://developers.notion.com/reference/patch-block-children
47
+ # @param [String] id block_id
48
+ # @param [Hash] body
49
+ # @return [Array<Notion::Block>]
50
+ def append(id, body)
51
+ response = patch("/v1/blocks/#{id}/children", body.to_json)
52
+ List.new(response.body)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,47 @@
1
+ module Notion
2
+ module Api
3
+ class DatabasesMethods
4
+ include RequestClient
5
+
6
+ # Retrieves a Database object using the ID specified.
7
+ # https://developers.notion.com/reference/retrieve-a-database
8
+ # @param [String] id database_id
9
+ # @return [Notion::Database]
10
+ def retrieve(id)
11
+ response = get("/v1/databases/#{id}")
12
+ Database.new(response.body)
13
+ end
14
+
15
+ # Gets a list of Pages contained in the database, filtered and ordered according
16
+ # to the filter conditions and sort criteria provided in the request.
17
+ # https://developers.notion.com/reference/post-database-query
18
+ # @param [String] id database_id
19
+ # @param [Hash] body
20
+ # @return [Array<Notion::Database>]
21
+ def query(id, body)
22
+ response = post("/v1/databases/#{id}/query", body.to_json)
23
+ List.new(response.body)
24
+ end
25
+
26
+ # Creates a database as a subpage in the specified parent page, with the
27
+ # specified properties schema.
28
+ # https://developers.notion.com/reference/create-a-database
29
+ # @param [Hash] body
30
+ # @return [Notion::Database]
31
+ def create(body)
32
+ response = post("/v1/databases", body.to_json)
33
+ Database.new(response.body)
34
+ end
35
+
36
+ # Updates an existing database as specified by the parameters.
37
+ # https://developers.notion.com/reference/update-a-database
38
+ # @param [String] id database_id
39
+ # @param [Hash] body
40
+ # @return [Notion::Database]
41
+ def update(id, body)
42
+ response = patch("/v1/databases/#{id}", body.to_json)
43
+ Database.new(response.body)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,36 @@
1
+ module Notion
2
+ module Api
3
+ class PagesMethods
4
+ include RequestClient
5
+
6
+ # Retrieves a Page object using the ID specified.
7
+ # https://developers.notion.com/reference/retrieve-a-page
8
+ # @param [String] id page_id
9
+ # @return [Notion::Page]
10
+ def retrieve(id)
11
+ response = get("/v1/pages/#{id}")
12
+ Page.new(response.body)
13
+ end
14
+
15
+ # Creates a new page in the specified database or as a child of
16
+ # an existing page.
17
+ # https://developers.notion.com/reference/post-page
18
+ # @param [Hash] body
19
+ # @return [Notion::Page]
20
+ def create(body)
21
+ response = post("/v1/pages", body.to_json)
22
+ Page.new(response.body)
23
+ end
24
+
25
+ # Updates page property values for the specified page.
26
+ # https://developers.notion.com/reference/patch-page
27
+ # @param [String] id page_id
28
+ # @param [Hash] body
29
+ # @return [Notion::Page]
30
+ def update(id, body)
31
+ response = patch("/v1/pages/#{id}", body.to_json)
32
+ Page.new(response.body)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ module Notion
2
+ module Api
3
+ module SearchMethods
4
+ include RequestClient
5
+
6
+ # Searches all original pages, databases, and child pages/databases
7
+ # that are shared with the integration.
8
+ # https://developers.notion.com/reference/post-search
9
+ # @param [Hash] body
10
+ # @return [Array]
11
+ def search(body)
12
+ response = post("/v1/search", body.to_json)
13
+ List.new(response.body)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ module Notion
2
+ module Api
3
+ class UsersMethods
4
+ include RequestClient
5
+
6
+ # Returns a paginated list of Users for the workspace.
7
+ # https://developers.notion.com/reference/get-users
8
+ # @return [Array<Notion::User>]
9
+ def list
10
+ response = get("/v1/users")
11
+ List.new(response.body)
12
+ end
13
+
14
+ # Retrieves a User using the ID specified.
15
+ # https://developers.notion.com/reference/get-user
16
+ # @param [String] id user_id
17
+ # @return [Notion::User]
18
+ def retrieve(id)
19
+ response = get("/v1/users/#{id}")
20
+ User.new(response.body)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,27 +1,30 @@
1
- module Notion
2
- class Client
3
- include RequestClient
4
- include Operations::Search
5
-
6
- def initialize(token:, notion_version: "2021-08-16")
7
- Notion.api_token = token
8
- Notion.notion_version = notion_version
9
- end
10
-
11
- def blocks
12
- Blocks.new
13
- end
14
-
15
- def databases
16
- Databases.new
17
- end
18
-
19
- def pages
20
- Pages.new
21
- end
22
-
23
- def users
24
- Users.new
25
- end
26
- end
27
- end
1
+ module Notion
2
+ class Client
3
+ include Api::SearchMethods
4
+
5
+ def initialize(token:, notion_version: "2022-02-22")
6
+ Notion.api_token = token
7
+ Notion.notion_version = notion_version
8
+ end
9
+
10
+ # @return [Notion::Api::DatabasesMethods]
11
+ def databases
12
+ Api::DatabasesMethods.new
13
+ end
14
+
15
+ # @return [Notion::Api::UsersMethods]
16
+ def users
17
+ Api::UsersMethods.new
18
+ end
19
+
20
+ # @return [Notion::Api::BlocksMethods]
21
+ def blocks
22
+ Api::BlocksMethods.new
23
+ end
24
+
25
+ # @return [Notion::Api::PagesMethods]
26
+ def pages
27
+ Api::PagesMethods.new
28
+ end
29
+ end
30
+ end
@@ -1,5 +1,5 @@
1
- module Notion
2
- class Config
3
- attr_accessor :api_token, :notion_version
4
- end
5
- end
1
+ module Notion
2
+ class Config
3
+ attr_accessor :api_token, :notion_version
4
+ end
5
+ end
@@ -1,52 +1,52 @@
1
- # Sourced from notion-sdk-js:
2
- # https://github.com/makenotion/notion-sdk-js/blob/main/src/errors.ts
3
- module Notion
4
- API_ERROR_CODE = {
5
- unauthorized: "unauthorized",
6
- restricted_resource: "restricted_resource",
7
- object_not_found: "object_not_found",
8
- rate_limited: "rate_limited",
9
- invalid_json: "invalid_json",
10
- invalid_request_url: "invalid_request_url",
11
- invalid_request: "invalid_request",
12
- validation_error: "validation_error",
13
- conflict_error: "conflict_error",
14
- internal_server_error: "internal_server_error",
15
- service_unavailable: "service_unavailable"
16
- }
17
-
18
- class ErrorFactory
19
- def self.create(error = {})
20
- return NotionError.new("Unknown error.") if error["message"].nil?
21
-
22
- if API_ERROR_CODE.value?(error["code"])
23
- APIResponseError.new(error["message"], body: error)
24
- elsif error["request"] && error["response"] && error["timings"]
25
- HTTPResponseError.new(error["message"], body: error)
26
- elsif error["request"] && error["timings"]
27
- RequestTimeoutError.new(error["message"], body: error)
28
- else
29
- NotionError.new(error["message"])
30
- end
31
- end
32
- end
33
-
34
- class NotionError < StandardError
35
- attr_reader :message, :body
36
-
37
- def initialize(message = nil, body: nil)
38
- @message = message
39
- @body = body
40
- end
41
- end
42
-
43
- class RequestTimeoutError < NotionError; end
44
-
45
- class HTTPResponseError < NotionError; end
46
-
47
- class APIResponseError < NotionError
48
- def code
49
- body["code"]
50
- end
51
- end
52
- end
1
+ # Sourced from notion-sdk-js:
2
+ # https://github.com/makenotion/notion-sdk-js/blob/main/src/errors.ts
3
+ module Notion
4
+ API_ERROR_CODE = {
5
+ unauthorized: "unauthorized",
6
+ restricted_resource: "restricted_resource",
7
+ object_not_found: "object_not_found",
8
+ rate_limited: "rate_limited",
9
+ invalid_json: "invalid_json",
10
+ invalid_request_url: "invalid_request_url",
11
+ invalid_request: "invalid_request",
12
+ validation_error: "validation_error",
13
+ conflict_error: "conflict_error",
14
+ internal_server_error: "internal_server_error",
15
+ service_unavailable: "service_unavailable"
16
+ }
17
+
18
+ class ErrorFactory
19
+ def self.create(error = {})
20
+ return NotionError.new("Unknown error.") if error["message"].nil?
21
+
22
+ if API_ERROR_CODE.value?(error["code"])
23
+ APIResponseError.new(error["message"], body: error)
24
+ elsif error["request"] && error["response"] && error["timings"]
25
+ HTTPResponseError.new(error["message"], body: error)
26
+ elsif error["request"] && error["timings"]
27
+ RequestTimeoutError.new(error["message"], body: error)
28
+ else
29
+ NotionError.new(error["message"])
30
+ end
31
+ end
32
+ end
33
+
34
+ class NotionError < StandardError
35
+ attr_reader :message, :body
36
+
37
+ def initialize(message = nil, body: nil)
38
+ @message = message
39
+ @body = body
40
+ end
41
+ end
42
+
43
+ class RequestTimeoutError < NotionError; end
44
+
45
+ class HTTPResponseError < NotionError; end
46
+
47
+ class APIResponseError < NotionError
48
+ def code
49
+ body["code"]
50
+ end
51
+ end
52
+ end
@@ -1,4 +1,7 @@
1
- module Notion
2
- class Block < OpenStruct
3
- end
4
- end
1
+ module Notion
2
+ # A block object represents content within Notion.
3
+ #
4
+ # https://developers.notion.com/reference/block
5
+ class Block < OpenStruct
6
+ end
7
+ end
@@ -1,4 +1,7 @@
1
- module Notion
2
- class Database < OpenStruct
3
- end
4
- end
1
+ module Notion
2
+ # Database objects describe the property schema of a database in Notion.
3
+ #
4
+ # https://developers.notion.com/reference/database
5
+ class Database < OpenStruct
6
+ end
7
+ end
@@ -1,27 +1,49 @@
1
- module Notion
2
- class List
3
- attr_reader :data, :next_cursor, :has_more
4
-
5
- def initialize(response_body)
6
- @data = response_body["results"].map do |d|
7
- get_model(d["object"]).new(d)
8
- end
9
-
10
- @next_cursor = response_body["next_cursor"]
11
- @has_more = response_body["has_more"]
12
- end
13
-
14
- def get_model(object_name)
15
- case object_name
16
- when "block"
17
- Block
18
- when "database"
19
- Database
20
- when "page"
21
- Page
22
- when "user"
23
- User
24
- end
25
- end
26
- end
27
- end
1
+ module Notion
2
+ # The List object is an intermediate object that helps with pagination.
3
+ #
4
+ # https://developers.notion.com/reference/pagination
5
+ class List
6
+ # An array of endpoint-dependent objects
7
+ # @return [Array<Notion::User>]
8
+ # @return [Array<Notion::Database>]
9
+ # @return [Array<Notion::Page>]
10
+ # @return [Array<Notion::Block>]
11
+ attr_reader :data
12
+
13
+ # Used to retrieve the next page of results by passing the value as the
14
+ # start_cursor parameter to the same endpoint.
15
+ # @return [nil] if has_more is true.
16
+ # @return [string] if has_more is false.
17
+ attr_reader :next_cursor
18
+
19
+ # When the response includes the end of the list, false. Otherwise, true.
20
+ # @return [boolean]
21
+ attr_reader :has_more
22
+
23
+ def initialize(response_body)
24
+ @data = response_body["results"].map do |d|
25
+ get_model(d["object"]).new(d)
26
+ end
27
+
28
+ @next_cursor = response_body["next_cursor"]
29
+ @has_more = response_body["has_more"]
30
+ end
31
+
32
+ private
33
+
34
+ def get_model(object_name)
35
+ case object_name
36
+ when "block"
37
+ Block
38
+ when "database"
39
+ Database
40
+ when "page"
41
+ Page
42
+ when "user"
43
+ User
44
+ else
45
+ raise NotionError.new("unimplemented object type")
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,4 +1,7 @@
1
- module Notion
2
- class Page < OpenStruct
3
- end
4
- end
1
+ module Notion
2
+ # The Page object contains the property values of a single Notion page.
3
+ #
4
+ # https://developers.notion.com/reference/page
5
+ class Page < OpenStruct
6
+ end
7
+ end
@@ -1,4 +1,7 @@
1
- module Notion
2
- class User < OpenStruct
3
- end
4
- end
1
+ module Notion
2
+ # The User object represents a user in a Notion workspace.
3
+ #
4
+ # https://developers.notion.com/reference/user
5
+ class User < OpenStruct
6
+ end
7
+ end
@@ -1,45 +1,45 @@
1
- module Notion
2
- module RequestClient
3
- def get(*args)
4
- handle_request(:get, *args)
5
- end
6
-
7
- def post(*args)
8
- handle_request(:post, *args)
9
- end
10
-
11
- def patch(*args)
12
- handle_request(:patch, *args)
13
- end
14
-
15
- def delete(*args)
16
- handle_request(:delete, *args)
17
- end
18
-
19
- private
20
-
21
- def handle_request(method, *args)
22
- faraday_client.public_send(method, *args)
23
- rescue Faraday::ClientError => error
24
- error_details = JSON.parse(error.response[:body])
25
- raise ErrorFactory.create(error_details)
26
- rescue JSON::ParserError => error
27
- raise NotionError.new(error.message)
28
- end
29
-
30
- def faraday_client
31
- @faraday_client ||= Faraday.new(
32
- url: "https://api.notion.com",
33
- headers: {
34
- "Content-Type" => "application/json",
35
- "Notion-Version" => Notion.config.notion_version,
36
- "Authorization" => "Bearer #{Notion.config.api_token}"
37
- }
38
- ) do |f|
39
- f.request :json
40
- f.response :json
41
- f.use Faraday::Response::RaiseError
42
- end
43
- end
44
- end
45
- end
1
+ module Notion
2
+ module RequestClient
3
+ private
4
+
5
+ def get(*args)
6
+ handle_request(:get, *args)
7
+ end
8
+
9
+ def post(*args)
10
+ handle_request(:post, *args)
11
+ end
12
+
13
+ def patch(*args)
14
+ handle_request(:patch, *args)
15
+ end
16
+
17
+ def delete(*args)
18
+ handle_request(:delete, *args)
19
+ end
20
+
21
+ def handle_request(method, *args)
22
+ faraday_client.public_send(method, *args)
23
+ rescue Faraday::ClientError => error
24
+ error_details = JSON.parse(error.response[:body])
25
+ raise ErrorFactory.create(error_details)
26
+ rescue JSON::ParserError => error
27
+ raise NotionError.new(error.message)
28
+ end
29
+
30
+ def faraday_client
31
+ @faraday_client ||= Faraday.new(
32
+ url: "https://api.notion.com",
33
+ headers: {
34
+ "Content-Type" => "application/json",
35
+ "Notion-Version" => Notion.config.notion_version,
36
+ "Authorization" => "Bearer #{Notion.config.api_token}"
37
+ }
38
+ ) do |f|
39
+ f.request :json
40
+ f.response :json
41
+ f.use Faraday::Response::RaiseError
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
- module Notion
2
- VERSION = "0.5.0"
3
- end
1
+ module Notion
2
+ VERSION = "0.6.0"
3
+ end