|
3 | 3 |
|
4 | 4 | require 'rest-client' |
5 | 5 | require 'json' |
| 6 | +require 'optparse' |
6 | 7 |
|
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 |
8 | 25 | puts |
9 | 26 | print 'IP => ' |
10 | 27 | ip = gets.chomp |
11 | 28 |
|
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 |
13 | 36 | 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' |
15 | 54 | 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) |
16 | 70 |
|
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' |
52 | 85 | end |
| 86 | +else |
| 87 | + puts 'Bad input' |
53 | 88 | end |
54 | | - |
55 | | -ip_location |
|
0 commit comments