class WeatherSage::CLI::Commands::BaseForecastCommand
Base forecast command.
Used by ForecastCommand and HourlyCommand.
Public Class Methods
new(ctx, app)
click to toggle source
Do not invoke this method directly; subclass this class and override the run
method.
Calls superclass method
WeatherSage::CLI::Commands::Command::new
# File lib/weather-sage/cli/commands/base-forecast.rb, line 11 def initialize(ctx, app) super(ctx, app) @forecast_method = self.class.const_get(:FORECAST_METHOD) end
Public Instance Methods
run(args)
click to toggle source
Run command.
# File lib/weather-sage/cli/commands/base-forecast.rb, line 19 def run(args) # get mode and args mode, args = parse_args(args) CSV(STDOUT) do |csv| # write column names csv << columns(mode).map { |col| col[:name] } args.each do |arg| # geocode argument, get first point if pt = geocode(arg).first # walk forecast periods pt.point.send(@forecast_method).periods.each do |p| csv << make_row(mode, arg, p) end end end end end
Private Instance Methods
columns(mode)
click to toggle source
Get columns for given mode.
# File lib/weather-sage/cli/commands/base-forecast.rb, line 67 def columns(mode) ::WeatherSage::CLI::Forecast::columns(@forecast_method, mode) end
make_row(mode, address, p)
click to toggle source
Convert forecast period to CSV row.
# File lib/weather-sage/cli/commands/base-forecast.rb, line 58 def make_row(mode, address, p) [address] + columns(mode).select { |col| col[:prop] }.map { |col| p.data[col[:prop]] } end
parse_args(args)
click to toggle source
Extract mode and args from command-line arguments.
# File lib/weather-sage/cli/commands/base-forecast.rb, line 44 def parse_args(args) case args.first when /^-f|--full$/ [:full, args[1 .. -1]] when /^-b|--brief$/ [:brief, args[1 .. -1]] else [:brief, args] end end