Sha256: f5ddab9827231b3fab1150115e58515da93136576cc7de7067efef601eb84940

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

#!/usr/bin/env ruby

require 'wor/weather'
require 'wor/weather/version'
require 'colorize'
require 'optparse'

VALID_UNITS = %i[metric imperial].freeze

options = {
  temp: false,
  unit: :metric,
  city: nil,
  api_key: nil
}

parser = OptionParser.new do |opts|
  opts.banner = 'Usage: wor-weather -k API_KEY -c CITY [-t] [-u imperial|metric]'

  opts.on('-t', '--temp', 'Returns the temperature of the city') do
    options[:temp] = true
  end

  opts.on('-c CITY', '--city CITY', 'City to be used') do |city|
    options[:city] = city
  end

  opts.on('-k KEY', '--key KEY', 'API key to be used') do |key|
    options[:api_key] = key
  end

  opts.on(
    '-u UNIT',
    '--unit UNIT',
    'Unit type to display. Valid options: imperial, metric. Default: metric'
  ) do |unit|

    unless VALID_UNITS.include? unit.to_sym
      puts "Invalid unit. Valid units are: #{VALID_UNITS.map(&:to_s)}".colorize(:red)
      exit
    end

    options[:unit] = unit.to_sym
  end

  opts.on('-v', '--version', 'Displays version') do
    puts "Wor-Weather Version: #{Wor::Weather::VERSION}".colorize(:blue)
    exit
  end

  opts.on('-h', '--help', 'Displays help') do
    puts opts
    exit
  end
end

parser.parse!

unless options[:city] && options[:api_key]
  puts 'City and API key are mandatory'.colorize(:red)
  exit
end

Wor::Weather.configure do |c|
  c.api_key = options[:api_key]
  c.unit_type = options[:unit]
end

if options[:temp]
  puts Wor::Weather.temp(options[:city])
else
  puts 'Invalid option'.colorize(:red)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wor-weather-0.1.0 bin/wor-weather