fixed_width_file_parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3455ef0663d655e5f0e6961429d6a3aa69eae6a0
4
+ data.tar.gz: fdf7ab1baa2f5047f8d9720b42c659a0c9276dba
5
+ SHA512:
6
+ metadata.gz: 8ed8bf0c4ff9214fb096c43c0df00f2b2a446232a4459538f947f6fbd8d903921375a1c5e161072cc7eff82486821a3321e348e1f3c06fc6954d16d39f8fab46
7
+ data.tar.gz: f5808f8824d113cde29ecb1b24503050cbe39550ce47b8cb9a7ed9952c3095219b516d11800dacc4b7ade542643d9a29fa931734869f7beb3e9eaa4d405ffeba
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fixed_width_file_parser.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # FixedWidthFileParser
2
+
3
+ FixedWidthFileParser is used to parse fixed width files. Crazy right?
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'fixed_width_file_parser'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install fixed_width_file_parser
20
+
21
+ ## Usage
22
+
23
+ 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.
24
+
25
+ ```ruby
26
+ filepath = 'path/to/file.txt'
27
+ fields = [
28
+ { name: 'first_name', position: 0..10 },
29
+ { name: 'middle_initial', position: 11 },
30
+ { name: 'last_name', position: 12..25 }
31
+ ]
32
+
33
+ FixedWidthFileParser.parse(filepath, fields) do |row|
34
+ puts row[:first_name]
35
+ puts row[:middle_initial]
36
+ puts row[:last_name]
37
+ end
38
+ ```
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
43
+
44
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it ( https://github.com/elevatorup/fixed_width_file_parser/fork )
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Dir.glob('lib/tasks/**/*.rake').each(&method(:import))
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fixed_width_file_parser"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fixed_width_file_parser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fixed_width_file_parser"
8
+ spec.version = FixedWidthFileParser::VERSION
9
+ spec.authors = ["Jim Smith"]
10
+ spec.email = ["jim@jimsmithdesign.com"]
11
+
12
+ spec.summary = "Parse fixed width files easily and efficiently."
13
+ spec.homepage = "https://github.com/elevatorup/fixed_width_file_parser"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.8"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency 'rspec'
23
+ end
@@ -0,0 +1,3 @@
1
+ module FixedWidthFileParser
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,63 @@
1
+ require "fixed_width_file_parser/version"
2
+
3
+ module FixedWidthFileParser
4
+ # Parse a fixed width file, yielding the proper data for each line based on the fields passed in
5
+ #
6
+ # @param filepath [String] The path to the file to be parsed.
7
+ # @param fields [Array(Hash{name => String, position => Range|Integer})] An array of field hashes, each containing a `name` and a `position`.
8
+ # @yield [Hash] Yields a hash object based on the fields provided.
9
+ #
10
+ # @example
11
+ # filepath = 'path/to/file'
12
+ # fields = [
13
+ # { name: 'first_name', position: 0..10 },
14
+ # { name: 'middle_initial', position: 11 },
15
+ # { name: 'last_name', position: 12..25 }
16
+ # ]
17
+ #
18
+ # FixedWidthFileParser.parse(filepath, fields) do |row|
19
+ # puts row
20
+ # end
21
+
22
+ def self.parse(filepath, fields)
23
+ # Verify `filepath` is a String
24
+ unless filepath.is_a?(String)
25
+ raise '`filepath` must be a String'
26
+ end
27
+
28
+ # Verify `fields` is an array
29
+ if fields.is_a?(Array)
30
+ # Verify fields is not emtpy
31
+ if fields.empty?
32
+ raise '`fields` must contain at least 1 item'
33
+ end
34
+ else
35
+ raise '`fields` must be an Array'
36
+ end
37
+
38
+ # Verify each field has a `name` and `position`
39
+ unless fields.all? { |item| item.key?(:name) && item.key?(:position) }
40
+ raise 'Each field hash must include a `name` and a `position`'
41
+ end
42
+
43
+ # Verify that each `position` is either a Range or an Integer
44
+ unless fields.all? { |item| item[:position].is_a?(Range) || item[:position].is_a?(Integer) }
45
+ raise "Each field's `position` must be a Range or an Integer"
46
+ end
47
+
48
+ file = File.open(filepath)
49
+
50
+ while !file.eof?
51
+ line = file.readline
52
+
53
+ line_fields = {}
54
+ fields.each do |field|
55
+ line_fields[field[:name].to_sym] = line[ field[:position] ].strip
56
+ end
57
+
58
+ yield(line_fields)
59
+ end
60
+
61
+ file.close
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fixed_width_file_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jim Smith
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-09 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.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - jim@jimsmithdesign.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - fixed_width_file_parser.gemspec
71
+ - lib/fixed_width_file_parser.rb
72
+ - lib/fixed_width_file_parser/version.rb
73
+ - lib/tasks/rspec.rake
74
+ homepage: https://github.com/elevatorup/fixed_width_file_parser
75
+ licenses: []
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.6
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Parse fixed width files easily and efficiently.
97
+ test_files: []