Class: ExchangeRatesGenerator::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/exchange-rates-generator/cli.rb

Class Method Summary collapse

Class Method Details

.execute(stdout, arguments = []) ⇒ Object



5
6
7
8
9
10
11
12
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/exchange-rates-generator/cli.rb', line 5

def self.execute(stdout, arguments=[])

  # NOTE: the option -p/--path= is given as an example, and should be replaced in your application.

  options = {
    :path     => nil,
    :source   => 'ecb',
    :format   => 'ruby'
  }
  mandatory_options = %w( currency )

  parser = OptionParser.new do |opts|
    opts.banner = <<-BANNER.gsub(/^          /,'')
      This application can generate helpful classes that can do currency exchange from a single target currency to numerous others.

      Usage: #{File.basename($0)} [options]

      Options are:
    BANNER
    opts.separator ""
    opts.on("-v", "--version", String,
            "Displays the current version number of this Gem") { |arg| stdout.puts "#{File.basename($0)} #{ExchangeRatesGenerator::VERSION}"; exit }
    opts.on("-c", "--currency CURRENCY", String,
            "This is the base currency you wish to convert from.") { |arg| options[:currency] = arg }
    opts.on("-p", "--path PATH", String,
            "The is the default output path for the exchange rate converter.",
            "Default: '.'") { |arg| options[:path] = arg }
    opts.on("-s", "--source SOURCE", String,
            "This is the name of the desired Source to retrieve the currency data from.",
            "To get a list of all available Sources using the -l or --list flags.",
            "Default: ecb") { |arg| options[:source] = arg.upcase }
    opts.on("-f", "--format Format", String,
            "This is the name of the desired output format of the exchange rate converter, the choice of format depends on the programming language you intend to use the converter from.",
            "To get a list of all available formats using the -l or --list flags.",
            "Default: ruby") { |arg| options[:format] = arg.upcase }

    opts.on("-l", "--list", "List all available formats and sources") do
      formatters = ExchangeRatesGenerator::Formatters::Base.formatters.collect {|f| "\s\s#{f.to_s.demodulize}: #{f.description} (*.#{f.default_extension.to_s})" }.join("\n")
      sources   = ExchangeRatesGenerator::Sources::Base.sources.collect {|s| "\s\s#{s.to_s.demodulize}: #{s.description}" }.join("\n")

      stdout.puts <<-EOS
Available Formats:
#{formatters}

Available Sources:
#{sources}
      EOS

      exit
    end
  opts.on("-h", "--help",
            "Show this help message.") { stdout.puts opts; exit }
    opts.parse!(arguments)

    if mandatory_options && mandatory_options.find { |option| options[option.to_sym].nil? }
      stdout.puts opts
      return
    end
  end

  formatter = ::ExchangeRatesGenerator::Formatters.get(options[:format].downcase)
  unless formatter
    stdout.puts "Sorry, I couldn't find a formatter for the format '#{options[:format]}', use the -l or --list flag to see the complete list of available formats."
    exit
  end

  source = ::ExchangeRatesGenerator::Sources.get(options[:source].downcase)
  unless source
    stdout.puts "Sorry, I couldn't find the source '#{options[:source]}', use the -l or --list flag to see the complete list of available sources."
    exit
  end

  path = options[:path] || "."
  path = File.join(path, "#{options[:currency].to_s.downcase}_exchange_rates.#{formatter.default_extension.to_s}")

  begin
    File.open(path, 'w') do |file|
      file.write formatter.new(options[:currency], source.rates_for(options[:currency]))
    end

    # TODO: Catch some exceptions?
  rescue ::ExchangeRatesGenerator::Errors::ExchangeRatesError => e
    stdout.puts e.message
    exit
  end

  stdout.puts "Exchange rate converted successfully. Find it at: #{path}"
end