Class: Kscript::KkIpLookupUtils

Inherits:
Base
  • Object
show all
Defined in:
lib/kscript/plugins/kk_ip_lookup_utils.rb

Constant Summary collapse

IP_API_BASE_URL =
'http://ip-api.com/json'
IP_CHECK_URL =
'https://api.ipify.org?format=json'

Instance Attribute Summary collapse

Attributes inherited from Base

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#human_output?, inherited, #with_error_handling

Constructor Details

#initialize(*args, **opts) ⇒ KkIpLookupUtils

Returns a new instance of KkIpLookupUtils.



17
18
19
20
# File 'lib/kscript/plugins/kk_ip_lookup_utils.rb', line 17

def initialize(*args, **opts)
  super
  @ip_address = args.first || fetch_public_ip
end

Instance Attribute Details

#ip_addressObject (readonly)

Returns the value of attribute ip_address.



15
16
17
# File 'lib/kscript/plugins/kk_ip_lookup_utils.rb', line 15

def ip_address
  @ip_address
end

Class Method Details

.argumentsObject



37
38
39
# File 'lib/kscript/plugins/kk_ip_lookup_utils.rb', line 37

def self.arguments
  '<ip_address>'
end

.authorObject



49
50
51
# File 'lib/kscript/plugins/kk_ip_lookup_utils.rb', line 49

def self.author
  'kk'
end

.descriptionObject



53
54
55
# File 'lib/kscript/plugins/kk_ip_lookup_utils.rb', line 53

def self.description
  'Query IP geolocation and ISP info.'
end

.groupObject



45
46
47
# File 'lib/kscript/plugins/kk_ip_lookup_utils.rb', line 45

def self.group
  'network'
end

.usageObject



41
42
43
# File 'lib/kscript/plugins/kk_ip_lookup_utils.rb', line 41

def self.usage
  "kscript ip_lookup <ip_address>\nkscript ip_lookup 8.8.8.8"
end

Instance Method Details

#fetch_locationObject



31
32
33
34
35
# File 'lib/kscript/plugins/kk_ip_lookup_utils.rb', line 31

def fetch_location
  validate_ip_address!
  response = make_api_request
  handle_response(response)
end

#run(*args, **_opts) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/kscript/plugins/kk_ip_lookup_utils.rb', line 22

def run(*args, **_opts)
  with_error_handling do
    ip = args.first || @ip_address
    validate_ip_address!(ip)
    response = make_api_request(ip)
    handle_response(response)
  end
end