ascii_data 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ require 'autotest/bundler'
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ascii_data.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem "rspec"
8
+ end
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ascii_data/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ascii_data"
7
+ s.version = AsciiData::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["David Santoro"]
10
+ s.email = ["soulnafein@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Little library for importing ascii data files}
13
+ s.description = %q{Just browse the specs and check it out}
14
+
15
+ s.rubyforge_project = "ascii_data"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,3 @@
1
+ module AsciiData
2
+ require 'ascii_data_row'
3
+ end
@@ -0,0 +1,3 @@
1
+ module AsciiData
2
+ VERSION = "0.0.6"
3
+ end
@@ -0,0 +1,84 @@
1
+ require 'time'
2
+ require 'iconv'
3
+
4
+ class AsciiDataRow
5
+ class << self
6
+ attr_reader :fields_definitions
7
+
8
+ def fields_definitions
9
+ @fields_definitions ||= {}
10
+ end
11
+ end
12
+
13
+ def self.field(name, range, type, options={})
14
+ options[:format] ||= '%d/%m/%Y'
15
+ fields_definitions[name] = FieldDefinition.new(type, range, options)
16
+ end
17
+
18
+ def self.create_from(ascii_row)
19
+ self.new(ascii_row)
20
+ end
21
+
22
+ def initialize(ascii_row)
23
+ @fields = {}
24
+ @ascii_row = remove_invalid_utf8_bytes(ascii_row)
25
+ self.class.fields_definitions.each_pair do |name, definition|
26
+ @fields[name] = get_value_for_field_definition(definition)
27
+ end
28
+ end
29
+
30
+ def fields
31
+ @fields
32
+ end
33
+
34
+ private
35
+ def remove_invalid_utf8_bytes(a_string)
36
+ @ic ||= Iconv.new('UTF-8//IGNORE', 'UTF-8')
37
+ @ic.iconv(a_string)
38
+ end
39
+
40
+ def get_value_for_field_definition(definition)
41
+ text_value = @ascii_row.slice(definition.range.min, definition.range.max).strip
42
+
43
+ case definition.type
44
+ when :string
45
+ text_value
46
+ when :int
47
+ text_value.empty? ? nil : text_value.to_i
48
+ when :float
49
+ text_value.empty? ? nil : text_value.to_f
50
+ when :date
51
+ text_value.empty? ? nil : parse_date(text_value, definition.options[:format])
52
+ when :bool
53
+ text_value == '1'
54
+ else
55
+ nil
56
+ end
57
+ end
58
+
59
+ def parse_date(text_value, format)
60
+ if format == "%Y/%m/%d"
61
+ year = text_value[0..3].to_i
62
+ month = text_value[5..6].to_i
63
+ day = text_value[8..9].to_i
64
+ Time.utc(year, month, day)
65
+ elsif format == "%d/%m/%Y"
66
+ day = text_value[0..1].to_i
67
+ month = text_value[3..4].to_i
68
+ year = text_value[6..9].to_i
69
+ Time.utc(year, month, day)
70
+ else
71
+ Time.strptime(text_value, format)
72
+ end
73
+ end
74
+ end
75
+
76
+ class FieldDefinition
77
+ attr_reader :type, :range, :options
78
+
79
+ def initialize(type, range, options)
80
+ @type = type
81
+ @range = Range.new(range.min-1, range.max-1)
82
+ @options = options
83
+ end
84
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,90 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe AsciiDataRow do
4
+ context "when parsing a ascii data row" do
5
+ # 1-5 5 CLHB CAP Vehicle Identification Number
6
+ # 6-15 10 CLHB Technical Data Date (dd/mm/yyyy)
7
+ # 16-18 3 CL B Insurance Group 1
8
+ # 19-21 3 B Insurance Group 2
9
+ # 22-31 10 CLHB Kerb Weight (kg) (Dry Weight for BIKES)
10
+ # 32-41 10 CLHB Length (mm)
11
+ # 42-53 10 CLHB Width (mm)
12
+ ASCII_LINE = %q{4404201/01/2010 13 1035 3657 1627.23}
13
+
14
+ it "should load strings in a certain ranges" do
15
+ class RowA < AsciiDataRow
16
+ field :vehicle_id_number, 1..5, :string
17
+ end
18
+
19
+ row = RowA.create_from(ASCII_LINE)
20
+
21
+ row.fields[:vehicle_id_number].should == "44042"
22
+ end
23
+
24
+ it "should trim spaces at the beginning and the end of string values" do
25
+ class RowB < AsciiDataRow
26
+ field :kerb_weight_as_text, 22..31, :string
27
+ end
28
+
29
+ row = RowB.create_from(ASCII_LINE)
30
+
31
+ row.fields[:kerb_weight_as_text].should == "1035"
32
+ end
33
+
34
+ it "should parse integers correctly" do
35
+ class RowC < AsciiDataRow
36
+ field :insurance_group_1, 16..18, :int
37
+ field :insurance_group_2, 19..21, :int
38
+ field :kerb_weight, 22..31, :int
39
+ end
40
+
41
+ row = RowC.create_from(ASCII_LINE)
42
+
43
+ row.fields[:insurance_group_1].should == 13
44
+ row.fields[:insurance_group_2].should be_nil
45
+ row.fields[:kerb_weight].should == 1035
46
+ end
47
+
48
+ it "should parse floats correctly" do
49
+ class RowD < AsciiDataRow
50
+ field :width, 42..54, :float
51
+ end
52
+
53
+ row = RowD.create_from(ASCII_LINE)
54
+
55
+ row.fields[:width].should == 1627.23
56
+ end
57
+
58
+ it "should parse bools correctly" do
59
+ class RowE < AsciiDataRow
60
+ field :flag_1, 3..3, :bool
61
+ field :flag_2, 5..5, :bool
62
+ end
63
+
64
+ row = RowE.create_from(" 1 0")
65
+
66
+ row.fields[:flag_1].should be_true
67
+ row.fields[:flag_2].should be_false
68
+ end
69
+
70
+ it "should parse dates correctly" do
71
+ class RowF < AsciiDataRow
72
+ field :date_1, 3..12, :date
73
+ end
74
+
75
+ row = RowF.create_from(" 12/03/2012")
76
+
77
+ row.fields[:date_1].should == Time.utc(2012, 3, 12)
78
+ end
79
+
80
+ it "should accept different date formats" do
81
+ class RowF < AsciiDataRow
82
+ field :date_1, 3..12, :date, :format => '%Y/%m/%d'
83
+ end
84
+
85
+ row = RowF.create_from(" 2012/03/12")
86
+
87
+ row.fields[:date_1].should == Time.utc(2012, 3, 12)
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,2 @@
1
+ require 'rspec'
2
+ require File.dirname(__FILE__) + '/../lib/ascii_data'
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ascii_data
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Santoro
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-06 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Just browse the specs and check it out
15
+ email:
16
+ - soulnafein@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .autotest
22
+ - .gitignore
23
+ - Gemfile
24
+ - Rakefile
25
+ - ascii_data.gemspec
26
+ - lib/ascii_data.rb
27
+ - lib/ascii_data/version.rb
28
+ - lib/ascii_data_row.rb
29
+ - spec/.rspec
30
+ - spec/ascii_data_row_spec.rb
31
+ - spec/spec_helper.rb
32
+ homepage: ''
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project: ascii_data
52
+ rubygems_version: 1.8.10
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Little library for importing ascii data files
56
+ test_files: []