Class: WifiWand::Commands::PublicIp

Inherits:
Base
  • Object
show all
Defined in:
lib/wifi_wand/commands/public_ip.rb

Constant Summary collapse

VALID_SELECTORS =
{
  'address' => 'address',
  'a'       => 'address',
  'country' => 'country',
  'c'       => 'country',
  'both'    => 'both',
  'b'       => 'both',
}.freeze

Constants inherited from Base

Base::DEFAULT_INVOCATION_OPTIONS

Instance Attribute Summary

Attributes inherited from Base

#cli, #metadata

Instance Method Summary collapse

Methods inherited from Base

#aliases, allow_invocation_options, allowed_invocation_options, #bind, binding_sources, binds, command_metadata, declared_metadata, #initialize, #validate_options

Constructor Details

This class inherits a constructor from WifiWand::Commands::Base

Instance Method Details

#call(*args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/wifi_wand/commands/public_ip.rb', line 45

def call(*args)
  validate_max_arguments!(args, 1)

  selector = args.empty? ? 'both' : args.first
  normalized_selector = normalize_selector(selector)

  case normalized_selector
  when 'address'
    address = model.public_ip_address
    output_support.handle_output(address, -> { "Public IP Address: #{address}" })
  when 'country'
    country = model.public_ip_country
    output_support.handle_output(country, -> { "Public IP Country: #{country}" })
  when 'both'
    info = model.public_ip_info
    output_support.handle_output(info, -> {
      "Public IP Address: #{info['address']}  Country: #{info['country']}"
    })
  end
end

#help_textObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wifi_wand/commands/public_ip.rb', line 28

def help_text
  <<~HELP
    #{.usage}

    #{.description}

    Selectors:
      address (a) - return only the public IP address
      country (c) - return only the public IP country
      both (b)    - return both address and country

    Providers:
      address      - uses https://api.ipify.org
      country/both - uses https://api.country.is/
  HELP
end