notion_rb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.rubocop.yml +1 -0
- data/.rubocop_todo.yml +14 -0
- data/.ruby-version +1 -0
- data/.travis.yml +14 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +115 -0
- data/LICENSE.txt +21 -0
- data/README.md +92 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/notion_rb.rb +31 -0
- data/lib/notion_rb/api/base.rb +56 -0
- data/lib/notion_rb/api/create.rb +74 -0
- data/lib/notion_rb/api/destroy.rb +41 -0
- data/lib/notion_rb/api/get.rb +58 -0
- data/lib/notion_rb/api/query_collection.rb +30 -0
- data/lib/notion_rb/api/restore.rb +42 -0
- data/lib/notion_rb/api/update.rb +45 -0
- data/lib/notion_rb/block.rb +97 -0
- data/lib/notion_rb/utils/block_cache.rb +58 -0
- data/lib/notion_rb/utils/converter.rb +23 -0
- data/lib/notion_rb/utils/parser.rb +100 -0
- data/lib/notion_rb/utils/types.rb +44 -0
- data/lib/notion_rb/utils/uuid_validator.rb +32 -0
- data/lib/notion_rb/version.rb +5 -0
- data/notion_rb.gemspec +41 -0
- metadata +216 -0
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module NotionRb
|
4
|
+
module Utils
|
5
|
+
class BlockCache
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@blocks = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def contains?(notion_id)
|
13
|
+
@blocks.any? { |block| block[:notion_id] == notion_id }
|
14
|
+
end
|
15
|
+
|
16
|
+
def find(notion_id)
|
17
|
+
add_block(notion_id) unless contains?(notion_id)
|
18
|
+
|
19
|
+
@blocks.find { |block| block[:notion_id] == notion_id }
|
20
|
+
rescue Mechanize::ResponseCodeError
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def children(notion_id)
|
25
|
+
return nil unless find(notion_id)
|
26
|
+
|
27
|
+
find(notion_id)[:children]
|
28
|
+
end
|
29
|
+
|
30
|
+
def clear
|
31
|
+
@blocks = []
|
32
|
+
end
|
33
|
+
|
34
|
+
def add_collection_view_blocks(block)
|
35
|
+
return unless block[:block_type] == 'collection_view_page'
|
36
|
+
|
37
|
+
metadata = block[:metadata]
|
38
|
+
query_collection = NotionRb::Api::QueryCollection.new(collection_id: metadata[:collection_id], view_id: metadata[:view_ids][0])
|
39
|
+
block[:children] = [metadata[:collection_id]]
|
40
|
+
add_blocks(query_collection.blocks)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def add_block(uuid)
|
46
|
+
add_blocks(NotionRb::Api::Get.new(notion_id: uuid).blocks)
|
47
|
+
end
|
48
|
+
|
49
|
+
def add_blocks(blocks)
|
50
|
+
new_block_ids = blocks.map { |block| block[:notion_id] }
|
51
|
+
@blocks.each do |block|
|
52
|
+
blocks << block unless new_block_ids.include?(block[:notion_id])
|
53
|
+
end
|
54
|
+
@blocks = blocks
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NotionRb
|
4
|
+
module Utils
|
5
|
+
class Converter
|
6
|
+
def initialize
|
7
|
+
@position = 0
|
8
|
+
end
|
9
|
+
|
10
|
+
def convert(value)
|
11
|
+
data = parse(value)
|
12
|
+
@position += 1
|
13
|
+
data
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def parse(value)
|
19
|
+
Parser.new(value, @position).parse
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NotionRb
|
4
|
+
module Utils
|
5
|
+
class Parser
|
6
|
+
include NotionRb::Utils::Types
|
7
|
+
|
8
|
+
def initialize(value, position)
|
9
|
+
unless valid_block_type?(value['type'])
|
10
|
+
raise ArgumentError, 'Invalid block type'
|
11
|
+
end
|
12
|
+
|
13
|
+
@value = value
|
14
|
+
@position = position
|
15
|
+
@parser = select_parser @value['type']
|
16
|
+
end
|
17
|
+
|
18
|
+
def parse
|
19
|
+
@parser == :null ? null : base
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def null
|
25
|
+
{}
|
26
|
+
end
|
27
|
+
|
28
|
+
def base
|
29
|
+
{
|
30
|
+
notion_id: @value['id'],
|
31
|
+
block_type: @value['type'],
|
32
|
+
title: @value.dig('properties', 'title', 0, 0),
|
33
|
+
parent_id: @value['parent_id'],
|
34
|
+
position: @position,
|
35
|
+
children: (@value['content'] || []),
|
36
|
+
metadata: send("metadata_#{@parser}".to_sym)
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
def metadata_base
|
41
|
+
{
|
42
|
+
properties: (@value.dig('properties') || {}),
|
43
|
+
color: (@value.dig('properties', 'title', 0, 1, 0, 1) || 'black'),
|
44
|
+
block_color: (@value.dig('format', 'block_color') || 'white')
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
def metadata_todo
|
49
|
+
metadata_base.merge(
|
50
|
+
checked: @value.dig('properties', 'checked', 0, 0) == 'Yes'
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
def metadata_code
|
55
|
+
metadata_base.merge(
|
56
|
+
language: @value.dig('properties', 'language', 0, 0)&.downcase
|
57
|
+
)
|
58
|
+
end
|
59
|
+
|
60
|
+
def metadata_embed
|
61
|
+
metadata_base.merge(
|
62
|
+
source: @value.dig('properties', 'source', 0, 0)
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
def metadata_bookmark
|
67
|
+
metadata_base.merge(
|
68
|
+
source: @value.dig('properties', 'link', 0, 0)
|
69
|
+
)
|
70
|
+
end
|
71
|
+
|
72
|
+
def metadata_callout
|
73
|
+
metadata_base.merge(
|
74
|
+
page_icon: @value.dig('format', 'page_icon')
|
75
|
+
)
|
76
|
+
end
|
77
|
+
|
78
|
+
def metadata_image
|
79
|
+
metadata_base.merge(
|
80
|
+
source: @value.dig('properties', 'source', 0, 0),
|
81
|
+
caption: @value.dig('properties', 'caption', 0, 0),
|
82
|
+
format: @value.dig('format')
|
83
|
+
)
|
84
|
+
end
|
85
|
+
|
86
|
+
def metadata_collection_view
|
87
|
+
{
|
88
|
+
collection_id: @value.dig('collection_id'),
|
89
|
+
view_ids: @value.dig('view_ids')
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
def metadata_collection
|
94
|
+
{
|
95
|
+
schema: @value.dig('schema')
|
96
|
+
}
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NotionRb
|
4
|
+
module Utils
|
5
|
+
module Types
|
6
|
+
TYPE_MAPPER = {
|
7
|
+
'collection' => :collection,
|
8
|
+
'collection_view_page' => :collection_view,
|
9
|
+
'column' => :base,
|
10
|
+
'column_list' => :base,
|
11
|
+
'divider' => :base,
|
12
|
+
'copy_indicator' => :null,
|
13
|
+
'table_of_contents' => :null,
|
14
|
+
'factory' => :null,
|
15
|
+
'page' => :base,
|
16
|
+
'header' => :base,
|
17
|
+
'bulleted_list' => :base,
|
18
|
+
'numbered_list' => :base,
|
19
|
+
'toggle' => :base,
|
20
|
+
'sub_header' => :base,
|
21
|
+
'sub_sub_header' => :base,
|
22
|
+
'quote' => :base,
|
23
|
+
'text' => :base,
|
24
|
+
'to_do' => :todo,
|
25
|
+
'code' => :code,
|
26
|
+
'image' => :image,
|
27
|
+
'file' => :embed,
|
28
|
+
'audio' => :embed,
|
29
|
+
'drive' => :embed,
|
30
|
+
'embed' => :embed,
|
31
|
+
'bookmark' => :bookmark,
|
32
|
+
'callout' => :callout
|
33
|
+
}.freeze
|
34
|
+
|
35
|
+
def valid_block_type?(value)
|
36
|
+
TYPE_MAPPER.key?(value)
|
37
|
+
end
|
38
|
+
|
39
|
+
def select_parser(value)
|
40
|
+
TYPE_MAPPER[value]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NotionRb
|
4
|
+
module Utils
|
5
|
+
module UuidValidator
|
6
|
+
def parse_uuid(url_or_uuid)
|
7
|
+
uuid = parse_from_url url_or_uuid
|
8
|
+
hex = uuid.delete('-')
|
9
|
+
|
10
|
+
parse_from_hex(hex)
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse_from_url(url)
|
14
|
+
if url.match? %r{https?://[\S]+}
|
15
|
+
url.split('/')[-1].split('?')[0].split('-')[-1][0..31]
|
16
|
+
else
|
17
|
+
url
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def parse_from_hex(hex)
|
22
|
+
raise ArgumentError, 'Incorrect hex size, must be equal to 32' unless hex.size == 32
|
23
|
+
|
24
|
+
[20, 16, 12, 8].each do |position|
|
25
|
+
hex = hex.to_s.insert(position, '-')
|
26
|
+
end
|
27
|
+
|
28
|
+
hex
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/notion_rb.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'notion_rb/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'notion_rb'
|
9
|
+
spec.version = NotionRb::VERSION
|
10
|
+
spec.authors = ['Tomas Gunther']
|
11
|
+
spec.email = ['tpgunther@uc.cl']
|
12
|
+
|
13
|
+
spec.summary = 'Unoficial integration with Notion'
|
14
|
+
spec.description = ''
|
15
|
+
spec.homepage = 'https://www.github.com/tpgunther/notion-rb'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
19
|
+
spec.metadata['source_code_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{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = 'exe'
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ['lib']
|
29
|
+
|
30
|
+
spec.add_runtime_dependency 'mechanize'
|
31
|
+
|
32
|
+
spec.add_development_dependency 'bundler', '~> 1.17.0'
|
33
|
+
spec.add_development_dependency 'byebug'
|
34
|
+
spec.add_development_dependency 'factory_bot'
|
35
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
36
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
37
|
+
spec.add_development_dependency 'rubocop'
|
38
|
+
spec.add_development_dependency 'single_cov'
|
39
|
+
spec.add_development_dependency 'vcr'
|
40
|
+
spec.add_development_dependency 'webmock'
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: notion_rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tomas Gunther
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-01-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mechanize
|
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: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.17.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.17.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: byebug
|
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: factory_bot
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
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: single_cov
|
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
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: vcr
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: webmock
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: ''
|
154
|
+
email:
|
155
|
+
- tpgunther@uc.cl
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- ".gitignore"
|
161
|
+
- ".rspec"
|
162
|
+
- ".rubocop.yml"
|
163
|
+
- ".rubocop_todo.yml"
|
164
|
+
- ".ruby-version"
|
165
|
+
- ".travis.yml"
|
166
|
+
- CODE_OF_CONDUCT.md
|
167
|
+
- Gemfile
|
168
|
+
- Gemfile.lock
|
169
|
+
- LICENSE.txt
|
170
|
+
- README.md
|
171
|
+
- Rakefile
|
172
|
+
- bin/console
|
173
|
+
- bin/setup
|
174
|
+
- lib/notion_rb.rb
|
175
|
+
- lib/notion_rb/api/base.rb
|
176
|
+
- lib/notion_rb/api/create.rb
|
177
|
+
- lib/notion_rb/api/destroy.rb
|
178
|
+
- lib/notion_rb/api/get.rb
|
179
|
+
- lib/notion_rb/api/query_collection.rb
|
180
|
+
- lib/notion_rb/api/restore.rb
|
181
|
+
- lib/notion_rb/api/update.rb
|
182
|
+
- lib/notion_rb/block.rb
|
183
|
+
- lib/notion_rb/utils/block_cache.rb
|
184
|
+
- lib/notion_rb/utils/converter.rb
|
185
|
+
- lib/notion_rb/utils/parser.rb
|
186
|
+
- lib/notion_rb/utils/types.rb
|
187
|
+
- lib/notion_rb/utils/uuid_validator.rb
|
188
|
+
- lib/notion_rb/version.rb
|
189
|
+
- notion_rb.gemspec
|
190
|
+
homepage: https://www.github.com/tpgunther/notion-rb
|
191
|
+
licenses:
|
192
|
+
- MIT
|
193
|
+
metadata:
|
194
|
+
homepage_uri: https://www.github.com/tpgunther/notion-rb
|
195
|
+
source_code_uri: https://www.github.com/tpgunther/notion-rb
|
196
|
+
post_install_message:
|
197
|
+
rdoc_options: []
|
198
|
+
require_paths:
|
199
|
+
- lib
|
200
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
201
|
+
requirements:
|
202
|
+
- - ">="
|
203
|
+
- !ruby/object:Gem::Version
|
204
|
+
version: '0'
|
205
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
206
|
+
requirements:
|
207
|
+
- - ">="
|
208
|
+
- !ruby/object:Gem::Version
|
209
|
+
version: '0'
|
210
|
+
requirements: []
|
211
|
+
rubyforge_project:
|
212
|
+
rubygems_version: 2.7.6.2
|
213
|
+
signing_key:
|
214
|
+
specification_version: 4
|
215
|
+
summary: Unoficial integration with Notion
|
216
|
+
test_files: []
|