notion-model 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c8fbf77634cc79e6fecbd412bed7fea8a1908edbddddfd448f517e11e240c083
4
+ data.tar.gz: 2f3312c53060320274145678cfdaf0b62f21dbe30214a2be6441e69d8103bdfe
5
+ SHA512:
6
+ metadata.gz: b385f026f1165f8f1e7bae232dc639e518b32d4817f975b33980b34931839370e081ee691722323e62e3a68cc63b1e86d833fc2ca3870361d4a3d15df99814e7
7
+ data.tar.gz: 064a25f683224c585a7a60a858e20316fca86457e136bdecff13e218f4f8cafae1c26a25e0e76be2c589b18d9ca39ae15989989e29e3b688b819574c698a9d89
data/.env ADDED
@@ -0,0 +1 @@
1
+ NOTION_API_TOKEN=<YOUR_NOTION_TOKEN>
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6.5
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Style/IfUnlessModifier:
13
+ Enabled: false
14
+
15
+ Layout/LineLength:
16
+ Max: 128
17
+
18
+ Metrics/MethodLength:
19
+ Enabled: false
20
+
21
+ Metrics/BlockLength:
22
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-03-01
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at 383490032@qq.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in notion-rb.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,89 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ notion-model (0.1.0)
5
+ dotenv
6
+ faraday (~> 2.7)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.8.2)
12
+ public_suffix (>= 2.0.2, < 6.0)
13
+ ast (2.4.2)
14
+ crack (0.4.5)
15
+ rexml
16
+ debug (1.7.1)
17
+ irb (>= 1.5.0)
18
+ reline (>= 0.3.1)
19
+ diff-lcs (1.5.0)
20
+ dotenv (2.8.1)
21
+ faraday (2.7.4)
22
+ faraday-net_http (>= 2.0, < 3.1)
23
+ ruby2_keywords (>= 0.0.4)
24
+ faraday-net_http (3.0.2)
25
+ hashdiff (1.0.1)
26
+ io-console (0.6.0)
27
+ irb (1.6.2)
28
+ reline (>= 0.3.0)
29
+ json (2.6.2)
30
+ parallel (1.22.1)
31
+ parser (3.2.1.0)
32
+ ast (~> 2.4.1)
33
+ public_suffix (5.0.1)
34
+ rainbow (3.1.1)
35
+ regexp_parser (2.7.0)
36
+ reline (0.3.2)
37
+ io-console (~> 0.5)
38
+ rexml (3.2.5)
39
+ rspec (3.11.0)
40
+ rspec-core (~> 3.11.0)
41
+ rspec-expectations (~> 3.11.0)
42
+ rspec-mocks (~> 3.11.0)
43
+ rspec-core (3.11.0)
44
+ rspec-support (~> 3.11.0)
45
+ rspec-expectations (3.11.1)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.11.0)
48
+ rspec-mocks (3.11.1)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.11.0)
51
+ rspec-support (3.11.1)
52
+ rubocop (1.36.0)
53
+ json (~> 2.3)
54
+ parallel (~> 1.10)
55
+ parser (>= 3.1.2.1)
56
+ rainbow (>= 2.2.2, < 4.0)
57
+ regexp_parser (>= 1.8, < 3.0)
58
+ rexml (>= 3.2.5, < 4.0)
59
+ rubocop-ast (>= 1.20.1, < 2.0)
60
+ ruby-progressbar (~> 1.7)
61
+ unicode-display_width (>= 1.4.0, < 3.0)
62
+ rubocop-ast (1.21.0)
63
+ parser (>= 3.1.1.0)
64
+ ruby-progressbar (1.11.0)
65
+ ruby2_keywords (0.0.5)
66
+ unicode-display_width (2.3.0)
67
+ vcr (6.1.0)
68
+ webmock (3.18.1)
69
+ addressable (>= 2.8.0)
70
+ crack (>= 0.3.2)
71
+ hashdiff (>= 0.4.0, < 2.0.0)
72
+
73
+ PLATFORMS
74
+ x64-unknown
75
+ x64-unknown
76
+ x86_64-darwin-20
77
+ x86_64-darwin-21
78
+ x86_64-linux
79
+
80
+ DEPENDENCIES
81
+ debug
82
+ notion-model!
83
+ rspec (~> 3.2)
84
+ rubocop (~> 1.7)
85
+ vcr
86
+ webmock
87
+
88
+ BUNDLED WITH
89
+ 2.3.3
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Even
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 even.z
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Notion::Rb
2
+
3
+ 一个能让你像ActiveModel一样增删改查Notion中的Databases和Pages的RubyGem
4
+
5
+ ## 安装
6
+
7
+ 依赖:
8
+ - Ruby >= 2.6
9
+
10
+ 安装命令:
11
+ ```bash
12
+ gem install notion-rb
13
+ ```
14
+
15
+ 或在Gemfile中增加
16
+ ```ruby
17
+ gem 'notion-rb'
18
+ ```
19
+
20
+ 然后执行
21
+ ```bash
22
+ bundle install
23
+ ```
24
+
25
+ ## 使用
26
+ ```ruby
27
+ # 先做好必要配置
28
+ Notion.config.token = 'YOUR_NOTION_TOKEN'
29
+
30
+ # 或者
31
+ Notion.configure do |config|
32
+ config.token = "YOUR_NOTION_TOKEN"
33
+ end
34
+
35
+ # 参考ActiveRecord处理数据的方法,以面向对象的方式操作数据
36
+ database = Notion::Database.find(your_database_id)
37
+
38
+ # 由于不同Database下的Page都可能有不同的属性
39
+ # 因此每个Page实例中的属性在获得pages时动态生成
40
+ pages = database.pages
41
+
42
+ # 把提供的选项转换为等价的JSON Query结构体
43
+ # 触发Http请求并从Response中检索对应的结果
44
+ # 为每个查询结果实例化对应的模型对象
45
+ some_pages = pages.where(options)
46
+
47
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
data/bin/console ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "notion"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+
16
+ def reload
17
+ # reload lib
18
+ Dir.glob("#{File.expand_path("../lib", __dir__)}/**/*.rb").each { |f| load(f) }
19
+ true
20
+ end
21
+
22
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +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
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Notion
4
+ # Dynamic configuration
5
+ module Configurable
6
+ def configure
7
+ yield config
8
+ end
9
+
10
+ def config
11
+ @config ||= Configuration.new
12
+ end
13
+
14
+ def setup
15
+ @config = nil
16
+ end
17
+
18
+ # Access Notion.config to get configurations
19
+ class Configuration
20
+ ATTRIBUTES = %i[token endpoint timeout].freeze
21
+
22
+ attr_accessor(*ATTRIBUTES)
23
+
24
+ def initialize
25
+ @token = ENV["NOTION_API_TOKEN"]
26
+ @endpoint = "https://api.notion.com/v1"
27
+ @timeout = 20
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Notion
4
+ module Errors
5
+ # Collect all standard errors
6
+ class Error < StandardError
7
+ end
8
+
9
+ # Require Token before request
10
+ class MissingTokenError < Error
11
+ def initialize
12
+ super("Token missing! Please use `Notion.config.token = YOUR_TOKEN` to set token.")
13
+ end
14
+ end
15
+
16
+ class ParamError < Error
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Notion
4
+ module Errors
5
+ # Collect all http errors
6
+ class HttpError < ::Faraday::Error
7
+ attr_reader :response
8
+
9
+ def initialize(code, message, response = nil)
10
+ super("Code: #{code}, Message: \"#{message}\"")
11
+ @response = response
12
+ end
13
+ end
14
+
15
+ # A unified client error for timeouts
16
+ class TimeoutError < HttpError; end
17
+
18
+ # Raised by middlewares that parse the response, like the JSON response middleware.
19
+ class ParsingError < HttpError; end
20
+
21
+ # Notion Error: https://developers.notion.com/reference/status-codes
22
+
23
+ # The request body could not be decoded as JSON.
24
+ class InvalidJsonError < HttpError; end
25
+
26
+ # The request URL is not valid.
27
+ class InvalidRequestUrlError < HttpError; end
28
+
29
+ # This request is not supported.
30
+ class InvalidRequestError < HttpError; end
31
+
32
+ # The request body does not match the schema for the expected parameters.
33
+ # Check the "message" property for more details.
34
+ class ValidationError < HttpError; end
35
+
36
+ # The request is missing the required Notion-Version header. See Versioning.
37
+ class MissingVersionError < HttpError; end
38
+
39
+ # The bearer token is not valid.
40
+ class UnauthorizedError < HttpError; end
41
+
42
+ # Given the bearer token used, the client doesn't have permission to perform this operation.
43
+ class RestrictedResourceError < HttpError; end
44
+
45
+ # Given the bearer token used, the resource does not exist.
46
+ # This error can also indicate that the resource has not been shared with owner of the bearer token.
47
+ class ObjectNotFoundError < HttpError; end
48
+
49
+ # The transaction could not be completed, potentially due to a data collision.
50
+ # Make sure the parameters are up to date and try again.
51
+ class ConflictError < HttpError; end
52
+
53
+ # This request exceeds the number of requests allowed. Slow down and try again. More details on rate limits.
54
+ class RateLimitedError < HttpError; end
55
+
56
+ # An unexpected error occurred. Reach out to Notion support.
57
+ class InternalServerError < HttpError; end
58
+
59
+ # Notion is unavailable. Try again later.
60
+ # This can occur when the time to respond to a request takes longer than 60 seconds, the maximum request timeout.
61
+ class ServiceUnavailableError < HttpError; end
62
+
63
+ # Notion's database is unavailable or in an unqueryable state. Try again later.
64
+ class DatabaseConnectionUnavailableError < HttpError; end
65
+
66
+ # Notion timed out while attempting to complete this request. Please try again later.
67
+ class GatewayTimeoutError < HttpError; end
68
+
69
+ NOTION_ERROR_CODES = {
70
+ "invalid_json" => InvalidJsonError,
71
+ "invalid_request_url" => InvalidRequestUrlError,
72
+ "invalid_request" => InvalidRequestError,
73
+ "validation_error" => ValidationError,
74
+ "missing_version" => MissingVersionError,
75
+ "unauthorized" => UnauthorizedError,
76
+ "restricted_resource" => RestrictedResourceError,
77
+ "object_not_found" => ObjectNotFoundError,
78
+ "conflict_error" => ConflictError,
79
+ "rate_limited" => RateLimitedError,
80
+ "internal_server_error" => InternalServerError,
81
+ "service_unavailable" => ServiceUnavailableError,
82
+ "database_connection_unavailable" => DatabaseConnectionUnavailableError,
83
+ "gateway_timeout" => GatewayTimeoutError
84
+ }.freeze
85
+ end
86
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Notion
4
+ module Http
5
+ # For http request and response
6
+ class Client
7
+ include Connection
8
+ include Request
9
+ include Endpoints::DatabaseMethods
10
+ include Endpoints::PageMethods
11
+
12
+ attr_accessor(*Configurable::Configuration::ATTRIBUTES)
13
+
14
+ def initialize(options = {})
15
+ Notion::Configurable::Configuration::ATTRIBUTES.each do |key|
16
+ send("#{key}=", options.fetch(key, Notion.config.send(key)))
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Notion
4
+ module Http
5
+ # Initialize a ::Faraday::Connection
6
+ module Connection
7
+ private
8
+
9
+ def connection
10
+ @connection ||=
11
+ begin
12
+ options = {
13
+ headers: {
14
+ "User-Agent" => "notion-rb/#{Notion::VERSION}",
15
+ "Authorization" => "Bearer #{token}",
16
+ "Notion-Version" => Notion::DEFAULT_NOTION_VERSION,
17
+ "Content-Type" => "application/json"
18
+ }
19
+ }
20
+
21
+ options[:request] = { timeout: timeout } if timeout
22
+
23
+ ::Faraday::Connection.new(endpoint, options) do |builder|
24
+ builder.use Notion::Http::Middlewares::RaiseError
25
+ builder.use ::Faraday::Response::Json
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Notion
4
+ module Http
5
+ module Endpoints
6
+ # Notion::Http::Client.new(token: NOTION_TOKEN).retrieve_a_database(DATABASE_ID)
7
+ module DatabaseMethods
8
+ # Retrieves a database object.
9
+ def retrieve_a_database(options = {})
10
+ raise Notion::Errors::ParamError, "Missing required params :database_id" if options[:database_id].nil?
11
+
12
+ get("databases/#{options[:database_id]}")
13
+ end
14
+
15
+ # Gets a list of Pages contained in the database, filtered and ordered
16
+ # according to the filter conditions and sort criteria provided in the request.
17
+ def query_a_database(options = {})
18
+ raise Notion::Errors::ParamError, "Missing required params :database_id" if options[:database_id].nil?
19
+
20
+ database_id = options.delete(:database_id)
21
+ post("databases/#{database_id}/query", options)
22
+ end
23
+
24
+ # Creates a database as a subpage in the specified parent page, with the specified properties schema.
25
+ def create_a_database(options = {})
26
+ raise Notion::Errors::ParamError, "Missing required params :parent.page_id" if options.dig(:parent, :page_id).nil?
27
+ raise Notion::Errors::ParamError, "Missing required params :parent.properties" if options[:properties].nil?
28
+
29
+ post("databases", options)
30
+ end
31
+
32
+ # Update the title, description, or properties of a specified database. Properties define the columns of a database.
33
+ def update_a_database(options = {})
34
+ raise Notion::Errors::ParamError, "Missing required params :database_id" if options[:database_id].nil?
35
+
36
+ database_id = options.delete(:database_id)
37
+ patch("databases/#{database_id}", options)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Notion
4
+ module Http
5
+ module Endpoints
6
+ # Notion::Http::Client.new.retrieve_a_page(page_id)
7
+ module PageMethods
8
+ # Retrieves a Page object using the ID specified.
9
+ def retrieve_a_page(options = {})
10
+ raise Notion::Errors::ParamError, "Missing required params :page_id" if options[:page_id].nil?
11
+
12
+ get("pages/#{options[:page_id]}")
13
+ end
14
+
15
+ # Retrieves a property_item object for a given page_id and property_id.
16
+ def retrieve_a_page_property_item(options = {})
17
+ raise Notion::Errors::ParamError, "Missing required params :page_id" if options[:page_id].nil?
18
+ raise Notion::Errors::ParamError, "Missing required params :property_id" if options[:property_id].nil?
19
+
20
+ get("pages/#{options[:page_id]}/properties/#{options[:property_id]}")
21
+ end
22
+
23
+ # Creates a new page that is a child of an existing page.
24
+ def create_a_page(options = {})
25
+ if options.dig(:parent, :page_id).nil? && options.dig(:parent, :database_id).nil?
26
+ raise Notion::Errors::ParamError, "Missing required params :parent.page_id or :parent.database_id"
27
+ end
28
+ raise Notion::Errors::ParamError, "Missing required params :parent.properties" if options[:properties].nil?
29
+
30
+ post("pages", options)
31
+ end
32
+
33
+ # Updates the properties of a page in a database.
34
+ def update_page(options = {})
35
+ raise Notion::Errors::ParamError, "Missing required params :page_id" if options[:page_id].nil?
36
+
37
+ page_id = options.delete(:page_id)
38
+ patch("pages/#{page_id}", options)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Notion
4
+ module Http
5
+ module Middlewares
6
+ # A middleware that raise Notion::Errors::HttpError
7
+ class RaiseError < ::Faraday::Middleware
8
+ def on_complete(env)
9
+ return if env.success?
10
+
11
+ body = env.body
12
+ return unless body
13
+
14
+ error_code = body["code"]
15
+ error_message = body["message"]
16
+
17
+ error_class = Notion::Errors::NOTION_ERROR_CODES[error_code]
18
+ error_class ||= Notion::Errors::HttpError
19
+ raise error_class.new(error_code, error_message, env.response)
20
+ end
21
+
22
+ def call(env)
23
+ super
24
+ rescue ::Faraday::TimeoutError, ::Faraday::ConnectionFailed
25
+ raise Notion::Errors::TimeoutError.new("timeout_error", "A unified client error for timeouts")
26
+ rescue ::Faraday::ParsingError
27
+ parsing_error_message = "Raised by middlewares that parse the response, like the JSON response middleware."
28
+ raise Notion::Errors::ParsingError.new("parsing_error", parsing_error_message, env.response)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Notion
4
+ module Http
5
+ # ::Faraday::Connection.http_method
6
+ module Request
7
+ def get(path, options = {})
8
+ request(:get, path, options)
9
+ end
10
+
11
+ def patch(path, options = {})
12
+ request(:patch, path, options)
13
+ end
14
+
15
+ def post(path, options = {})
16
+ request(:post, path, options)
17
+ end
18
+
19
+ def put(path, options = {})
20
+ request(:put, path, options)
21
+ end
22
+
23
+ def delete(path, options = {})
24
+ request(:delete, path, options)
25
+ end
26
+
27
+ private
28
+
29
+ def request(method, path, options)
30
+ raise Notion::Errors::MissingTokenError if token.nil?
31
+
32
+ response = connection.send(method) do |request|
33
+ case method
34
+ when :get, :delete
35
+ request.url(path, options)
36
+ when :post, :put, :patch
37
+ request.path = path
38
+ request.body = options.to_json unless options.empty?
39
+ end
40
+ request.options.merge!(options.delete(:request)) if options.key?(:request)
41
+ end
42
+ response.body
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Notion
4
+ VERSION = "0.1.0"
5
+ DEFAULT_NOTION_VERSION = "2022-06-28"
6
+ end
data/lib/notion.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "dotenv/load"
5
+
6
+ require_relative "notion/version"
7
+ require_relative "notion/configurable"
8
+ require_relative "notion/errors/error"
9
+ require_relative "notion/errors/http_error"
10
+ require_relative "notion/http/middlewares/raise_error"
11
+ require_relative "notion/http/connection"
12
+ require_relative "notion/http/request"
13
+ require_relative "notion/http/endpoints/database_methods"
14
+ require_relative "notion/http/endpoints/page_methods"
15
+ require_relative "notion/http/client"
16
+
17
+ module Notion
18
+ extend Configurable
19
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: notion-model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - even.z
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dotenv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: debug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - 383490032@qq.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".env"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - CHANGELOG.md
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - LICENSE
126
+ - LICENSE.txt
127
+ - README.md
128
+ - Rakefile
129
+ - bin/console
130
+ - bin/setup
131
+ - lib/notion.rb
132
+ - lib/notion/configurable.rb
133
+ - lib/notion/errors/error.rb
134
+ - lib/notion/errors/http_error.rb
135
+ - lib/notion/http/client.rb
136
+ - lib/notion/http/connection.rb
137
+ - lib/notion/http/endpoints/database_methods.rb
138
+ - lib/notion/http/endpoints/page_methods.rb
139
+ - lib/notion/http/middlewares/raise_error.rb
140
+ - lib/notion/http/request.rb
141
+ - lib/notion/version.rb
142
+ homepage: https://github.com/Ven0802/notion-model
143
+ licenses:
144
+ - MIT
145
+ metadata:
146
+ homepage_uri: https://github.com/Ven0802/notion-model
147
+ source_code_uri: https://github.com/Ven0802/notion-model
148
+ changelog_uri: https://github.com/Ven0802/notion-model/releases
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: 2.6.0
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubygems_version: 3.0.9
165
+ signing_key:
166
+ specification_version: 4
167
+ summary: A ruby gem that allows you to read, write, and update Notion databases and
168
+ pages like ActiveModel.
169
+ test_files: []