fixedwidth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fixedwidth.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Sean Behan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Fixedwidth
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'fixedwidth'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install fixedwidth
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fixedwidth/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "fixedwidth"
8
+ gem.version = Fixedwidth::VERSION
9
+ gem.authors = ["Sean Behan"]
10
+ gem.email = ["inbox@seanbehan.com"]
11
+ gem.description = %q{Fixedwidth data parsing.}
12
+ gem.summary = %q{This is a bare bones gem that lets you parse fixed width data files and transform them into CSV or Ruby hashes.}
13
+ gem.homepage = "https://github.com/gristmill/fixedwidth"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,69 @@
1
+ require "fixedwidth/version"
2
+
3
+ module Fixedwidth
4
+ def self.parse(options)
5
+ @options = options
6
+ @options[:delimiter] ||= ","
7
+
8
+ if block_given?
9
+ File.open(@options[:file]).each_line do |line|
10
+ yield Line.new(line)
11
+ end
12
+ end
13
+ end
14
+
15
+ def self.start
16
+ @start ||= @options[:start].split(@options[:delimiter])
17
+ end
18
+
19
+ def self.stop
20
+ @stop ||= @options[:stop].split(@options[:delimiter])
21
+ end
22
+
23
+ def self.header
24
+ @header ||= @options[:header].split(@options[:delimiter])
25
+ end
26
+
27
+ def self.options
28
+ @options
29
+ end
30
+
31
+ def self.column_positions
32
+ [].tap do |positions|
33
+ start.zip(stop).each do |a,b|
34
+ a, b = a.to_i, b.to_i
35
+ a = a - 1
36
+ b = b-a
37
+
38
+ positions << [a,b]
39
+ end
40
+ end
41
+ end
42
+
43
+ class Line
44
+ def initialize(line)
45
+ @line = line
46
+ end
47
+
48
+ def to_s
49
+ @line
50
+ end
51
+
52
+ def to_hash
53
+ Fixedwidth.header.zip(to_a).inject({}){ |sum, d| sum.merge(Hash[d[0].to_sym, d[1]]) }
54
+ end
55
+
56
+ def to_a
57
+ to_csv.split(Fixedwidth.options[:delimiter])
58
+ end
59
+
60
+ def to_csv
61
+ [].tap do |results|
62
+ Fixedwidth.column_positions.each do |a,b|
63
+ results << @line[a,b]
64
+ end
65
+ end.join(Fixedwidth.options[:delimiter]).gsub(/\s{2}/, '').split(Fixedwidth.options[:delimiter]).map(&:strip).join(Fixedwidth.options[:delimiter]) # TODO: Actually think about this!
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,3 @@
1
+ module Fixedwidth
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ John Smith john@example.com 1-888-555-6666
2
+ Michele O'Reileymichele@example.com 1-333-321-8765
@@ -0,0 +1,39 @@
1
+ $:.push(File.expand_path("../lib", __FILE__))
2
+
3
+ require 'fixedwidth'
4
+ require 'test/unit'
5
+
6
+ class FixedwidthTest < Test::Unit::TestCase
7
+ def setup
8
+ Fixedwidth.parse(start: '1,9,17,44,46', stop: '8,16,36,45,63', header: 'first,last,email,blank,phone')
9
+ end
10
+
11
+ def test_line_transformation_formats
12
+ line = Fixedwidth::Line.new('John Smith john@example.com 1-888-555-6666')
13
+
14
+ # Types
15
+ assert_kind_of String, line.to_s
16
+ assert_kind_of String, line.to_csv
17
+ assert_kind_of Hash, line.to_hash
18
+
19
+ # Formats
20
+ assert_equal 'John,Smith,john@example.com,,1-888-555-6666', line.to_csv
21
+ assert_equal ['John', 'Smith', 'john@example.com', '', '1-888-555-6666'], line.to_a
22
+ assert_equal Hash[first: "John", last: "Smith", email: "john@example.com", blank: "", phone: "1-888-555-6666"], line.to_hash
23
+ end
24
+
25
+ def test_fixedwidth_column_position_offsets
26
+ assert_equal [[0, 8], [8, 8], [16, 20], [43, 2], [45, 18]], Fixedwidth.column_positions
27
+ end
28
+
29
+ def test_fixedwidth_yield_line
30
+ Fixedwidth.parse(file: File.dirname(__FILE__)+'/contacts.txt', start: '1,9,17,44,46', stop: '8,16,36,45,63', header: 'first,last,email,blank,phone') do |line|
31
+ assert_kind_of Fixedwidth::Line, line
32
+ assert_kind_of Array, line.to_a
33
+ assert_kind_of String, line.to_s
34
+ assert_kind_of Hash, line.to_hash
35
+ [:first, :last, :email, :blank, :phone].each { |header| assert line.to_hash.keys.include?(header) }
36
+ end
37
+ end
38
+ end
39
+
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fixedwidth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sean Behan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Fixedwidth data parsing.
15
+ email:
16
+ - inbox@seanbehan.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - fixedwidth.gemspec
27
+ - lib/fixedwidth.rb
28
+ - lib/fixedwidth/version.rb
29
+ - test/contacts.txt
30
+ - test/fixedwidth_test.rb
31
+ homepage: https://github.com/gristmill/fixedwidth
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.24
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: This is a bare bones gem that lets you parse fixed width data files and transform
55
+ them into CSV or Ruby hashes.
56
+ test_files:
57
+ - test/contacts.txt
58
+ - test/fixedwidth_test.rb