eatr 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 723534e02fafa057740113e05c2a05a57dca3f4f
4
+ data.tar.gz: 51a0cbf659f1686797df1e77bbf4ca7bd2edb8c0
5
+ SHA512:
6
+ metadata.gz: 15920883e030fc4c6ffae98dc92358bff41af91a702b482a38e879abfff20be560fe47719e1261f5ac6692f722ab48afa225734e6d04f2078b2f337ae21eb11f
7
+ data.tar.gz: 0824a84729c1ef1da495bd62d497757d1e5090695a839d823b8ad545dc9ad3d9ca09caed27d3089393607ab51c86a3a04d65beedd92c739b6464d3a60207bdbc
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.13.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at greggroth@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in eatr.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Greggory Rothmeier
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,426 @@
1
+ # Eatr
2
+
3
+ Configuration-based XML and CSV document parsing and transformation library.
4
+ Define structs in YAML configuration files and parse documents to create and
5
+ populate the structs.
6
+
7
+ ## Table of Contents
8
+
9
+ * [Installation](#installation)
10
+ * [Parsing Documents](#parsing-documents)
11
+ * [Schema Fields Reference](#schema-fields-reference)
12
+ * [Common Header Attributes](#common-header-attributes)
13
+ * [Fields for `Eatr::Csv::Document` schemas](#fields-for-eatrcsvdocument-schemas)
14
+ * [Fields for `Eatr::Xml::Document` schemas](#fields-for-eatrxmldocument-schemas)
15
+ * [Transformation Pipeline](#transformation-pipeline)
16
+ * [Helpful Tools](#helpful-tools)
17
+ * [`Eatr::Xml::SchemaGenerator`](#eatrxmlschemagenerator)
18
+ * [`Eatr::Sql::TableGenerator`](#eatrsqltablegenerator)
19
+ * [`Eatr::DotGenerator`](#eatrdotgenerator)
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem 'eatr'
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ $ bundle
32
+
33
+ Or install it yourself as:
34
+
35
+ $ gem install eatr
36
+
37
+ ## Parsing Documents
38
+
39
+ The library supports creating a collection of struct objects of arbirary
40
+ cardinality. For example, if you're interested in capturing all the chapters
41
+ from an XML representation of a book, but also want to capture higher-level
42
+ keys such as the author:
43
+
44
+ ```xml
45
+ <book>
46
+ <author>
47
+ <firstName>greggroth</firstName>
48
+ </author>
49
+ <publishedAt>2016-11-12T8:00:00Z</publishedAt>
50
+ <rating>8.9</rating>
51
+ <pages>120</pages>
52
+ <chapters>
53
+ <chapter>
54
+ <title>Ch 1</title>
55
+ </chapter>
56
+ <chapter>
57
+ <title>Ch 2</title>
58
+ </chapter>
59
+ </chapters>
60
+ </book>
61
+ ```
62
+
63
+ You can use a schema definition like:
64
+
65
+ ```yaml
66
+ name: chapters
67
+ fields:
68
+ - name: author
69
+ xpath: //author/firstName
70
+ type: string
71
+ - node: chapters
72
+ xpath: //chapters/chapter
73
+ children:
74
+ - name: title
75
+ xpath: ./title
76
+ type: string
77
+ ```
78
+
79
+ Here is an example from the test suite of using this XML and schema defintion:
80
+
81
+ ```ruby
82
+ > chapters = Eatr::Xml::Document.new('./spec/fixtures/schema/chapters.yaml').parse('./spec/fixtures/xml/book.xml')
83
+ => [#<struct Struct::Chapters author="greggroth", title="Ch 1">,
84
+ #<struct Struct::Chapters author="greggroth", title="Ch 2">]
85
+ ```
86
+
87
+ ### Schema Fields Reference
88
+
89
+ #### Common Header Attributes
90
+
91
+ - `name`
92
+ - Name of the document this schema represents
93
+ - `table_name`
94
+ - optional -- defaults to `name`
95
+ - Name of the table in the `CREATE TABLE` statement generated by `Eatr::Sql::TableGenerator`
96
+ - `remove_namespaces`
97
+ - optional -- defaults to `false`
98
+ - only apply to XML documents and configures Whether XML namespaces ought to
99
+ be removed before applying XPaths.
100
+
101
+ `input_field` attributes:
102
+
103
+ - `name`
104
+ - required
105
+ - Name of the field to be used as the `attr_accessor` in the destination struct.
106
+ - `type`
107
+ - optional -- defaults to `string`
108
+ - Can be
109
+ - `boolean`
110
+ - `float`
111
+ - `integer`
112
+ - `string`
113
+ - `timestamp`
114
+ - `required`
115
+ - optional -- defaults to `true`
116
+ - If a node cannot be found at the given `xpath`, an `Eatr::NodeNotFound` error is raised.
117
+ - `value`
118
+ - optional -- superceeds `xpath` or `csv_header` values
119
+ - If set, this value will be used as the attribute's value
120
+ - `strptime`
121
+ - optional -- only applicable if `type` is `timestamp`
122
+ - Format string used to parse the string into a `DateTime` object
123
+ - `max_length`
124
+ - optional -- only applicable if `type` is `string`
125
+ - Truncate the string after `max_length` characters
126
+ - `length`
127
+ - optional -- only applicable if `type` is `string`
128
+ - Truncate the string after `length` characters
129
+
130
+ #### Fields for `Eatr::Csv::Document` schemas
131
+
132
+ - `csv_header`
133
+ - required
134
+ - Name of header the field is expected to be under
135
+
136
+ #### Fields for `Eatr::Xml::Document` schemas
137
+
138
+ - `xpath`
139
+ - required
140
+ - Path to the object in the XML document that should be used to populate this field
141
+
142
+ Node field attributes:
143
+
144
+ - `node`
145
+ - optional
146
+ - Name to describe this collection of nodes
147
+ - `xpath`
148
+ - required
149
+ - Indicates the collection of children documents to be passed to the `chilren` field definitions
150
+ - `children`:
151
+ - required
152
+ - Collection of normal field definitions, except their `xpath` is relative to the child document defined per the `xpath` of the parent node.
153
+
154
+ ## Transformation Pipeline
155
+
156
+ The transformation options provided are lightweight, but very flexible. A
157
+ class `Eatr::Pipeline` is used to execute a set of transformations, which are
158
+ simply any object that can be initialized with a hash and responds to `#call`
159
+ with a collection of structs.
160
+
161
+ For example, if you wanted to add a date ID field generated from a timestamp, the schema file should add a `transformations` key:
162
+
163
+ ```yaml
164
+ transformations:
165
+ - class: Eatr::Transformation::AddDateId
166
+ args:
167
+ source: published_at
168
+ destination: published_at_date_id
169
+ ```
170
+
171
+ The `transformations` key should point to an array of objects with at least a `class` key. The `class` is the transformer's class name and can be anything that can be looked up using `Object.const_get`. The `args` key is optional and is passed to `initialize`.
172
+
173
+ Following is an example of loading a schema file, creating an array of `Book`
174
+ struct objects, and applying the transformation defined in the schema.
175
+
176
+ ```ruby
177
+ > document = Eatr::Xml::Document.new('./spec/fixtures/schema/book.yaml')
178
+ > books = document.parse('./spec/fixtures/xml/book.xml')
179
+ => [#<struct Struct::Book
180
+ id=1,
181
+ author="greggroth",
182
+ library_id=nil,
183
+ pages=120,
184
+ for_sale=false,
185
+ published_at=#<DateTime: 2016-11-12T08:00:00+00:00 ((2457705j,28800s,0n),+0s,2299161j)>,
186
+ published_at_date_id=nil,
187
+ rating=8.9,
188
+ icbn=nil,
189
+ summary="In this lovely ",
190
+ age=12>]
191
+ > document.schema.transformation_pipeline.call(books)
192
+ => [#<struct Struct::Book
193
+ id=1,
194
+ author="greggroth",
195
+ library_id=nil,
196
+ pages=120,
197
+ for_sale=false,
198
+ published_at=#<DateTime: 2016-11-12T08:00:00+00:00 ((2457705j,28800s,0n),+0s,2299161j)>,
199
+ published_at_date_id=20161112,
200
+ rating=8.9,
201
+ icbn=nil,
202
+ summary="In this lovely ",
203
+ age=12>]
204
+ ```
205
+
206
+ This library only provides the `AddDateId` transformer, however you can create
207
+ your own transformation classes. See the source for `AddDateId` for reference
208
+ on how a transformer can be set up.
209
+
210
+ ## Helpful Tools
211
+
212
+ Included are a few two helper classes:
213
+ - [`Eatr::Xml::SchemaGenerator`](#eatrxmlschemagenerator) creates a rough schema file from a sample XML file.
214
+ - [`Eatr::Sql::TableGenerator`](#eatrsqltablegenerator) creates a SQL `CREATE TABLE` statement from a schema file.
215
+ - [`Eatr::DotGenerator`](#eatrdotgenerator) creates a Graphviz DOT file for visualizing schema relationships (experimental).
216
+
217
+ ### `Eatr::Xml::SchemaGenerator`
218
+
219
+ Given a sample XML file, this class can be used to generate a schema definition. For example, from [the spec](spec/xml/schema_generator_spec.rb) you can see that given the XML:
220
+
221
+ ```xml
222
+ <book>
223
+ <id>1</id>
224
+ <author>
225
+ <firstName>greggroth</firstName>
226
+ <lastName>roth</lastName>
227
+ </author>
228
+ <publisher>
229
+ <name>BBC</name>
230
+ </publisher>
231
+ <publishedAt>2016-11-12T8:00:00Z</publishedAt>
232
+ <forSale>no</forSale>
233
+ <rating>8.9</rating>
234
+ <pages>120</pages>
235
+ <summary>In this lovely tale, an intrepid warrior ventures out to save a princess.</summary>
236
+ <chapters>
237
+ <chapter>
238
+ <title>Ch 1</title>
239
+ </chapter>
240
+ <chapter>
241
+ <title>Ch 2</title>
242
+ </chapter>
243
+ </chapters>
244
+ </book>
245
+ ```
246
+
247
+ A schema can be generated using the following:
248
+
249
+ ```ruby
250
+ > require 'eatr/xml/schema_generator'
251
+ > generator = Eatr::Xml::SchemaGenerator.new('./spec/fixtures/xml/book.xml')
252
+ > puts generator.schema('/book')
253
+ => ---
254
+ name: ''
255
+ remove_namespaces: true
256
+ fields:
257
+ - name: id
258
+ xpath: "/book/id"
259
+ type: string
260
+ required: false
261
+ - name: author_first_name
262
+ xpath: "/book/author/firstName"
263
+ type: string
264
+ required: false
265
+ - name: author_last_name
266
+ xpath: "/book/author/lastName"
267
+ type: string
268
+ required: false
269
+ - name: publisher_name
270
+ xpath: "/book/publisher/name"
271
+ type: string
272
+ required: false
273
+ - name: published_at
274
+ xpath: "/book/publishedAt"
275
+ type: string
276
+ required: false
277
+ - name: for_sale
278
+ xpath: "/book/forSale"
279
+ type: string
280
+ required: false
281
+ - name: rating
282
+ xpath: "/book/rating"
283
+ type: string
284
+ required: false
285
+ - name: pages
286
+ xpath: "/book/pages"
287
+ type: string
288
+ required: false
289
+ - name: summary
290
+ xpath: "/book/summary"
291
+ type: string
292
+ required: false
293
+ - node: chapters
294
+ xpath: "/book/chapters/chapter"
295
+ children:
296
+ - name: chapters_title
297
+ xpath: "./title"
298
+ type: string
299
+ required: false
300
+ ```
301
+
302
+ The schema generated should be treated as a starting place rather than a full
303
+ solution since no attempt is made to detect types and all fields are considered
304
+ optional by default. Nonetheless, this can save a lot of time when setting up
305
+ new documents.
306
+
307
+ ### `Eatr::Sql::TableGenerator`
308
+
309
+ This utility is helpful for generating `CREATE TABLE` statements from a schema.
310
+ From [the tests](eatr/sql/table_generator_spec.rb):
311
+
312
+ ```ruby
313
+ > require 'eatr/sql/table_generator'
314
+ > generator = Eatr::Sql::TableGenerator.new('./spec/fixtures/schema/book.yaml')
315
+ > puts generator.statement
316
+ => CREATE TABLE books (
317
+ id INT NOT NULL,
318
+ author TEXT NOT NULL,
319
+ library_id INT,
320
+ pages INT NOT NULL,
321
+ for_sale BOOLEAN NOT NULL,
322
+ published_at TIMESTAMP NOT NULL,
323
+ rating REAL NOT NULL,
324
+ icbn TEXT,
325
+ summary VARCHAR(15) NOT NULL,
326
+ age INT NOT NULL
327
+ );
328
+ ```
329
+
330
+ When working with large schema files, this can greatry reduce the amount of
331
+ effort required to set up database tables.
332
+
333
+
334
+ ### `Eatr::DotGenerator`
335
+
336
+ This is currently a work-in-progress class, but currently supports specifying two types of relationships as field key:
337
+
338
+ - `belongs_to_one`
339
+ - `has_many`
340
+
341
+ The values at the keys should reference another schema file as `<table_name>.<field name>`. For example, from `book.yaml`:
342
+
343
+ ```yaml
344
+ - name: library_id
345
+ required: false
346
+ type: integer
347
+ belongs_to_one: libraries.id
348
+ ```
349
+
350
+ Following is a complete example taken from [the tests](spec/eatr/dot_generator_spec.rb):
351
+
352
+
353
+ ```ruby
354
+ > generator = Eatr::DotGenerator.new(['./spec/fixtures/schema/book.yaml', './spec/fixtures/schema/chapters.yaml', './spec/fixtures/schema/library.yaml'])
355
+ > puts generator.to_dot
356
+ => strict digraph g {
357
+ ranksep="1.6"
358
+ graph [
359
+ rankdir = "LR"
360
+ ];
361
+ node [
362
+ fontsize = "16"
363
+ ];
364
+ edge [
365
+ arrowhead = "none"
366
+ ];
367
+ "books" [shape=none, margin=0, label=<
368
+ <table border="0" cellborder="1" cellspacing="0" cellpadding="4">
369
+ <tr><td bgcolor="lightblue">books</td></tr>
370
+ <tr><td port="id" align="left">id</td></tr>
371
+ <tr><td port="author" align="left">author</td></tr>
372
+ <tr><td port="library_id" align="left">library_id</td></tr>
373
+ <tr><td port="pages" align="left">pages</td></tr>
374
+ <tr><td port="for_sale" align="left">for_sale</td></tr>
375
+ <tr><td port="published_at" align="left">published_at</td></tr>
376
+ <tr><td port="rating" align="left">rating</td></tr>
377
+ <tr><td port="icbn" align="left">icbn</td></tr>
378
+ <tr><td port="summary" align="left">summary</td></tr>
379
+ <tr><td port="age" align="left">age</td></tr>
380
+ </table>>];
381
+ "chapters" [shape=none, margin=0, label=<
382
+ <table border="0" cellborder="1" cellspacing="0" cellpadding="4">
383
+ <tr><td bgcolor="lightblue">chapters</td></tr>
384
+ <tr><td port="book_id" align="left">book_id</td></tr>
385
+ <tr><td port="title" align="left">title</td></tr>
386
+ </table>>];
387
+ "libraries" [shape=none, margin=0, label=<
388
+ <table border="0" cellborder="1" cellspacing="0" cellpadding="4">
389
+ <tr><td bgcolor="lightblue">libraries</td></tr>
390
+ <tr><td port="id" align="left">id</td></tr>
391
+ <tr><td port="book_title" align="left">book_title</td></tr>
392
+ <tr><td port="desk_number" align="left">desk_number</td></tr>
393
+ </table>>];
394
+ "books":"id" -> "chapters":"book_id" [arrowhead="crow"];
395
+ "books":"library_id" -> "libraries":"id" [arrowhead="teeodot"];
396
+ "chapters":"book_id" -> "books":"id" [arrowhead="tee"];
397
+ }
398
+ ```
399
+
400
+ Which used to generate a PNG, looks like:
401
+
402
+ ![Sample DOT ERD](spec/example_dot.png)
403
+
404
+ By default, a [built-in template](lib/eatr/dot_template.dot)is used, however you can provide your own template:
405
+
406
+ ```ruby
407
+ Eatr::DotGenerator.new(['path/to/schema.yaml'], template_path: 'path/to/dot_template.dot')
408
+ ```
409
+
410
+ The template is processed using ERB, so the provided template should be referenced when creating your own.
411
+
412
+ ## Development
413
+
414
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
415
+
416
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
417
+
418
+ ## Contributing
419
+
420
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bluebottlecoffee/eatr. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
421
+
422
+
423
+ ## License
424
+
425
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
426
+