fixed_width_file_parser 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.ruby-gemset +1 -0
- data/.travis.yml +3 -0
- data/LICENSE +21 -0
- data/README.md +25 -4
- data/lib/fixed_width_file_parser.rb +7 -3
- data/lib/fixed_width_file_parser/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ed5236150765f850f0ef121969ead2690e2902a
|
4
|
+
data.tar.gz: 8b192816560cebe723d2273234f13c47c7fc46b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3dc0f28edf26041202972b8a7beff261a05069470c268cadec1941a3bf58d7a624beb3f7544513e71c1ef267b8144d993517e7eff81c843cb2936cb44a3ea485
|
7
|
+
data.tar.gz: 1f52e50aa212549daa08b685d2383f97312aa6ffb74d19523b6a90897ec6c2ae97db136fcb0f90c620bffcfe78a91112b58bf93c663d4132fe2db19af00febb0
|
data/.gitignore
CHANGED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
fixed_width_file_parser
|
data/.travis.yml
CHANGED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2016 Jim Smith
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -26,21 +26,34 @@ gem install fixed_width_file_parser
|
|
26
26
|
|
27
27
|
## Usage
|
28
28
|
|
29
|
-
In order to parse a fixed width file, you need to define the fields
|
29
|
+
In order to parse a fixed width file, you need to define the fields and their positions.
|
30
|
+
Call parse with a filepath or an opened IO object, and pass those to a block that will yield the data for each row.
|
30
31
|
|
31
32
|
```ruby
|
32
|
-
filepath = 'path/to/file.txt'
|
33
33
|
fields = [
|
34
34
|
{ name: 'first_name', position: 0..10 },
|
35
35
|
{ name: 'middle_initial', position: 11 },
|
36
36
|
{ name: 'last_name', position: 12..25 }
|
37
37
|
]
|
38
38
|
|
39
|
+
filepath = 'path/to/file.txt'
|
40
|
+
|
39
41
|
FixedWidthFileParser.parse(filepath, fields) do |row|
|
40
42
|
puts row[:first_name]
|
41
43
|
puts row[:middle_initial]
|
42
44
|
puts row[:last_name]
|
43
45
|
end
|
46
|
+
|
47
|
+
# or, from an input stream:
|
48
|
+
|
49
|
+
file = StringIO.new(sftp.file.open(filename).read, 'rb')
|
50
|
+
|
51
|
+
FixedWidthFileParser.parse(file, fields) do |row|
|
52
|
+
puts row[:first_name]
|
53
|
+
puts row[:middle_initial]
|
54
|
+
puts row[:last_name]
|
55
|
+
end
|
56
|
+
|
44
57
|
```
|
45
58
|
|
46
59
|
### Tips
|
@@ -60,6 +73,14 @@ fields = [
|
|
60
73
|
|---|---|---|
|
61
74
|
|force_utf8_encoding|true|Force UTF-8 encoding on lines being parsed. This alleviates `invalid byte sequence in UTF-8` errors thrown when trying to split a string with invalid UTF characters. For more information, view this [article](https://robots.thoughtbot.com/fight-back-utf-8-invalid-byte-sequences).|
|
62
75
|
|
76
|
+
e.g.
|
77
|
+
|
78
|
+
```
|
79
|
+
FixedWidthFileParser.parse(filepath, fields, force_utf8_encoding: false) do |row|
|
80
|
+
puts row
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
63
84
|
|
64
85
|
## Development
|
65
86
|
|
@@ -70,7 +91,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
70
91
|
## Contributing
|
71
92
|
|
72
93
|
1. Fork it ( https://github.com/elevatorup/fixed_width_file_parser/fork )
|
73
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
94
|
+
2. Create your feature branch (`git checkout -b feature/my-new-feature`)
|
74
95
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
75
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
96
|
+
4. Push to the branch (`git push origin feature/my-new-feature`)
|
76
97
|
5. Create a new Pull Request
|
@@ -24,8 +24,8 @@ module FixedWidthFileParser
|
|
24
24
|
# Set options, or use default
|
25
25
|
force_utf8_encoding = options.fetch(:force_utf8_encoding, true)
|
26
26
|
|
27
|
-
# Verify `filepath` is a String
|
28
|
-
raise '`filepath` must be a String' unless filepath.is_a?(String)
|
27
|
+
# Verify `filepath` is a String or IO object
|
28
|
+
raise '`filepath` must be a String' unless filepath.is_a?(String) || filepath.respond_to?(:readline)
|
29
29
|
|
30
30
|
# Verify `fields` is an array
|
31
31
|
if fields.is_a?(Array)
|
@@ -47,7 +47,11 @@ module FixedWidthFileParser
|
|
47
47
|
|
48
48
|
GC.start
|
49
49
|
|
50
|
-
|
50
|
+
if filepath.respond_to? :readline
|
51
|
+
file = filepath
|
52
|
+
else
|
53
|
+
file = File.open(filepath)
|
54
|
+
end
|
51
55
|
|
52
56
|
until file.eof?
|
53
57
|
line = file.readline
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fixed_width_file_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Smith
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -75,8 +75,10 @@ extra_rdoc_files: []
|
|
75
75
|
files:
|
76
76
|
- ".gitignore"
|
77
77
|
- ".rspec"
|
78
|
+
- ".ruby-gemset"
|
78
79
|
- ".travis.yml"
|
79
80
|
- Gemfile
|
81
|
+
- LICENSE
|
80
82
|
- README.md
|
81
83
|
- Rakefile
|
82
84
|
- bin/console
|