estratto 1.0.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
+ SHA1:
3
+ metadata.gz: f4425ecb691c561000266f1717d1bcab1aa9c198
4
+ data.tar.gz: f3526413190af262cf089112ae34682f1465894b
5
+ SHA512:
6
+ metadata.gz: fd3154c347e94acd858b7a92e88817d00f3e55618ac8191786a5b5606212fb519f9180e071dcffec9e776d15c8bb88dedbe709a3fcedd514098d74ae96718ce6
7
+ data.tar.gz: be0cdd97b6a300d68bd30c6b4090c20c2274c2b4a160ca7d925c49c32a30704c144cf6de82e88aa8cd0f2a068cc22b83c71f4ab84e5282daffd11728f5f903c7
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.0
7
+ before_install: gem install bundler -v 1.17.3
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in estratto.gemspec
6
+ gemspec
7
+
8
+ group :test do
9
+ gem 'coveralls', require: false
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,63 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ estratto (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ coveralls (0.8.22)
11
+ json (>= 1.8, < 3)
12
+ simplecov (~> 0.16.1)
13
+ term-ansicolor (~> 1.3)
14
+ thor (~> 0.19.4)
15
+ tins (~> 1.6)
16
+ diff-lcs (1.3)
17
+ docile (1.3.1)
18
+ json (2.1.0)
19
+ method_source (0.8.2)
20
+ pry (0.10.4)
21
+ coderay (~> 1.1.0)
22
+ method_source (~> 0.8.1)
23
+ slop (~> 3.4)
24
+ pry-nav (0.2.4)
25
+ pry (>= 0.9.10, < 0.11.0)
26
+ rake (10.5.0)
27
+ rspec (3.7.0)
28
+ rspec-core (~> 3.7.0)
29
+ rspec-expectations (~> 3.7.0)
30
+ rspec-mocks (~> 3.7.0)
31
+ rspec-core (3.7.1)
32
+ rspec-support (~> 3.7.0)
33
+ rspec-expectations (3.7.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.7.0)
36
+ rspec-mocks (3.7.0)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.7.0)
39
+ rspec-support (3.7.1)
40
+ simplecov (0.16.1)
41
+ docile (~> 1.1)
42
+ json (>= 1.8, < 3)
43
+ simplecov-html (~> 0.10.0)
44
+ simplecov-html (0.10.2)
45
+ slop (3.6.0)
46
+ term-ansicolor (1.7.0)
47
+ tins (~> 1.0)
48
+ thor (0.19.4)
49
+ tins (1.20.2)
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ bundler (~> 1.17)
56
+ coveralls
57
+ estratto!
58
+ pry-nav
59
+ rake (~> 10.0)
60
+ rspec (~> 3.0)
61
+
62
+ BUNDLED WITH
63
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 TODO: Write your name
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,41 @@
1
+ # Estratto
2
+
3
+ [![Build Status](https://travis-ci.com/Rynaro/estratto.svg?branch=master)](https://travis-ci.com/Rynaro/estratto)
4
+ [![Coverage Status](https://coveralls.io/repos/github/Rynaro/estratto/badge.svg?branch=master)](https://coveralls.io/github/Rynaro/estratto?branch=master)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/46532b90e850401fce72/maintainability)](https://codeclimate.com/github/Rynaro/estratto/maintainability)
6
+
7
+ ### TODO: Write a super README
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'estratto'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install estratto
24
+
25
+ ## Usage
26
+
27
+ TODO: Comming soon
28
+
29
+ ## Development
30
+
31
+ 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.
32
+
33
+ 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).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Rynaro/estratto.
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "estratto"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ 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
data/estratto.gemspec ADDED
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "estratto/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "estratto"
8
+ spec.version = Estratto::VERSION
9
+ spec.authors = ["Henrique A. Lavezzo"]
10
+ spec.email = ["me@hlavezzo.run"]
11
+
12
+ spec.summary = %q{Parsing fixed width files made easy}
13
+ spec.homepage = "https://github.com/Rynaro/estratto"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.17"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "pry-nav"
27
+ end
@@ -0,0 +1,19 @@
1
+ module Estratto
2
+ module Data
3
+ class TypeCoercionNotFound < StandardError; end
4
+
5
+ class Base
6
+ attr_reader :data, :formats
7
+
8
+ def initialize(data, formats = {})
9
+ @data = data
10
+ @formats = formats
11
+ end
12
+
13
+ def coerce
14
+ raise TypeCoercionNotFound
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'integer'
2
+ require_relative 'float'
3
+ require_relative 'datetime'
4
+ require_relative 'date'
5
+ require_relative 'string'
6
+
7
+ module Estratto
8
+ module Data
9
+ class InvalidCoercionType < StandardError; end
10
+
11
+ class Coercer
12
+ attr_reader :data, :type, :formats
13
+
14
+ def initialize(data:, type: 'String', formats: {})
15
+ @data = data
16
+ @type = type
17
+ @formats = formats
18
+ end
19
+
20
+ def build
21
+ target_coercer.coerce
22
+ end
23
+
24
+ def target_coercer
25
+ Object.const_get("Estratto::Data::#{type}").new(data, formats)
26
+ rescue NameError
27
+ raise InvalidCoercionType
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'base'
2
+
3
+ module Estratto
4
+ module Data
5
+ class Date < DateTime
6
+ def coerce
7
+ super.to_date
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'base'
2
+
3
+ module Estratto
4
+ module Data
5
+ class DateTime < Base
6
+
7
+ def coerce
8
+ parse_datetime_from
9
+ end
10
+
11
+ private
12
+
13
+ def parse_datetime_from
14
+ ::DateTime.strptime(data, datetime_format)
15
+ end
16
+
17
+ def datetime_format
18
+ formats.dig('format') || '%Y-%m-%d'
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,41 @@
1
+ require_relative 'base'
2
+
3
+ module Estratto
4
+ module Data
5
+ class Float < Base
6
+
7
+ def coerce
8
+ if expected_comma
9
+ from_comma_data
10
+ elsif custom_precision
11
+ from_precision_data
12
+ else
13
+ data.to_f
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def expected_comma
20
+ formats.dig('comma_format') || false
21
+ end
22
+
23
+ def custom_precision
24
+ formats.has_key?('precision')
25
+ end
26
+
27
+ def from_comma_data
28
+ data.gsub('.', '').gsub(',', '.').to_f
29
+ end
30
+
31
+ def from_precision_data
32
+ data.to_f / (10**precision)
33
+ end
34
+
35
+ def precision
36
+ formats['precision'].to_i || 2
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'base'
2
+
3
+ module Estratto
4
+ module Data
5
+ class Integer < Base
6
+
7
+ def coerce
8
+ data.to_i
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'base'
2
+
3
+ module Estratto
4
+ module Data
5
+ class String < Base
6
+
7
+ def coerce
8
+ if strip_value?
9
+ stripped_data
10
+ else
11
+ data
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def strip_value?
18
+ formats.dig('strip') || false
19
+ end
20
+
21
+ def stripped_data
22
+ data.strip
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ require_relative 'layout/factory'
2
+ require_relative 'parser'
3
+
4
+ module Estratto
5
+ class Document
6
+ class << self
7
+ def process(layout:,file:)
8
+ layout_fabricated = Layout::Factory.fabricate(layout)
9
+ Parser.new(file, layout_fabricated).perform
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module Estratto
2
+ module Helpers
3
+ class Range
4
+ def self.for(string)
5
+ ::Range.new(*string.split('..').map(&:to_i))
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module Estratto
2
+ module Layout
3
+ class Base
4
+ extend Forwardable
5
+
6
+ attr_reader :reader
7
+ def_delegators :@reader, :layout, :registers
8
+
9
+ def initialize(reader)
10
+ @reader = reader
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ require_relative 'reader'
2
+ require_relative 'multi_register'
3
+
4
+ module Estratto
5
+ module Layout
6
+ class LayoutUndefinedError < StandardError; end
7
+ class Factory
8
+
9
+ def self.fabricate(layout_path)
10
+ reader = Reader.new(layout_path)
11
+ if reader.multiregister?
12
+ MultiRegister.new(reader)
13
+ else
14
+ raise LayoutUndefinedError
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'base'
2
+ require_relative '../helpers/range'
3
+
4
+ module Estratto
5
+ module Layout
6
+ class LayoutNotFoundForPrefix < StandardError; end
7
+
8
+ class MultiRegister < Base
9
+
10
+ def prefix_range
11
+ Estratto::Helpers::Range.for(layout['prefix'])
12
+ end
13
+
14
+ def register_fields_for(prefix)
15
+ find_register(prefix)&.dig('fields')
16
+ end
17
+
18
+ private
19
+
20
+ def find_register(prefix)
21
+ registers.find { |register| register['register'] == prefix }
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ require 'yaml'
2
+
3
+ module Estratto
4
+ module Layout
5
+ class Reader
6
+ attr_reader :file
7
+
8
+ def initialize(file)
9
+ @file = file
10
+ end
11
+
12
+ def template
13
+ @template ||= YAML.load_file(file)
14
+ end
15
+
16
+ def layout
17
+ template['layout']
18
+ end
19
+
20
+ def registers
21
+ @registers ||= layout['registers']
22
+ end
23
+
24
+ def multiregister?
25
+ layout.dig('multi-register') || false
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'register'
2
+
3
+ module Estratto
4
+ class Parser
5
+ attr_reader :file_path, :layout
6
+
7
+ def initialize(file_path, layout)
8
+ @file_path = file_path
9
+ @layout = layout
10
+ end
11
+
12
+ def perform
13
+ @data ||= raw_data.map do |line|
14
+ register_layout = layout.register_fields_for(line[layout.prefix_range])
15
+ next if register_layout.nil?
16
+ Register.new(line, register_layout).refine
17
+ end.compact
18
+ end
19
+
20
+ def raw_data
21
+ @raw_data ||= File.open(file_path, 'r')
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'data/coercer'
2
+ require_relative 'helpers/range'
3
+
4
+ module Estratto
5
+ class Register
6
+ attr_reader :line, :register_layout
7
+
8
+ def initialize(line, register_layout)
9
+ @line = line
10
+ @register_layout = register_layout
11
+ end
12
+
13
+ def refine
14
+ register_layout.map do |layout|
15
+ {
16
+ layout['name'].to_sym => coerced_data(layout['range'], layout['type'], layout['formats'])
17
+ }
18
+ end.inject(&:merge)
19
+ end
20
+
21
+ private
22
+
23
+ def coerced_data(range, type, formats)
24
+ Estratto::Data::Coercer.new(
25
+ data: line[Estratto::Helpers::Range.for(range)],
26
+ type: type,
27
+ formats: formats || {}
28
+ ).build
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Estratto
2
+ VERSION = "1.0.0"
3
+ end
data/lib/estratto.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "estratto/version"
2
+ require "estratto/document"
3
+
4
+ module Estratto
5
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: estratto
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Henrique A. Lavezzo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-nav
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
+ description:
70
+ email:
71
+ - me@hlavezzo.run
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - estratto.gemspec
87
+ - lib/estratto.rb
88
+ - lib/estratto/data/base.rb
89
+ - lib/estratto/data/coercer.rb
90
+ - lib/estratto/data/date.rb
91
+ - lib/estratto/data/datetime.rb
92
+ - lib/estratto/data/float.rb
93
+ - lib/estratto/data/integer.rb
94
+ - lib/estratto/data/string.rb
95
+ - lib/estratto/document.rb
96
+ - lib/estratto/helpers/range.rb
97
+ - lib/estratto/layout/base.rb
98
+ - lib/estratto/layout/factory.rb
99
+ - lib/estratto/layout/multi_register.rb
100
+ - lib/estratto/layout/reader.rb
101
+ - lib/estratto/parser.rb
102
+ - lib/estratto/register.rb
103
+ - lib/estratto/version.rb
104
+ homepage: https://github.com/Rynaro/estratto
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.6.8
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Parsing fixed width files made easy
128
+ test_files: []