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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 59078b301af3aef44e9855d8d06a4a0d554a7ec4
4
- data.tar.gz: fa56e8c90146151dba6d45f1925f10e68409feff
3
+ metadata.gz: 1ed5236150765f850f0ef121969ead2690e2902a
4
+ data.tar.gz: 8b192816560cebe723d2273234f13c47c7fc46b6
5
5
  SHA512:
6
- metadata.gz: 37ea2200ebea71049b61aca3ceaebfa6f8890893ed49dd98a9401b1893aa122209e12386c12c70ab2a71c02c0f1a88f30c2f137e07c2e3078bdc72c995759e9c
7
- data.tar.gz: 0bc5c285b39f7182b94964359403eda95f03c96a286001a849a323425ea2bae159ec3e8b5fb70f2e0bf8894643bf8b4de9ef969b6cf7dbbdc35e7fc1dbe56a5a
6
+ metadata.gz: 3dc0f28edf26041202972b8a7beff261a05069470c268cadec1941a3bf58d7a624beb3f7544513e71c1ef267b8144d993517e7eff81c843cb2936cb44a3ea485
7
+ data.tar.gz: 1f52e50aa212549daa08b685d2383f97312aa6ffb74d19523b6a90897ec6c2ae97db136fcb0f90c620bffcfe78a91112b58bf93c663d4132fe2db19af00febb0
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  /.bundle/
2
+ /.ruby-version
2
3
  /.yardoc
3
4
  /Gemfile.lock
4
5
  /_yardoc/
@@ -0,0 +1 @@
1
+ fixed_width_file_parser
@@ -2,6 +2,9 @@ language: ruby
2
2
  rvm:
3
3
  - 2.2.2
4
4
 
5
+ after_script:
6
+ - bundle exec codeclimate-test-reporter
7
+
5
8
  addons:
6
9
  code_climate:
7
10
  repo_token: 189bae4a1eece160ea8b6b996a2f3b2b2ba6ded05f1fd89359176debd7d75e72
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 (and their positions) as well as the filepath, and then pass those to a block that will yield the data for each row.
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
- file = File.open(filepath)
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
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module FixedWidthFileParser
3
- VERSION = '1.0.0'
3
+ VERSION = '1.0.1'
4
4
  end
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.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: 2016-10-05 00:00:00.000000000 Z
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