notion_ruby 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fbfef85b3b04544b084e74ee01fc9a14bd47d3bc17327d698df7b11b8b5b430e
4
+ data.tar.gz: c09cb9c39baaf5be2b132f78d6b2e9f9ada3a763226c46749153d0c8accb9ca3
5
+ SHA512:
6
+ metadata.gz: 0d4aa758219c1f663632c5d8f9d064fca0342be8d89dc21288ecaa1c5f3d4264ae17abea07921e51c4d0e1068832c67ee58a25bdf334a4c7ab8c0cf4e09c4f44
7
+ data.tar.gz: 4837ec9eb5f569f7cf3f8d3d36fc41d0f711b3c31703d302f2413d259d083732d6185b645668d1364a1dec002d69c6e2d463ccdd1765803c90c6f875d6e7cd8e
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.1
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.15
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ Gemfile.lock
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ spec/settings.yml
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.7
6
+
7
+ Style/StringLiterals:
8
+ Enabled: true
9
+ EnforcedStyle: double_quotes
10
+
11
+ Style/StringLiteralsInInterpolation:
12
+ Enabled: true
13
+ EnforcedStyle: double_quotes
14
+
15
+ Layout/LineLength:
16
+ Max: 120
17
+
18
+ Documentation:
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] - 2021-07-03
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 tominaga.switch@gmail.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,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in notion_ruby.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
13
+ gem "rubocop-rspec"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Akifumi Tominaga
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,179 @@
1
+ # NotionRuby
2
+
3
+ This is an unofficial ruby client for the [Notion API](https://developers.notion.com/).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'notion_ruby'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install notion_ruby
20
+
21
+ ## Usage
22
+
23
+ Instantiate the client with auth token:
24
+
25
+ ```ruby
26
+ notion = NotionRuby.new({ access_token: 'dummy' })
27
+ ```
28
+
29
+ ### Databases
30
+
31
+ List databases:
32
+
33
+ ```ruby
34
+ notion.databases
35
+ ```
36
+
37
+ Retrieve a database:
38
+
39
+ ```ruby
40
+ notion.databases("database_id")
41
+ ```
42
+
43
+ Query a database:
44
+
45
+ ```ruby
46
+ query = { filter: { or: [{ property: "title", text: { contains: "A Notion Page" } }] } }
47
+ response = notion.databases("database_id").query(query)
48
+ ```
49
+
50
+ ### Pages
51
+
52
+ Retrieve a page:
53
+
54
+ ```ruby
55
+ pages = notion.pages("page_id")
56
+ ```
57
+
58
+ Create a page:
59
+
60
+ ```ruby
61
+ params = {
62
+ parent: { database_id: "database_id" },
63
+ properties: {
64
+ Name: {
65
+ title: [
66
+ {
67
+ text: {
68
+ content: "Create"
69
+ }
70
+ }
71
+ ]
72
+ }
73
+ }
74
+ }
75
+ response = notion.pages.create(params)
76
+ ```
77
+
78
+ Update page:
79
+
80
+ ```ruby
81
+ params = {
82
+ properties: {
83
+ Name: {
84
+ title: [
85
+ {
86
+ text: {
87
+ content: "Update"
88
+ }
89
+ }
90
+ ]
91
+ }
92
+ }
93
+ }
94
+ response = notion.pages(id).update(params)
95
+ ```
96
+
97
+ ### Blocks
98
+
99
+ Retrieve block children:
100
+
101
+ ```ruby
102
+ children = notion.blocks("block_id").children
103
+ ```
104
+
105
+ Append block children:
106
+
107
+ ```ruby
108
+ params = {
109
+ "children": [
110
+ {
111
+ object: "block",
112
+ type: "heading_2",
113
+ heading_2: { text: [{ type: "text", text: { content: "Lacinato kale" } }] }
114
+ },
115
+ {
116
+ object: "block",
117
+ type: "paragraph",
118
+ paragraph: {
119
+ text: [
120
+ {
121
+ type: "text",
122
+ text: {
123
+ content: "example content",
124
+ link: { "url": "https://en.wikipedia.org/wiki/Lacinato_kale" }
125
+ }
126
+ }
127
+ ]
128
+ }
129
+ }
130
+ ]
131
+ }
132
+ response = notion.blocks("block_id").children.append(params)
133
+ ```
134
+
135
+ ### User
136
+
137
+ Retrieve a user:
138
+
139
+ ```ruby
140
+ user = notion.users("user_id")
141
+ ```
142
+
143
+ List all users:
144
+
145
+ ```ruby
146
+ notion.users
147
+ ```
148
+
149
+ ### Search
150
+
151
+ Search:
152
+
153
+ ```ruby
154
+ response = notion.search({ query: "Example" })
155
+ ```
156
+
157
+ ## Development
158
+
159
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
160
+ also run `bin/console` for an interactive prompt that will allow you to experiment.
161
+
162
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
163
+ version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
164
+ push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
165
+
166
+ ## Contributing
167
+
168
+ Bug reports and pull requests are welcome on GitHub at https://github.com/decoch/notion-ruby. This project is intended
169
+ to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
170
+ the [code of conduct](https://github.com/decoch/notion-ruby/blob/main/CODE_OF_CONDUCT.md).
171
+
172
+ ## License
173
+
174
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
175
+
176
+ ## Code of Conduct
177
+
178
+ Everyone interacting in the NotionRuby project's codebases, issue trackers, chat rooms and mailing lists is expected to
179
+ follow the [code of conduct](https://github.com/decoch/notion-ruby/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "notion_ruby"
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
+ 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,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "notion_ruby/version"
4
+ require_relative "notion_ruby/connection"
5
+ require_relative "notion_ruby/errors"
6
+ require_relative "notion_ruby/state_methods"
7
+ require_relative "notion_ruby/resource_proxy"
8
+ require_relative "notion_ruby/api/blocks"
9
+ require_relative "notion_ruby/api/databases"
10
+ require_relative "notion_ruby/api/pages"
11
+ require_relative "notion_ruby/api/search"
12
+ require_relative "notion_ruby/api/users"
13
+
14
+ class NotionRuby
15
+ attr_reader :connection
16
+
17
+ include NotionRuby::API::Blocks
18
+ include NotionRuby::API::Databases
19
+ include NotionRuby::API::Pages
20
+ include NotionRuby::API::Search
21
+ include NotionRuby::API::Users
22
+
23
+ def initialize(options = {}, &block)
24
+ @options = options
25
+ @block = block if block
26
+ @connection = NotionRuby::Connection.new(options, &block)
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ class NotionRuby
4
+ module API
5
+ module Blocks
6
+ module Children
7
+ class Proxy < ::NotionRuby::ResourceProxy
8
+ def append(params)
9
+ connection.patch(path_prefix, params).body
10
+ end
11
+ end
12
+ end
13
+
14
+ class Proxy < ::NotionRuby::ResourceProxy
15
+ def children
16
+ prefix = "#{path_prefix}/children"
17
+ NotionRuby::API::Blocks::Children::Proxy.new(connection, prefix)
18
+ end
19
+ end
20
+
21
+ def blocks(id, params = {})
22
+ Proxy.new(connection, "/v1/blocks/#{id}", params)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ class NotionRuby
4
+ module API
5
+ module Databases
6
+ class Proxy < ::NotionRuby::ResourceProxy
7
+ def query(params)
8
+ connection.post("#{path_prefix}/query", params).body
9
+ end
10
+ end
11
+
12
+ def databases(id = nil, params = {})
13
+ params = id if id.is_a? Hash
14
+ path_prefix = !id.is_a?(Hash) && id ? "/v1/databases/#{id}" : "/v1/databases"
15
+ Proxy.new(connection, path_prefix, params)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class NotionRuby
4
+ module API
5
+ module Pages
6
+ class Proxy < ::NotionRuby::ResourceProxy
7
+ def create(params)
8
+ connection.post(path_prefix, params).body
9
+ end
10
+
11
+ def update(params)
12
+ connection.patch(path_prefix, params).body
13
+ end
14
+ end
15
+
16
+ def pages(id = nil, params = {})
17
+ params = id if id.is_a? Hash
18
+ path_prefix = !id.is_a?(Hash) && id ? "/v1/pages/#{id}" : "/v1/pages"
19
+ Proxy.new(connection, path_prefix, params)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class NotionRuby
4
+ module API
5
+ module Search
6
+ class Proxy < ::NotionRuby::ResourceProxy
7
+ end
8
+
9
+ def search(params = {})
10
+ path_prefix = "/v1/search"
11
+ connection.post(path_prefix, params).body
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ class NotionRuby
4
+ module API
5
+ module Users
6
+ class Proxy < ::NotionRuby::ResourceProxy
7
+ end
8
+
9
+ def users(id = nil, params = {})
10
+ params = id if id.is_a? Hash
11
+ path_prefix = !id.is_a?(Hash) && id ? "/v1/users/#{id}" : "/v1/users"
12
+ Proxy.new(connection, path_prefix, params)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "faraday_middleware"
5
+ require "multi_json"
6
+
7
+ class NotionRuby
8
+ class Connection < Faraday::Connection
9
+ attr_reader :hash
10
+
11
+ def parallel_connection(adapter = :typhoeus)
12
+ conn = self.class.new @hash
13
+ conn.adapter adapter
14
+ conn
15
+ end
16
+
17
+ def initialize(hash = {})
18
+ @hash = hash
19
+
20
+ super(hash[:api_url] || "https://api.notion.com") do |builder|
21
+ yield builder if block_given?
22
+ builder.use Faraday::Response::RaiseNotionRubyError
23
+ builder.use FaradayMiddleware::EncodeJson
24
+ builder.use FaradayMiddleware::ParseJson, content_type: /\bjson$/
25
+ builder.adapter Faraday.default_adapter
26
+ end
27
+
28
+ headers["Authorization"] = "Bearer #{hash[:access_token]}" if hash.key?(:access_token)
29
+ headers["Notion-Version"] = NOTION_VERSION
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json/ext"
4
+ require "multi_json"
5
+
6
+ class NotionRuby
7
+ # Custom error class for rescuing from all Notion errors
8
+ class Error < StandardError
9
+ def initialize(response = nil)
10
+ @response = response
11
+ super(build_error_message)
12
+ end
13
+
14
+ def response_body
15
+ @response_body ||= (body = @response[:body]) && !body.empty? ? body : nil
16
+ end
17
+
18
+ private
19
+
20
+ def build_error_message
21
+ return nil if @response.nil?
22
+
23
+ "#{message}#{errors}"
24
+ end
25
+
26
+ def message
27
+ response_body ? (response_body["error"] || response_body["message"] || "").to_s : ""
28
+ end
29
+
30
+ def errors
31
+ return if message.empty?
32
+
33
+ if response_body["errors"]
34
+ " #{response_body["errors"].to_a.map do |e|
35
+ e["message"] || e["code"]
36
+ end.join(", ")} "
37
+ else
38
+ ""
39
+ end
40
+ end
41
+ end
42
+
43
+ # Raised when Notion returns a 400 HTTP status code
44
+ class BadRequest < Error; end
45
+
46
+ # Raised when Notion returns a 401 HTTP status code
47
+ class Unauthorized < Error; end
48
+
49
+ # Raised when Notion returns a 403 HTTP status code
50
+ class Forbidden < Error; end
51
+
52
+ # Raised when Notion returns a 404 HTTP status code
53
+ class NotFound < Error; end
54
+
55
+ # Raised when Notion returns a 406 HTTP status code
56
+ class NotAcceptable < Error; end
57
+
58
+ # Raised when Notion returns a 422 HTTP status code
59
+ class UnprocessableEntity < Error; end
60
+
61
+ # Raised when Notion returns a 500 HTTP status code
62
+ class InternalServerError < Error; end
63
+
64
+ # Raised when Notion returns a 501 HTTP status code
65
+ class NotImplemented < Error; end
66
+
67
+ # Raised when Notion returns a 502 HTTP status code
68
+ class BadGateway < Error; end
69
+
70
+ # Raised when Notion returns a 503 HTTP status code
71
+ class ServiceUnavailable < Error; end
72
+ end
73
+
74
+ # @api private
75
+ module Faraday
76
+ class Response
77
+ class RaiseNotionRubyError < Response::Middleware
78
+ ERROR_MAP = {
79
+ 400 => NotionRuby::BadRequest,
80
+ 401 => NotionRuby::Unauthorized,
81
+ 403 => NotionRuby::Forbidden,
82
+ 406 => NotionRuby::NotAcceptable,
83
+ 422 => NotionRuby::UnprocessableEntity,
84
+ 500 => NotionRuby::InternalServerError,
85
+ 501 => NotionRuby::NotImplemented,
86
+ 502 => NotionRuby::BadGateway,
87
+ 503 => NotionRuby::ServiceUnavailable
88
+ }.freeze
89
+
90
+ def on_complete(response)
91
+ key = response[:status].to_i
92
+ raise ERROR_MAP[key], response if ERROR_MAP.key? key
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ class NotionRuby
4
+ # ResourceProxy lets us create a virtual
5
+ # proxy for any API resource, utilizing
6
+ # method_missing to handle passing
7
+ # messages to the real object
8
+ #
9
+ class ResourceProxy
10
+ # Undefine methods that might get in the way
11
+ instance_methods.each do |m|
12
+ undef_method m unless m =~ /^__|instance_eval|instance_variable_get|object_id|respond_to|class/
13
+ end
14
+
15
+ include NotionRuby::CRUD
16
+
17
+ # Make connection and path_prefix readable
18
+ attr_reader :connection, :path_prefix, :params, :id
19
+
20
+ # Instantiates proxy with the connection
21
+ # and path_prefix
22
+ #
23
+ # connection - [NotionRuby::Connection] object
24
+ # path_prefix - String
25
+ # param - Hash or String
26
+ #
27
+ def initialize(connection, path_prefix, params = {}, &block)
28
+ unless params.is_a? Hash
29
+ @id = params
30
+ params = {}
31
+ end
32
+ @connection = connection
33
+ @path_prefix = path_prefix
34
+ @params = params
35
+ @block = block if block
36
+ subject if block
37
+ end
38
+
39
+ # Method_missing takes any message passed
40
+ # to the ResourceProxy and sends it to the
41
+ # real object
42
+ #
43
+ # message - Message object
44
+ # args* - Arguments passed
45
+ #
46
+ def method_missing(message, *args, &block)
47
+ subject.send(message, *args, &block)
48
+ end
49
+
50
+ def respond_to_missing?(symbol, _include_private)
51
+ subject.keys(symbol)
52
+ end
53
+
54
+ # Raw is the raw response from the faraday
55
+ # Use this if you need access to status codes
56
+ # or header values
57
+ #
58
+ def raw
59
+ connection.get(path_prefix) { |req| req.params.merge! params }
60
+ end
61
+
62
+ # Subject is the response body parsed
63
+ # as json
64
+ #
65
+ # Returns json
66
+ #
67
+ def subject
68
+ @subject ||= connection.get(path_prefix) do |req|
69
+ req.params.merge! params
70
+ @block&.call(req)
71
+ end.body
72
+ end
73
+
74
+ def build_prefix(first_argument, endpoint)
75
+ if !first_argument.is_a?(Hash) && !first_argument.nil?
76
+ File.join(path_prefix, "/#{endpoint}/#{first_argument}")
77
+ else
78
+ File.join(path_prefix, "/#{endpoint}")
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ class NotionRuby
4
+ module CRUD
5
+ # Create
6
+ #
7
+ # return json
8
+ #
9
+ def get(attributes, &block)
10
+ connection.get(path_prefix, attributes, &block).body
11
+ end
12
+
13
+ # Create
14
+ #
15
+ # return json
16
+ #
17
+ def create(attributes, &block)
18
+ connection.post(path_prefix, attributes, &block).body
19
+ end
20
+
21
+ # Patch
22
+ #
23
+ # return json
24
+ #
25
+ def patch(attributes, &block)
26
+ connection.patch(path_prefix, attributes, &block).body
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ class NotionRuby
4
+ VERSION = "0.1.0"
5
+ NOTION_VERSION = "2021-05-13"
6
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/notion_ruby/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "notion_ruby"
7
+ spec.version = NotionRuby::VERSION
8
+ spec.authors = ["decoch"]
9
+ spec.email = ["tominaga.switch@gmail.com"]
10
+
11
+ spec.summary = "Access Notion in ruby."
12
+ spec.description = "A simple notion client api."
13
+ spec.homepage = "https://github.com/decoch/notion-ruby"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "faraday", "~> 0.9"
31
+ spec.add_dependency "faraday_middleware", "~> 0.9"
32
+ spec.add_dependency "hashie", "~> 3.3.2"
33
+ spec.add_dependency "highline", "~> 1.6.15"
34
+ spec.add_dependency "multi_json", "~> 1.3"
35
+ spec.add_dependency "typhoeus", "~> 0.7.0"
36
+
37
+ spec.add_development_dependency "vcr"
38
+ spec.add_development_dependency "webmock"
39
+ end
metadata ADDED
@@ -0,0 +1,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: notion_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - decoch
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-07-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: hashie
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.3.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.3.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: highline
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.6.15
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.6.15
69
+ - !ruby/object:Gem::Dependency
70
+ name: multi_json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: typhoeus
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.7.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.7.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: vcr
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
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: A simple notion client api.
126
+ email:
127
+ - tominaga.switch@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".github/workflows/main.yml"
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".rubocop.yml"
136
+ - CHANGELOG.md
137
+ - CODE_OF_CONDUCT.md
138
+ - Gemfile
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - lib/notion_ruby.rb
145
+ - lib/notion_ruby/api/blocks.rb
146
+ - lib/notion_ruby/api/databases.rb
147
+ - lib/notion_ruby/api/pages.rb
148
+ - lib/notion_ruby/api/search.rb
149
+ - lib/notion_ruby/api/users.rb
150
+ - lib/notion_ruby/connection.rb
151
+ - lib/notion_ruby/errors.rb
152
+ - lib/notion_ruby/resource_proxy.rb
153
+ - lib/notion_ruby/state_methods.rb
154
+ - lib/notion_ruby/version.rb
155
+ - notion_ruby.gemspec
156
+ homepage: https://github.com/decoch/notion-ruby
157
+ licenses:
158
+ - MIT
159
+ metadata:
160
+ allowed_push_host: https://rubygems.org
161
+ homepage_uri: https://github.com/decoch/notion-ruby
162
+ post_install_message:
163
+ rdoc_options: []
164
+ require_paths:
165
+ - lib
166
+ required_ruby_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: 2.7.0
171
+ required_rubygems_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ requirements: []
177
+ rubygems_version: 3.1.6
178
+ signing_key:
179
+ specification_version: 4
180
+ summary: Access Notion in ruby.
181
+ test_files: []