notion-sdk-ruby 0.4.1 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,26 +1,30 @@
1
- module Notion
2
- class Client
3
- include Operations::Search
4
-
5
- def initialize(token:, notion_version: "2021-08-16")
6
- Notion.api_token = token
7
- Notion.notion_version = notion_version
8
- end
9
-
10
- def blocks
11
- Blocks.new
12
- end
13
-
14
- def databases
15
- Databases.new
16
- end
17
-
18
- def pages
19
- Pages.new
20
- end
21
-
22
- def users
23
- Users.new
24
- end
25
- end
26
- 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
@@ -0,0 +1,7 @@
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
@@ -0,0 +1,7 @@
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
@@ -0,0 +1,49 @@
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
@@ -0,0 +1,7 @@
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
@@ -0,0 +1,7 @@
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,50 +1,45 @@
1
- module Notion
2
- class RequestClient
3
- include HTTParty
4
-
5
- base_uri "https://api.notion.com"
6
- headers "Content-Type": "application/json"
7
-
8
- def self.active_client
9
- RequestClient.new(Notion.config)
10
- end
11
-
12
- def initialize(config)
13
- self.class.headers Authorization: "Bearer #{config.api_token}"
14
- self.class.headers "Notion-Version": config.notion_version
15
- end
16
-
17
- def get(*args, &block)
18
- response = self.class.get(*args, &block)
19
- raise_on_failure(response)
20
- end
21
-
22
- def post(*args, &block)
23
- response = self.class.post(*args, &block)
24
- raise_on_failure(response)
25
- end
26
-
27
- def patch(*args, &block)
28
- response = self.class.patch(*args, &block)
29
- raise_on_failure(response)
30
- end
31
-
32
- def put(*args, &block)
33
- response = self.class.put(*args, &block)
34
- raise_on_failure(response)
35
- end
36
-
37
- def delete(*args, &block)
38
- response = self.class.delete(*args, &block)
39
- raise_on_failure(response)
40
- end
41
-
42
- def raise_on_failure(response)
43
- if response.success?
44
- response
45
- else
46
- raise ErrorFactory.create(response)
47
- end
48
- end
49
- end
50
- 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.4.1"
3
- end
1
+ module Notion
2
+ VERSION = "0.6.1"
3
+ end
@@ -1,27 +1,37 @@
1
- require "httparty"
2
- require "forwardable"
3
-
4
- require "notion-sdk-ruby/version"
5
-
6
- require "notion-sdk-ruby/config"
7
- require "notion-sdk-ruby/resources/blocks"
8
- require "notion-sdk-ruby/resources/databases"
9
- require "notion-sdk-ruby/resources/pages"
10
- require "notion-sdk-ruby/resources/users"
11
- require "notion-sdk-ruby/operations/search"
12
- require "notion-sdk-ruby/error"
13
- require "notion-sdk-ruby/request_client"
14
- require "notion-sdk-ruby/client"
15
-
16
- module Notion
17
- @config = Config.new
18
-
19
- class << self
20
- extend Forwardable
21
-
22
- attr_reader :config
23
-
24
- def_delegators :@config, :api_token, :api_token=
25
- def_delegators :@config, :notion_version, :notion_version=
26
- end
27
- end
1
+ require "json"
2
+ require "faraday"
3
+ require "faraday_middleware"
4
+ require "forwardable"
5
+ require "ostruct"
6
+
7
+ require "notion-sdk-ruby/version"
8
+ require "notion-sdk-ruby/config"
9
+ require "notion-sdk-ruby/request_client"
10
+ require "notion-sdk-ruby/error"
11
+
12
+ require "notion-sdk-ruby/models/user"
13
+ require "notion-sdk-ruby/models/list"
14
+ require "notion-sdk-ruby/models/block"
15
+ require "notion-sdk-ruby/models/database"
16
+ require "notion-sdk-ruby/models/page"
17
+
18
+ require "notion-sdk-ruby/api/blocks"
19
+ require "notion-sdk-ruby/api/databases"
20
+ require "notion-sdk-ruby/api/pages"
21
+ require "notion-sdk-ruby/api/users"
22
+ require "notion-sdk-ruby/api/search"
23
+
24
+ require "notion-sdk-ruby/client"
25
+
26
+ module Notion
27
+ @config = Config.new
28
+
29
+ class << self
30
+ extend Forwardable
31
+
32
+ attr_reader :config
33
+
34
+ def_delegators :@config, :api_token, :api_token=
35
+ def_delegators :@config, :notion_version, :notion_version=
36
+ end
37
+ end
@@ -1,37 +1,41 @@
1
- lib = File.expand_path("../lib", __FILE__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require_relative "lib/notion-sdk-ruby/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "notion-sdk-ruby"
7
- spec.version = Notion::VERSION
8
- spec.authors = ["Graham Marlow"]
9
- spec.email = ["mgmarlow@hey.com"]
10
-
11
- spec.summary = "Notion SDK"
12
- spec.homepage = "https://github.com/mgmarlow/notion-sdk-ruby"
13
- spec.license = "MIT"
14
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
15
-
16
- spec.metadata["homepage_uri"] = spec.homepage
17
- spec.metadata["source_code_uri"] = "https://github.com/mgmarlow/notion-sdk-ruby"
18
- spec.metadata["changelog_uri"] = "https://github.com/mgmarlow/notion-sdk-ruby/blob/master/CHANGELOG.md"
19
-
20
- # Specify which files should be added to the gem when it is released.
21
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
- spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
23
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
- end
25
- spec.bindir = "exe"
26
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
- spec.require_paths = ["lib"]
28
-
29
- spec.add_dependency "httparty", "~> 0.18.1"
30
-
31
- spec.add_development_dependency "rake", "~> 12.0"
32
- spec.add_development_dependency "rspec", "~> 3.0"
33
- spec.add_development_dependency "standardrb", "~> 1.0"
34
- spec.add_development_dependency "webmock", "~> 3.12"
35
- spec.add_development_dependency "pry", "~> 0.14.1"
36
- spec.add_development_dependency "dotenv", "~> 2.7"
37
- end
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require_relative "lib/notion-sdk-ruby/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "notion-sdk-ruby"
7
+ spec.version = Notion::VERSION
8
+ spec.authors = ["Graham Marlow"]
9
+ spec.email = ["mgmarlow@hey.com"]
10
+
11
+ spec.summary = "Notion SDK"
12
+ spec.homepage = "https://github.com/mgmarlow/notion-sdk-ruby"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/mgmarlow/notion-sdk-ruby"
18
+ spec.metadata["changelog_uri"] = "https://github.com/mgmarlow/notion-sdk-ruby/blob/master/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency "faraday", "~> 1.8"
30
+ spec.add_dependency "faraday_middleware", "~> 1.1"
31
+
32
+ spec.add_development_dependency "rake", "~> 12.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ spec.add_development_dependency "standard", "~> 1.0"
35
+ spec.add_development_dependency "webmock", "~> 3.12"
36
+ spec.add_development_dependency "pry", "~> 0.14.1"
37
+ spec.add_development_dependency "dotenv", "~> 2.7"
38
+ spec.add_development_dependency "activesupport", "~> 6.1"
39
+ spec.add_development_dependency "yard", "~> 0.9.26"
40
+ spec.add_development_dependency "vcr", "~> 6.0"
41
+ end
data/todos.md ADDED
@@ -0,0 +1,11 @@
1
+ # todos
2
+
3
+ - [x] Add VCR
4
+ - [x] Swap over specs to use VCR
5
+ - [x] Update to latest notion SDK version
6
+ - [x] Improve generated docs by removing extra module namespaces
7
+ - [x] Remove manual docs from readme, move to auto-generated docs
8
+ - [ ] New endpoint: Delete block
9
+ - [ ] New endpoint: Retrieve a page property item
10
+ - [ ] Pagination APIs
11
+ - [ ] Examples