Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Commit f54d7df

Browse files
committed
refactored ip_geo for specific inputs
1 parent 04c3b88 commit f54d7df

File tree

1 file changed

+73
-40
lines changed

1 file changed

+73
-40
lines changed

ip_utils/ip_geo.rb

Lines changed: 73 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,86 @@
33

44
require 'rest-client'
55
require 'json'
6+
require 'optparse'
67

7-
def ip_location
8+
9+
options = {}
10+
OptionParser.new do |opts|
11+
opts.banner = "Usage: example.rb [options]"
12+
13+
opts.on("-m", "--machine VARIABLE", "Machine Readable") do |v|
14+
options[:var] = v
15+
end
16+
end.parse!
17+
18+
# Ex. ip
19+
opt = options[:var]
20+
21+
# Ex. 8.8.8.8
22+
*arg = ARGV
23+
24+
if opt == nil && arg == nil
825
puts
926
print 'IP => '
1027
ip = gets.chomp
1128

12-
if ip == 'quit' || ip == 'exit'
29+
url = "http://ip-api.com/json/#{ip}"
30+
response = RestClient.get(url)
31+
info = JSON.parse(response)
32+
33+
if info['status'] == 'fail'
34+
puts 'No IP found'
35+
else
1336
puts
14-
exit(1)
37+
puts '============================='
38+
puts "| IP: #{info['query']}"
39+
puts "| City: #{info['city']}"
40+
puts "| Region: #{info['region']}"
41+
puts "| Country: #{info['country']}"
42+
puts "| ZIP: #{info['zip']}"
43+
puts "| ISP: #{info['isp']}"
44+
puts '============================='
45+
puts
46+
end
47+
elsif opt == nil && arg != nil
48+
url = "http://ip-api.com/json/#{arg.join}"
49+
response = RestClient.get(url)
50+
info = JSON.parse(response)
51+
52+
if info['status'] == 'fail'
53+
puts 'No IP found'
1554
else
55+
puts
56+
puts '============================='
57+
puts "| IP: #{info['query']}"
58+
puts "| City: #{info['city']}"
59+
puts "| Region: #{info['region']}"
60+
puts "| Country: #{info['country']}"
61+
puts "| ZIP: #{info['zip']}"
62+
puts "| ISP: #{info['isp']}"
63+
puts '============================='
64+
puts
65+
end
66+
elsif opt != nil && arg != nil
67+
url = "http://ip-api.com/json/#{arg.join}"
68+
response = RestClient.get(url)
69+
info = JSON.parse(response)
1670

17-
begin
18-
url = "http://ip-api.com/json/#{ip}"
19-
response = RestClient.get(url)
20-
info = JSON.parse(response)
21-
rescue
22-
puts
23-
puts 'IP not found.'
24-
puts
25-
exit(1)
26-
end
27-
28-
ip = info['query']
29-
city = info['city']
30-
region = info['region']
31-
country = info['country']
32-
isp = info['isp']
33-
zip = info['zip']
34-
35-
if city.nil?
36-
puts
37-
puts 'IP not found.'
38-
puts
39-
exit(1)
40-
else
41-
puts
42-
puts '============================='
43-
puts "| IP: #{ip}"
44-
puts "| City: #{city}"
45-
puts "| Region: #{region}"
46-
puts "| Country: #{country}"
47-
puts "| ZIP: #{zip}"
48-
puts "| ISP: #{isp}"
49-
puts '============================='
50-
puts
51-
end
71+
if opt == 'ip'
72+
puts info['query']
73+
elsif opt == 'city'
74+
puts info['city']
75+
elsif opt == 'region'
76+
puts info['region']
77+
elsif opt == 'country'
78+
puts info['country']
79+
elsif opt == 'zip'
80+
puts info['zip']
81+
elsif opt == 'isp'
82+
puts info['isp']
83+
else
84+
puts 'Bad input'
5285
end
86+
else
87+
puts 'Bad input'
5388
end
54-
55-
ip_location

0 commit comments

Comments
 (0)