Class: HarmonizedTariff::Convert
- Inherits:
-
Object
- Object
- HarmonizedTariff::Convert
- Defined in:
- lib/harmonized_tariff/convert.rb
Instance Attribute Summary collapse
-
#delimeter ⇒ Object
Returns the value of attribute delimeter.
-
#encoding ⇒ Object
Returns the value of attribute encoding.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
- #build_heirarchy(parent, depth, index) ⇒ Object
- #buildSQL(parent) ⇒ Object
- #escape(text) ⇒ Object
-
#initialize ⇒ Convert
constructor
A new instance of Convert.
- #load ⇒ Object
- #sqlRow(row) ⇒ Object
- #toJSON ⇒ Object
- #toSQL ⇒ Object
- #toXML ⇒ Object
Constructor Details
#initialize ⇒ Convert
Returns a new instance of Convert.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/harmonized_tariff/convert.rb', line 12 def initialize spec = Gem::Specification.find_by_name("harmonized_tariff") gem_root = spec.gem_dir @source = gem_root + '/data/1300_HTS_delimited.txt' @encoding = 'ISO8859-1' @delimeter = '|' @raw = Array.new end |
Instance Attribute Details
#delimeter ⇒ Object
Returns the value of attribute delimeter.
10 11 12 |
# File 'lib/harmonized_tariff/convert.rb', line 10 def delimeter @delimeter end |
#encoding ⇒ Object
Returns the value of attribute encoding.
10 11 12 |
# File 'lib/harmonized_tariff/convert.rb', line 10 def encoding @encoding end |
#source ⇒ Object
Returns the value of attribute source.
10 11 12 |
# File 'lib/harmonized_tariff/convert.rb', line 10 def source @source end |
Instance Method Details
#build_heirarchy(parent, depth, index) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/harmonized_tariff/convert.rb', line 138 def build_heirarchy(parent, depth, index) siblings = Array.new while index < @raw.length current = Hash.new current[:number], current[:suffix], level, current[:description], current[:unit], current[:col1_rate], current[:special_rate], current[:col2_rate], current[:note] = @raw[index] if level.nil? index += 1 next end if level.to_i == depth index += 1 siblings.push self.build_heirarchy(current, depth + 1, index) next end if level.to_i < depth if siblings.length > 0 parent[:children] = siblings end return parent end index += 1 end if siblings.length > 0 parent[:children] = siblings end return parent end |
#buildSQL(parent) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/harmonized_tariff/convert.rb', line 60 def buildSQL(parent) sql = '' unless parent[:number].nil? sql << self.sqlRow(parent) end if parent[:children].nil? return sql end parent[:children].each do |child| child[:description] = parent[:description] + ' ' + child[:description] sql << self.buildSQL(child) end return sql end |
#escape(text) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/harmonized_tariff/convert.rb', line 112 def escape(text) text = text.gsub(/'/) {|s| "\\'"} text = text.gsub(/"/) {|s| "\""} text = text.gsub(/\\/) {|s| "\\"} return text end |
#load ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/harmonized_tariff/convert.rb', line 122 def load parent = nil previous = nil @raw = CSV.read(@source, col_sep: @delimeter, encoding: @encoding, quote_char: "\x00") root = self.build_heirarchy(Hash.new, 0, 1) @data = root[:children] @raw = nil root = nil end |
#sqlRow(row) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/harmonized_tariff/convert.rb', line 84 def sqlRow(row) clean = Hash.new row.each do |k,v| if k == :children next end if v.nil? clean[k] = 'NULL' else clean[k] = "'" + self.escape(v) + "'" end end sql = <<-SQL.unindent INSERT INTO `harmonized_tariffs` (`id`, `code`, `suffix`, `description`, `unit`, `rate_1`, `special_rate`, `rate_2`, `notes`) VALUES (NULL, #{clean[:number]}, #{clean[:suffix]}, #{clean[:description]}, #{clean[:unit]}, #{clean[:col1_rate]}, #{clean[:special_rate]}, #{clean[:col2_rate]}, #{clean[:note]}); SQL return sql end |
#toJSON ⇒ Object
24 25 26 |
# File 'lib/harmonized_tariff/convert.rb', line 24 def toJSON return @data.to_json end |
#toSQL ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/harmonized_tariff/convert.rb', line 32 def toSQL sql = <<-SQL.unindent -- create the table CREATE TABLE `harmonized_tariffs` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `code` varchar(255) NOT NULL DEFAULT '', `suffix` varchar(255) DEFAULT NULL, `description` text, `unit` varchar(255) DEFAULT NULL, `rate_1` varchar(255) DEFAULT NULL, `special_rate` varchar(255) DEFAULT '', `rate_2` varchar(255) DEFAULT NULL, `notes` text, PRIMARY KEY (`id`), KEY `tariff` (`code`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- insert the flattened data SQL @data.each do |child| sql << self.buildSQL(child) end return sql end |
#toXML ⇒ Object
28 29 30 |
# File 'lib/harmonized_tariff/convert.rb', line 28 def toXML return @data.to_xml(:root => 'tariffs') end |