class WeatherSage::CLI::Commands::NowCommand

Implementation of now command-line command.

Constants

COL_NAMES

CSV column names.

HELP

Help for this command.

Used by the help command.

Public Instance Methods

run(args) click to toggle source

Run now command.

# File lib/weather-sage/cli/commands/now.rb, line 35
def run(args)
  CSV(STDOUT) do |csv|
    # write column names
    csv << COL_NAMES

    # iterate over command-line arguments and write each one
    args.each do |arg|
      # geocode to first point
      if pt = geocode(arg).first
        # get first station
        if st = pt.point.stations.first
          # get latest observation data
          data = st.latest_observations

          # write observations
          make_rows(arg, data) do |row|
            csv << row
          end
        end
      end
    end
  end
end

Private Instance Methods

make_rows(address, data, &block) click to toggle source

Map observation properties in result to CSV rows and yield each row.

FIXME: this is a bit of a hack.

# File lib/weather-sage/cli/commands/now.rb, line 67
def make_rows(address, data, &block)
  ::WeatherSage::Weather::Observation::PROPERTIES.each do |key, type|
    # get observation
    if v = data[key.to_s]
      # map observation to row, then yield row
      block.call(case type
      when :text, :time, :url
        [address, key, type, v]
      when :value
        [address, key, type, v['value'], v['unitCode'], v['qualityControl']]
      when :cloud
        # hack: only show data for first cloud layer
        base = v.first['base']
        [address, key, type, base['value'], base['unitCode']]
      else
        raise "unkown type: #{type}"
      end)
    end
  end
end