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

#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