class MeteoPl::Utility::Command

Constants

DEFAULT_PERIOD
DEFAULT_TIMEOUT
PERIODS

Attributes

args[R]
location[R]
options[R]

Public Class Methods

new(args) click to toggle source
# File lib/meteo_pl/utility/command.rb, line 12
def initialize(args)
  @args = args
  @options = {
    timeout: DEFAULT_TIMEOUT,
    period: PERIODS[DEFAULT_PERIOD]
  }
end

Public Instance Methods

call() click to toggle source
# File lib/meteo_pl/utility/command.rb, line 20
def call
  parse_input
  set_location
  valid?
ensure
  print_help unless valid?
end

Private Instance Methods

option_parser() click to toggle source
# File lib/meteo_pl/utility/command.rb, line 56
def option_parser
  @option_parser ||= OptionParser.new do |opts|
    opts.banner = 'Usage: LOCATION [options]'

    opts.on(
      '-t', '--timeout TIMEOUT', /^([1-9]|[1-5][0-9]|60)$/,
      'Provide request timeout, value from 1s to 60s, default: 2s'
    ) do |timeout|
      options[:timeout] = timeout.first.to_i
    end

    opts.on(
      '-p', '--period PERIOD', /short|long/,
      'Provide forecast period, either short (60h) or long (84h),' \
      ' default: short'
    ) do |period|
      options[:period] = PERIODS[period.to_sym]
    end

    opts.on('-h', '--help', 'Prints this help') do
      puts opts
    end
  end
end
parse_input() click to toggle source
# File lib/meteo_pl/utility/command.rb, line 41
def parse_input
  option_parser.parse!(args)
rescue OptionParser::InvalidArgument, OptionParser::MissingArgument => e
  @invalid = true
  puts e.message
end
print_help() click to toggle source
set_location() click to toggle source
# File lib/meteo_pl/utility/command.rb, line 48
def set_location
  return unless valid?
  @location = args.join(' ')
  return unless @location.empty?
  @invalid = true
  puts 'LOCATION is not provided'
end
valid?() click to toggle source
# File lib/meteo_pl/utility/command.rb, line 32
def valid?
  !@invalid
end