soogem 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/soogem.rb +2 -0
  2. data/lib/soosv.rb +59 -0
  3. data/lib/soosv_tests.rb +62 -0
  4. metadata +67 -0
data/lib/soogem.rb ADDED
@@ -0,0 +1,2 @@
1
+ class Soogem
2
+ end
data/lib/soosv.rb ADDED
@@ -0,0 +1,59 @@
1
+ class SooSV
2
+
3
+ attr_accessor :new_line_char, :delimiter, :header, :body
4
+
5
+ def initialize(file_name)
6
+ @file_name = file_name
7
+ @last_header_line = 1
8
+ @new_line_char = "\n"
9
+ @delimiter = ","
10
+ self
11
+ end
12
+
13
+ def get_lines
14
+ file = File.open(@file_name)
15
+ header, body = [], []
16
+ i=1
17
+
18
+ file.each_line(@new_line_char).each do |line|
19
+ if i <= @last_header_line
20
+ header << line
21
+ else
22
+ body << line
23
+ end
24
+ i=i+1
25
+ end
26
+ @header = header.to_s
27
+ @body = body
28
+ [header, body]
29
+ end
30
+
31
+ # Takes in a line and splits it on the delimiter
32
+ # and turns it into an array
33
+ def line_to_array(line)
34
+ attributes = []
35
+ last_split_index = 0
36
+ inside_quote = false
37
+ line.chars.each_with_index do |char, index|
38
+
39
+ inside_quote = !inside_quote if quote_toggles?(line, index)
40
+ next unless char == @delimiter && !inside_quote
41
+ next if line[index-1]=="\\"
42
+
43
+ attributes << line[last_split_index..index-1]
44
+ last_split_index = index+1
45
+ end
46
+ attributes << line[last_split_index..-1]
47
+ attributes
48
+ end
49
+
50
+ private #----------------
51
+
52
+ # If a valid quote was found, ie one that is not escaped and thus changes the value of
53
+ # whether we are inside a quoted section of text, return true, false otherwise
54
+ def quote_toggles?(line, index)
55
+ return false unless line[index].chr == '"'
56
+ (line[index-1].chr == /\\/) ? false : true
57
+ end
58
+
59
+ end
@@ -0,0 +1,62 @@
1
+ require "soosv"
2
+ require "test/unit"
3
+ require 'tempfile'
4
+
5
+ class TestSooSV < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @sample_header = "Name,Definition\r\n"
9
+ @attributes = ["ACNTBL_BAL","\"ACCOUNTABLE BALANCE. The dollar amount held by VBA which is\n\n associated with a PTCPNT, or unknown person(s). This is established\n\n as a result of a returned check, amounts pending fiduciary\n\n identification or approval prior to release, Personal Funds Of\n\n Patients (PFOP), recurring payments calculated monthly but released\n\n other than monthly, payments due less than the established amount\n\n ($5) which are not to be released until additional funds are due,\n\n unassociated returned checks with no existing PTCPNT information,\n\n unassociated funds from PTCPNT which exceed RECEIVABLE balances and\n\n unassociated funds from anyone being held for research purposes.<br>\"","\"third attribute\"","fourth attribute\r\n"]
10
+ @sample_line = @attributes.join(',')
11
+ @csv_file = Tempfile.new(['fake', '.csv'])
12
+ @csv_file.write(@sample_header)
13
+ @csv_file.write(@sample_line)
14
+ @csv_file.close
15
+
16
+ @soosv = SooSV.new(@csv_file.path)
17
+ @soosv.new_line_char = "\r\n"
18
+ @soosv.get_lines
19
+ end
20
+
21
+ def teardown
22
+ @csv_file.unlink
23
+ end
24
+
25
+ def test_setup
26
+ @csv_file.open
27
+ assert_equal(@sample_header, @csv_file.gets)
28
+ end
29
+
30
+ def test_initialize
31
+ soosv = SooSV.new(@csv_file.path)
32
+ assert_instance_of(SooSV, soosv)
33
+ end
34
+
35
+ def test_get_lines
36
+ # nothing atm
37
+ end
38
+
39
+ def test_parse_header
40
+ assert_equal(@sample_header, @soosv.header)
41
+ end
42
+
43
+ def test_parse_line
44
+ assert_equal(@sample_line, @soosv.body.first)
45
+ end
46
+
47
+ def test_quote_toggles?
48
+ simple_line = "foo\\\"bar. lol--+==234\\f"
49
+ true_index = 4
50
+ simple_line.chars.each_with_index do |char, index|
51
+ eval_to = (index == true_index) ? true : false
52
+ assert_equal(eval_to, @soosv.send(:quote_toggles?, simple_line, index))
53
+ end
54
+ end
55
+
56
+ def test_line_to_array
57
+ @attributes.each_with_index do |attribute, index|
58
+ assert_equal(attribute, @soosv.line_to_array(@soosv.body.first)[index])
59
+ end
60
+ end
61
+
62
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: soogem
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jason Soo
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-12-19 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: A collection of classes I always seem to be rewriting
22
+ email: wwwjscom@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/soogem.rb
31
+ - lib/soosv.rb
32
+ - lib/soosv_tests.rb
33
+ homepage: http://rubygems.org/gems/soogem
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.10
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: A collection of classes I always seem to be rewriting
66
+ test_files: []
67
+