Class: Wettr::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/wettr/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ CLI

Returns a new instance of CLI.



4
5
6
# File 'lib/wettr/cli.rb', line 4

def initialize(args = nil)
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



2
3
4
# File 'lib/wettr/cli.rb', line 2

def args
  @args
end

Instance Method Details

#parse_argsObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/wettr/cli.rb', line 18

def parse_args
  if args.include?("--help")
    print_help_menu
  elsif args.include?("--version")
    puts "wettr #{Wettr::VERSION}"
  elsif zip = args[args.index("--zip") + 1]
    weather = Wettr::Weather.new_with_zip(zip)
    weather.print
  else
    puts "Please enter a zip code"
  end
end


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/wettr/cli.rb', line 31

def print_help_menu
  help_text = <<-HELP_TEXT
  Usage:
    wettr [options]
  \nOptions:
    --help                           # Print this menu
    --version                        # Print the version number
    --zip ZIP_CODE                   # Weather by zip/postal code
  \nDescription:
    wettr is a command line ruby gem to get current weather information
    using OpenWeatherMap's Current Weather API in conjunction with the
    ipapi.co IP address api.
  \n  wettr can get current weather data using either a zip/postal code
    or an IP address. Simply type wettr to get started.
  \nExamples:
    wettr                            # Weather by current IP address location
    wettr --zip 10001                # Weather by zip/postal code for New York City
    wettr --zip 20001                # Weather by zip/postal code for Washington D.C.
  HELP_TEXT
  
  puts help_text
end

#runObject



8
9
10
11
12
13
14
15
16
# File 'lib/wettr/cli.rb', line 8

def run
  if !args.empty?
    parse_args
  else
    ip = Wettr::IP.new_without_ip
    weather = Wettr::Weather.new_with_lat_and_lon(lat: ip.lat, lon: ip.lon)
    weather.print
  end
end