Class: WeatherSage::CLI::Commands::BaseForecastCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/weather-sage/cli/commands/base-forecast.rb

Overview

Base forecast command.

Used by ForecastCommand and HourlyCommand.

Direct Known Subclasses

ForecastCommand, HourlyCommand

Instance Method Summary collapse

Methods inherited from Command

run

Constructor Details

#initialize(ctx, app) ⇒ BaseForecastCommand

Do not invoke this method directly; subclass this class and override the run method.



11
12
13
14
# 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

Instance Method Details

#run(args) ⇒ Object

Run command.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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