Class: HarmonizedTariff::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/harmonized_tariff/cli.rb

Constant Summary collapse

@@type =
'json'
@@verbose =
false
@@destination =
'~/'

Class Method Summary collapse

Class Method Details

.gzip(data) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/harmonized_tariff/cli.rb', line 88

def self.gzip(data)

  path = File.expand_path(@@destination) + File::SEPARATOR + 'hts.' + @@type

  puts 'Outputting converted gzipped SQL to: ' + path + "\n"

  File.open(path, 'w') do |f|
    gz = Zlib::GzipWriter.new(f)
    gz.write data
    gz.close
  end

end

.output(data) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/harmonized_tariff/cli.rb', line 72

def self.output(data)

  path = File.absolute_path(@@destination) + File::SEPARATOR + 'hts.' + @@type

  puts 'Outputting converted ' + @@type.upcase + ' to: ' + path + "\n"

  target = File.new(path, 'w')
  target.write data
  target.close

  if @@verbose
    puts data
  end

end

.parse(args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/harmonized_tariff/cli.rb', line 13

def self.parse(args)

  hts = HarmonizedTariff::Convert.new

  opt_parser = OptionParser.new do |opts|

    opts.banner = "Usage: bundle exec bin/hts [options]"

    opts.separator ""
    opts.separator "Specific options:"

    opts.on("-s", "--source [PATH]", "Set source file") do |source|
      hts.source = source || hts.source
    end

    opts.on("-o", "--output [PATH]", "Set destination folder path") do |path|
      @@destination = path || destination
    end

    opts.on("--type [TYPE]", "Select output format (json, xml, sql, gz (gzipped sql))") do |t|
      @@type = t.downcase
    end

    opts.on("-v", "--verbose", "Print output to screen") do |d|
      @@verbose = d
    end

    opts.separator ""
    opts.separator "Common options:"

    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    opts.on_tail("--version", "Show version") do
      puts HarmonizedTariff::VERSION
      exit
    end

  end

  opt_parser.parse!(args)

  # parse the file
  hts.load

  if @@type == 'json'
    self.output hts.toJSON
  elsif @@type == 'xml'
    self.output hts.toXML
  elsif @@type == 'sql'
    self.output hts.toSQL
  elsif @@type == 'gz'
    self.gzip hts.toSQL
  end

end