schemic 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.rubocop.yml +183 -0
- data/.rvmrc +53 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +194 -0
- data/Rakefile +14 -0
- data/TODO +3 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/exe/schemic +15 -0
- data/lib/schemic/adapter/rails.rb +160 -0
- data/lib/schemic/adapter.rb +12 -0
- data/lib/schemic/crawler/cache.rb +77 -0
- data/lib/schemic/crawler/http.rb +163 -0
- data/lib/schemic/crawler/navigation.rb +125 -0
- data/lib/schemic/crawler/pipeline.rb +123 -0
- data/lib/schemic/crawler.rb +161 -0
- data/lib/schemic/generator/ruby.rb +41 -0
- data/lib/schemic/generator/yaml.rb +23 -0
- data/lib/schemic/generator.rb +21 -0
- data/lib/schemic/inflector.rb +66 -0
- data/lib/schemic/parser/callbacks.rb +71 -0
- data/lib/schemic/parser/cli/input.rb +62 -0
- data/lib/schemic/parser/cli/switches.rb +52 -0
- data/lib/schemic/parser/cli.rb +148 -0
- data/lib/schemic/parser/methods/selectors.rb +98 -0
- data/lib/schemic/parser/methods.rb +177 -0
- data/lib/schemic/parser/selection.rb +78 -0
- data/lib/schemic/parser/values.rb +122 -0
- data/lib/schemic/parser.rb +121 -0
- data/lib/schemic/version.rb +6 -0
- data/lib/schemic.rb +12 -0
- data/schemic.gemspec +44 -0
- data/tools/rubocop/compact_block_braces.rb +39 -0
- data/tools/rubocop/spacing.rb +96 -0
- metadata +279 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 0d68db29d42aa04118bdbc144693a7ee941fb997325b05be08edf420b2f339b0
|
|
4
|
+
data.tar.gz: f844452486f89d382c73385e16d4f5a8f4b1a2573e9b0b021d0f90fc5f9024bb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e1bf0bd5065aa82097a51f239019915cda1e47cfee600b67eb19985f0ce2b2d7e1b568bc010d1f189c503894b8a7578e124cc37d95927911d75a583522945e21
|
|
7
|
+
data.tar.gz: c2cbf171670f3b79facfab3b87f1cad59eec8a53de9e633c917f1d8ceb05877e6eb7cd263292c27d9cc2beda42e12af294a0f6d1db6350a70715de9539d73733
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
plugins:
|
|
2
|
+
- rubocop-performance
|
|
3
|
+
- rubocop-rspec
|
|
4
|
+
- rubocop-md
|
|
5
|
+
|
|
6
|
+
require:
|
|
7
|
+
- ./tools/rubocop/spacing.rb
|
|
8
|
+
- ./tools/rubocop/compact_block_braces.rb
|
|
9
|
+
|
|
10
|
+
AllCops:
|
|
11
|
+
TargetRubyVersion: 3.1
|
|
12
|
+
NewCops: enable
|
|
13
|
+
SuggestExtensions: false
|
|
14
|
+
Exclude:
|
|
15
|
+
- node_modules/**/*
|
|
16
|
+
- dist/**/*
|
|
17
|
+
- vendor/**/*
|
|
18
|
+
- .bundle/**/*
|
|
19
|
+
- tmp/**/*
|
|
20
|
+
- coverage/**/*
|
|
21
|
+
|
|
22
|
+
Style/DefWithParentheses:
|
|
23
|
+
Enabled: true
|
|
24
|
+
Style/MethodDefParentheses:
|
|
25
|
+
Enabled: true
|
|
26
|
+
EnforcedStyle: require_no_parentheses
|
|
27
|
+
Style/MethodCallWithArgsParentheses:
|
|
28
|
+
Enabled: true
|
|
29
|
+
EnforcedStyle: require_parentheses
|
|
30
|
+
IgnoreMacros: true
|
|
31
|
+
Layout/SpaceInsideParens:
|
|
32
|
+
Enabled: true
|
|
33
|
+
Layout/SpaceInsideHashLiteralBraces:
|
|
34
|
+
Enabled: true
|
|
35
|
+
EnforcedStyle: compact
|
|
36
|
+
Layout/IndentationWidth:
|
|
37
|
+
Enabled: true
|
|
38
|
+
Width: 3
|
|
39
|
+
Layout/MultilineAssignmentLayout:
|
|
40
|
+
Enabled: true
|
|
41
|
+
EnforcedStyle: new_line
|
|
42
|
+
Layout/TrailingWhitespace:
|
|
43
|
+
Enabled: true
|
|
44
|
+
Layout/TrailingEmptyLines:
|
|
45
|
+
Enabled: true
|
|
46
|
+
Layout/EmptyLines:
|
|
47
|
+
Enabled: true
|
|
48
|
+
|
|
49
|
+
Naming/PredicatePrefix:
|
|
50
|
+
Enabled: true
|
|
51
|
+
NamePrefix: [is_, has_, have_, does_, can_, should_]
|
|
52
|
+
ForbiddenPrefixes: []
|
|
53
|
+
Naming/MethodParameterName:
|
|
54
|
+
MinNameLength: 1
|
|
55
|
+
Naming/RescuedExceptionsVariableName:
|
|
56
|
+
PreferredName: error
|
|
57
|
+
Naming/MemoizedInstanceVariableName:
|
|
58
|
+
EnforcedStyleForLeadingUnderscores: optional
|
|
59
|
+
Naming/PredicateMethod:
|
|
60
|
+
AllowedMethods: [call, apply_to]
|
|
61
|
+
Metrics/MethodLength:
|
|
62
|
+
Max: 12
|
|
63
|
+
Metrics/ModuleLength:
|
|
64
|
+
Max: 120
|
|
65
|
+
Metrics/PerceivedComplexity:
|
|
66
|
+
Max: 9
|
|
67
|
+
Layout/SpaceInsideBlockBraces:
|
|
68
|
+
# Replaced by the same checks with adjacent closing braces collapsed.
|
|
69
|
+
Enabled: false
|
|
70
|
+
Tiun/CompactBlockBraces:
|
|
71
|
+
Enabled: true
|
|
72
|
+
EnforcedStyle: space
|
|
73
|
+
SupportedStyles: [space, no_space]
|
|
74
|
+
EnforcedStyleForEmptyBraces: no_space
|
|
75
|
+
SpaceBeforeBlockParameters: false
|
|
76
|
+
Style/ClassAndModuleChildren:
|
|
77
|
+
EnforcedStyle: compact
|
|
78
|
+
Gemspec/DevelopmentDependencies:
|
|
79
|
+
EnforcedStyle: gemspec
|
|
80
|
+
|
|
81
|
+
# Preserve the project's expression grouping and reduce-based context helpers.
|
|
82
|
+
Lint/AmbiguousOperatorPrecedence:
|
|
83
|
+
Enabled: false
|
|
84
|
+
Lint/AmbiguousRange:
|
|
85
|
+
Enabled: false
|
|
86
|
+
Layout/MultilineOperationIndentation:
|
|
87
|
+
Enabled: false
|
|
88
|
+
Style/EachWithObject:
|
|
89
|
+
Enabled: false
|
|
90
|
+
|
|
91
|
+
# Match RuboCop's treatment of spec/test helpers for the Cucumber suite.
|
|
92
|
+
Metrics:
|
|
93
|
+
Exclude:
|
|
94
|
+
- features/**/*
|
|
95
|
+
|
|
96
|
+
# OpenStruct is the schema representation accepted by rdoba and the parser.
|
|
97
|
+
Style/OpenStructUse:
|
|
98
|
+
Exclude:
|
|
99
|
+
- lib/schemic/parser/methods.rb
|
|
100
|
+
- lib/schemic/parser/cli.rb
|
|
101
|
+
- features/step_definitions/calendar_extraction_steps.rb
|
|
102
|
+
|
|
103
|
+
Tiun/AssignmentSpacing:
|
|
104
|
+
Description: Separate assignment groups from other statements by an empty line.
|
|
105
|
+
Enabled: true
|
|
106
|
+
Include:
|
|
107
|
+
- lib/**/*.rb
|
|
108
|
+
Tiun/ReturnSpacing:
|
|
109
|
+
Description: Add an empty line before a separate final expression in a multiline method.
|
|
110
|
+
Enabled: true
|
|
111
|
+
Include:
|
|
112
|
+
- lib/**/*.rb
|
|
113
|
+
|
|
114
|
+
# Ruby extraction schemas also use context; they are not RSpec example groups.
|
|
115
|
+
RSpec:
|
|
116
|
+
Exclude:
|
|
117
|
+
- features/fixtures/**/*
|
|
118
|
+
Include:
|
|
119
|
+
- "**/*_spec.rb"
|
|
120
|
+
- "**/spec/**/*"
|
|
121
|
+
- features/**/*.rb
|
|
122
|
+
|
|
123
|
+
Markdown:
|
|
124
|
+
WarnInvalid: true
|
|
125
|
+
Autodetect: true
|
|
126
|
+
|
|
127
|
+
# Include the opt-in extension rules as requested.
|
|
128
|
+
|
|
129
|
+
Performance/ArraySemiInfiniteRangeSlice:
|
|
130
|
+
Enabled: true
|
|
131
|
+
|
|
132
|
+
Performance/BigDecimalWithNumericArgument:
|
|
133
|
+
Enabled: true
|
|
134
|
+
|
|
135
|
+
Performance/BlockGivenWithExplicitBlock:
|
|
136
|
+
Enabled: true
|
|
137
|
+
|
|
138
|
+
Performance/CaseWhenSplat:
|
|
139
|
+
Enabled: true
|
|
140
|
+
|
|
141
|
+
Performance/Casecmp:
|
|
142
|
+
Enabled: true
|
|
143
|
+
|
|
144
|
+
Performance/ChainArrayAllocation:
|
|
145
|
+
Enabled: true
|
|
146
|
+
|
|
147
|
+
Performance/IoReadlines:
|
|
148
|
+
Enabled: true
|
|
149
|
+
|
|
150
|
+
Performance/OpenStruct:
|
|
151
|
+
Enabled: true
|
|
152
|
+
Exclude:
|
|
153
|
+
- lib/schemic/parser/methods.rb
|
|
154
|
+
- lib/schemic/parser/cli.rb
|
|
155
|
+
|
|
156
|
+
Performance/SelectMap:
|
|
157
|
+
Enabled: true
|
|
158
|
+
|
|
159
|
+
RSpec/AlignLeftLetBrace:
|
|
160
|
+
Enabled: true
|
|
161
|
+
|
|
162
|
+
RSpec/AlignRightLetBrace:
|
|
163
|
+
Enabled: true
|
|
164
|
+
|
|
165
|
+
RSpec/DescribedClassModuleWrapping:
|
|
166
|
+
Enabled: true
|
|
167
|
+
|
|
168
|
+
RSpec/Dialect:
|
|
169
|
+
Enabled: true
|
|
170
|
+
|
|
171
|
+
RSpec/MessageExpectation:
|
|
172
|
+
Enabled: true
|
|
173
|
+
|
|
174
|
+
RSpec/Pending:
|
|
175
|
+
Enabled: true
|
|
176
|
+
|
|
177
|
+
Markdown/Syntax:
|
|
178
|
+
Enabled: true
|
|
179
|
+
|
|
180
|
+
# Proc arity controls which callback arguments the parser supplies.
|
|
181
|
+
# Symbol#to_proc has a different arity and is not an equivalent replacement.
|
|
182
|
+
Style/SymbolProc:
|
|
183
|
+
AllowedMethods: [define_method, proc, lambda]
|
data/.rvmrc
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#!/bin/bash -l
|
|
2
|
+
|
|
3
|
+
set -x
|
|
4
|
+
gemruby=$(cat Gemfile|grep ^ruby)
|
|
5
|
+
if [[ "$gemruby" == *engine* ]]; then
|
|
6
|
+
ruby_type=$(echo "$gemruby"|sed "s/.*engine[: ][^\"']\+.\([^\"']\+\).*/\1/")
|
|
7
|
+
ruby_version=$(echo "$gemruby"|sed "s/.*engine_version[: ][^\"']\+.\([^\"']\+\).*/\1/")
|
|
8
|
+
fi
|
|
9
|
+
|
|
10
|
+
if [ -z "$ruby_version" -o -z "$ruby_type" ]; then
|
|
11
|
+
set -
|
|
12
|
+
info=$(rvm info)
|
|
13
|
+
set -x
|
|
14
|
+
ruby_type=$(echo "$info"|grep "^ ruby:" -A 6|grep " interpreter"|sed "s/[^\"]*\"\([^\"]*\)\"/\1/")
|
|
15
|
+
|
|
16
|
+
ruby_version=$(echo "$info"|grep "^ ruby:" -A 6|grep " version"|sed "s/[^\"]*\"\([^\"]*\)\"/\1/")
|
|
17
|
+
if [ -z "$ruby_version" ]; then
|
|
18
|
+
ruby_path=$(echo "$info"|grep "^ homes:" -A 2|grep " ruby:"|sed "s/[^\"]*\"\([^\"]*\)\"/\1/")
|
|
19
|
+
ruby_type=$(basename "$ruby_path"|sed "s/^\([^\[]\+\)-.*/\1/")
|
|
20
|
+
ruby_version=$(basename "$ruby_path"|sed "s/.*-\([^\[]\+\)/\1/")
|
|
21
|
+
fi
|
|
22
|
+
fi
|
|
23
|
+
|
|
24
|
+
ruby_gemset=$(basename $(pwd))
|
|
25
|
+
if [ "$ruby_version" = 'not set' ]; then
|
|
26
|
+
# selecting first ruby
|
|
27
|
+
set -
|
|
28
|
+
list=$(rvm list)
|
|
29
|
+
set -x
|
|
30
|
+
ruby_type=$(echo "$list"|grep rubies -A 2|grep "\[.*\]"|sed "s/.* \([^\[]\+\)-.*/\1/")
|
|
31
|
+
ruby_version=$(echo "$list"|grep rubies -A 2|grep "\[.*\]"|sed "s/.*-\([^\[]\+\)\[.*/\1/")
|
|
32
|
+
fi
|
|
33
|
+
|
|
34
|
+
if [ -z "$(cat ~/.rvm/config/known_strings|grep ${ruby_type}-$ruby_version)" ]; then
|
|
35
|
+
echo ${ruby_type}-$ruby_version >> ~/.rvm/config/known_strings
|
|
36
|
+
fi
|
|
37
|
+
echo will use ${ruby_type}-$ruby_version@$ruby_gemset with create flag
|
|
38
|
+
set -
|
|
39
|
+
rvm use ${ruby_type}-$ruby_version@$ruby_gemset --create
|
|
40
|
+
|
|
41
|
+
set -x
|
|
42
|
+
if [ -z "$(echo "$PATH"|grep "$(pwd)/.bundle/bin")" ]; then
|
|
43
|
+
if [ -z "$_ORIGINAL_GEM_PATH" ]; then
|
|
44
|
+
export _ORIGINAL_GEM_PATH=$GEM_PATH
|
|
45
|
+
fi
|
|
46
|
+
export PATH=$(pwd)/bin:$(pwd)/.bundle/bin:$PATH
|
|
47
|
+
export GEM_HOME=$(pwd)/.bundle
|
|
48
|
+
export GEM_PATH=$GEM_HOME:$GEM_PATH
|
|
49
|
+
bundler=$(gem which bundler)
|
|
50
|
+
export RUBYLIB=$(dirname $bundler)
|
|
51
|
+
fi
|
|
52
|
+
set -
|
|
53
|
+
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Contributor Code of Conduct
|
|
2
|
+
|
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
|
7
|
+
|
|
8
|
+
We are committed to making participation in this project a harassment-free
|
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
|
12
|
+
|
|
13
|
+
Examples of unacceptable behavior by participants include:
|
|
14
|
+
|
|
15
|
+
* The use of sexualized language or imagery
|
|
16
|
+
* Personal attacks
|
|
17
|
+
* Trolling or insulting/derogatory comments
|
|
18
|
+
* Public or private harassment
|
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
|
20
|
+
addresses, without explicit permission
|
|
21
|
+
* Other unethical or unprofessional conduct
|
|
22
|
+
|
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
27
|
+
threatening, offensive, or harmful.
|
|
28
|
+
|
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
|
32
|
+
Conduct may be permanently removed from the project team.
|
|
33
|
+
|
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
|
35
|
+
when an individual is representing the project or its community.
|
|
36
|
+
|
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
38
|
+
reported by contacting a project maintainer at majioa@yandex.ru. All
|
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
|
42
|
+
incident.
|
|
43
|
+
|
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
45
|
+
version 1.3.0, available at
|
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
|
47
|
+
|
|
48
|
+
[homepage]: http://contributor-covenant.org
|
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
source 'https://rubygems.org'
|
|
4
|
+
|
|
5
|
+
# Specify your gem's dependencies in schemic.gemspec
|
|
6
|
+
gemspec
|
|
7
|
+
|
|
8
|
+
# gem "rdoba", path: "/usr/local/home/majioa/git/rdoba"
|
|
9
|
+
gem 'rspec-json_expectations', git: 'https://github.com/majioa/rspec-json_expectations.git',
|
|
10
|
+
branch: 'remove_dep_to_rspec'
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 Malo Skrylevo
|
|
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,194 @@
|
|
|
1
|
+
# Schemic
|
|
2
|
+
|
|
3
|
+
Collect XML/HTML sources, cache their original bytes, and extract Ruby hashes or YAML
|
|
4
|
+
using a Ruby DSL or a YAML schema.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```ruby
|
|
9
|
+
gem 'schemic'
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Modules
|
|
13
|
+
|
|
14
|
+
`require 'schemic'` loads the namespace and version; modules load on first use.
|
|
15
|
+
You can also require their paths individually.
|
|
16
|
+
|
|
17
|
+
| Module | Require path | Responsibility |
|
|
18
|
+
| --- | --- | --- |
|
|
19
|
+
| `Schemic::Parser` | `schemic/parser` | Extracts hashes from XML/HTML and reports document and field errors. |
|
|
20
|
+
| `Schemic::Parser::Methods` | `schemic/parser` | Defines Ruby extraction schemas: fields, nested records, selectors and callbacks. |
|
|
21
|
+
| `Schemic::Generator` | `schemic/generator` | Loads a YAML schema or runs a trusted Ruby schema generator. |
|
|
22
|
+
| `Schemic::Generator::YAML` | `schemic/generator/yaml` | Loads a YAML extraction schema for the parser. |
|
|
23
|
+
| `Schemic::Crawler` | `schemic/crawler` | Collects seed URLs and selected links inside configured source boundaries. |
|
|
24
|
+
| `Schemic::Crawler::HTTP` | `schemic/crawler` | Delivers HTTP responses with TLS, proxy support, retries and size limits. |
|
|
25
|
+
| `Schemic::Crawler::Cache` | `schemic/crawler` | Stores original response bytes, metadata and checksums for offline replay. |
|
|
26
|
+
| `Schemic::Crawler::Pipeline` | `schemic/crawler` | Converts a complete collection into records and atomically publishes YAML. |
|
|
27
|
+
| `Schemic::Parser::CLI` | `schemic/parser/cli` | Implements the `schemic` command for collection and extraction. |
|
|
28
|
+
| `Schemic::Adapter::Rails` | `schemic/adapter/rails` | Provides the existing model-import mixin for applications that supply reference lookup helpers. |
|
|
29
|
+
|
|
30
|
+
`Schemic::VERSION` is the gem version. Applications own source-specific schemas,
|
|
31
|
+
seed URLs and database bindings.
|
|
32
|
+
|
|
33
|
+
## Ruby schemas
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
require 'schemic'
|
|
37
|
+
|
|
38
|
+
class CalendarPage
|
|
39
|
+
extend Schemic::Parser::Methods
|
|
40
|
+
|
|
41
|
+
context '.calendar' do
|
|
42
|
+
has_field :title, from: 'h1', required: true
|
|
43
|
+
has_field :date, from: 'time', on_complete: proc {|text| text.strip }
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
parser = Schemic::Parser.new(CalendarPage.generate)
|
|
48
|
+
tree = parser.parse('<div class="calendar"><h1>Annunciation</h1><time>25.03</time></div>', format: :html)
|
|
49
|
+
# => {"title" => "Annunciation", "date" => "25.03"}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Save the generator class in a trusted `.rb` file to load it with
|
|
53
|
+
`Schemic::Generator.load('calendar.rb')` or `--scheme-file=calendar.rb`.
|
|
54
|
+
The file must define exactly one top-level class or module with a public
|
|
55
|
+
`generate` method. The DSL provides this method and returns the schema as an
|
|
56
|
+
`OpenStruct`. Other schema files are read as YAML.
|
|
57
|
+
|
|
58
|
+
`parse(source, format: :xml)` accepts a string, File/IO or a Nokogiri node.
|
|
59
|
+
The default format is `:xml`; use `:html` for HTML recovery. An unsupported
|
|
60
|
+
format raises `Schemic::Parser::ParameterError`. Parsing does not load external
|
|
61
|
+
network entities.
|
|
62
|
+
|
|
63
|
+
## Fields and nested schemas
|
|
64
|
+
|
|
65
|
+
- `has_field(name, options)` extracts a field. `from` is a CSS selector or an
|
|
66
|
+
ordered array of alternatives. The first nonempty result wins; transformed
|
|
67
|
+
`0` and `false` are valid results.
|
|
68
|
+
- `context(selector) { ... }` scopes the selectors in its block. Nested contexts
|
|
69
|
+
combine with explicit field contexts; exceptions restore the enclosing scope.
|
|
70
|
+
- `has_scheme(name, options)` extracts one nested hash; `has_schemes` extracts an
|
|
71
|
+
array under the plural name. Define its fields with `scheme(name) { ... }`.
|
|
72
|
+
`as` selects another schema; `postfix: 'attributes'` appends a suffix to its key.
|
|
73
|
+
Standalone Schemic adds `es` after final `o`, or `s` when the `o` follows
|
|
74
|
+
`[aeiouy]`. Both `-os` and `-oes` resolve to `-o`.
|
|
75
|
+
The pairs `-en`/`-ina`, `-us`/`-i` and `-um`/`-a` also work in both
|
|
76
|
+
directions. Names ending in `-ea` use the English rule, as in `area`/`areas`.
|
|
77
|
+
In Rails the application's loaded inflection rules take precedence.
|
|
78
|
+
- `required: true` reports an error for a missing or empty value. Zero, false
|
|
79
|
+
and structures containing those values are not empty.
|
|
80
|
+
- `on_proceed` handles selected nodes; `on_complete` handles the resulting field.
|
|
81
|
+
Callbacks accept Proc/Method objects, or a string/symbol naming a parser method.
|
|
82
|
+
Ruby DSL symbol callbacks can also refer to methods of the schema builder.
|
|
83
|
+
- `@` selects from the document root, `^` moves to a parent, and a standalone
|
|
84
|
+
`.` is a placeholder for the field name. Dots in CSS class selectors remain
|
|
85
|
+
literal dots.
|
|
86
|
+
|
|
87
|
+
## YAML schemas and output
|
|
88
|
+
|
|
89
|
+
```ruby
|
|
90
|
+
require 'schemic/generator/yaml'
|
|
91
|
+
require 'schemic/parser'
|
|
92
|
+
require 'yaml'
|
|
93
|
+
|
|
94
|
+
scheme = Schemic::Generator::YAML.load_from('calendar-schema.yaml')
|
|
95
|
+
parser = Schemic::Parser.new(scheme)
|
|
96
|
+
tree = parser.parse(File.open('calendar.html'), format: :html)
|
|
97
|
+
yaml = tree.to_yaml if parser.errors.empty?
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The schema document describes extraction; `tree.to_yaml` serializes the extracted
|
|
101
|
+
data. Parsing does not import the result into a database.
|
|
102
|
+
|
|
103
|
+
## Errors
|
|
104
|
+
|
|
105
|
+
`parser.errors` is reset for each call to `parse`. Field errors map field names
|
|
106
|
+
to arrays of messages. Malformed XML returns an empty tree and records syntax
|
|
107
|
+
errors under `:document`; HTML mode permits ordinary HTML recovery. Required-field
|
|
108
|
+
errors may accompany a partial tree, so check `errors.empty?` before importing or
|
|
109
|
+
publishing extracted data.
|
|
110
|
+
|
|
111
|
+
## Collecting and replaying pages
|
|
112
|
+
|
|
113
|
+
```ruby
|
|
114
|
+
require 'schemic/crawler'
|
|
115
|
+
|
|
116
|
+
cache = Schemic::Crawler::Cache.new('tmp/raw-calendar')
|
|
117
|
+
crawler = Schemic::Crawler.new(
|
|
118
|
+
origins: ['https://calendar.example'],
|
|
119
|
+
path_prefix: '/calendar',
|
|
120
|
+
cache: cache,
|
|
121
|
+
max_pages: 100,
|
|
122
|
+
delay: 1
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
pages = crawler.each_page(['https://calendar.example/calendar/march'], links: 'a.next')
|
|
126
|
+
pipeline = Schemic::Crawler::Pipeline.new(parser: parser, format: :html, collection: 'memoes')
|
|
127
|
+
records = pipeline.write(pages, output: 'calendar.yaml')
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The application supplies the extraction schema and seed URLs (including any date
|
|
131
|
+
range). `links:` optionally follows a CSS selection of links in breadth-first
|
|
132
|
+
order. Fragment-only differences are deduplicated; query parameters remain part
|
|
133
|
+
of page identity. `origins`, `path_prefix` and `max_pages` bound collection.
|
|
134
|
+
For frame-based indexes, `links: { 'a[href]' => 'href', 'frame[src]' => 'src' }`
|
|
135
|
+
follows both kinds of references; HTML `base` URLs are respected.
|
|
136
|
+
Discovered external links are skipped; out-of-scope seeds and redirects raise
|
|
137
|
+
`Schemic::Crawler::ScopeError`.
|
|
138
|
+
|
|
139
|
+
`fetch(url)` uses an existing cached response. `refresh: true` revalidates with
|
|
140
|
+
ETag/Last-Modified; `offline: true` requires the cache and makes no HTTP request.
|
|
141
|
+
The two options cannot be combined. The cache atomically stores response bytes,
|
|
142
|
+
effective URL, headers, fetch time and a SHA-256 checksum. Missing or corrupt
|
|
143
|
+
offline entries raise `CacheMiss`. Failed refreshes preserve the previous page.
|
|
144
|
+
|
|
145
|
+
`Schemic::Crawler::HTTP.new(timeout: 15, retries: 2, redirects: 5,
|
|
146
|
+
max_bytes: 10 * 1024 * 1024)` configures delivery. Retries apply to network
|
|
147
|
+
failures and HTTP 429/502/503/504; pauses also apply to retries and redirects.
|
|
148
|
+
Delivery inherits the process HTTP proxy and verified TLS configuration.
|
|
149
|
+
Non-successful delivery raises `FetchError` without publishing an import bundle.
|
|
150
|
+
|
|
151
|
+
`Pipeline#records(pages)` returns complete records. Without `collection`, each
|
|
152
|
+
page produces one record; with it, the extracted top-level key must contain a
|
|
153
|
+
nonempty array of hashes. Any failed page raises `Pipeline::ExtractionError`,
|
|
154
|
+
whose `errors` maps URLs to parser diagnostics. `write` replaces output only
|
|
155
|
+
after the full collection succeeds. Raw pages remain available for correction
|
|
156
|
+
and offline replay. HTTP charset declarations, including CP866, are honored
|
|
157
|
+
without modifying the cached bytes.
|
|
158
|
+
|
|
159
|
+
## Command line
|
|
160
|
+
|
|
161
|
+
Fetch raw pages independently of extraction:
|
|
162
|
+
|
|
163
|
+
```sh
|
|
164
|
+
schemic --url=https://calendar.example/calendar/march \
|
|
165
|
+
--path-prefix=/calendar --links='a.next' --cache=tmp/raw --fetch-only
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Convert the saved collection using a YAML extraction schema:
|
|
169
|
+
|
|
170
|
+
```sh
|
|
171
|
+
schemic --url=https://calendar.example/calendar/march \
|
|
172
|
+
--path-prefix=/calendar --links='a.next' --cache=tmp/raw --offline \
|
|
173
|
+
--scheme-file=calendar-schema.yaml --collection=memoes --output=calendar.yaml
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Repeat `--url` for explicit seeds. Supply local documents as positional
|
|
177
|
+
filenames or with repeated `--input=FILE`. Use one input mode per command,
|
|
178
|
+
either URLs or local files. If you combine both ways to specify local files,
|
|
179
|
+
`--input` files are read before positional files.
|
|
180
|
+
|
|
181
|
+
```sh
|
|
182
|
+
schemic --scheme-file=calendar.rb --format=xml march.xml april.xml
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
With positional filenames and no `--output`, the command prints the extracted
|
|
186
|
+
records as JSON under `records`. Supply `--output` to write YAML and print a
|
|
187
|
+
JSON summary with the record count and output path. Commands that supply files
|
|
188
|
+
solely through `--input` still require `--output`.
|
|
189
|
+
|
|
190
|
+
`--format=xml` selects strict XML. HTML is the CLI default. Delivery options
|
|
191
|
+
are `--max-pages`, `--delay`, `--timeout` and `--retries`. Use `--refresh`
|
|
192
|
+
to refresh the cache. The command exits nonzero on failure. Failed collection
|
|
193
|
+
or extraction preserves existing YAML output. The CLI does not write to a
|
|
194
|
+
database.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'cucumber/rake/task'
|
|
5
|
+
require 'bundler/gem_tasks'
|
|
6
|
+
require 'rubocop/rake_task'
|
|
7
|
+
|
|
8
|
+
RuboCop::RakeTask.new(:rubocop)
|
|
9
|
+
|
|
10
|
+
Cucumber::Rake::Task.new do |t|
|
|
11
|
+
t.cucumber_opts = %w[--format progress]
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
task default: :cucumber
|
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 'schemic'
|
|
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
|
data/bin/setup
ADDED
data/exe/schemic
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'schemic/parser/cli'
|
|
5
|
+
require 'json'
|
|
6
|
+
|
|
7
|
+
begin
|
|
8
|
+
result = Schemic::Parser::CLI.new(ARGV).run
|
|
9
|
+
puts(JSON.generate(result)) if result.is_a?(Hash)
|
|
10
|
+
rescue Schemic::Crawler::Error, Schemic::Parser::ParameterError, Schemic::Parser::InvalidSchemeError, OptionParser::ParseError,
|
|
11
|
+
ArgumentError, Psych::Exception, SystemCallError => error
|
|
12
|
+
warn(error.message)
|
|
13
|
+
warn(error.errors.inspect) if error.respond_to?(:errors)
|
|
14
|
+
exit(1)
|
|
15
|
+
end
|