-
Notifications
You must be signed in to change notification settings - Fork 0
/
locate_ip.rb
49 lines (42 loc) · 1.16 KB
/
locate_ip.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env ruby
require 'json'
require 'addressable'
require 'rest-client'
require 'byebug'
class GetIpInfo
def get_site_or_ip_address(site)
query = setup_ip_query(clean(site))
response = get_request(query) # max of 150 requests per minute
process_json_response(response.body)
end
private
def clean(site)
site[/((www)?\.?\w+\.\w+)/]
end
def process_json_response(response)
JSON.parse(response)
end
def get_request(url)
response = RestClient::Request.execute(method: :get, url: Addressable::URI
.parse(url) .normalize.to_str,
timeout: 5)
response
end
def setup_ip_query(url)
"http://ip-api.com/json/#{url}"
end
end
def start
puts '[locate_ip] ENTER URL OR IP ADDRESS'
url_or_ip = gets.chomp
puts "[locate_ip] PROCESSING REQUEST FOR #{url_or_ip}"
ip_info_obj = GetIpInfo.new
json_response = ip_info_obj.get_site_or_ip_address(url_or_ip)
if json_response['status'] == 'success'
puts "#{json_response}"
else
puts "[locate_ip] REQUEST FAILED TO RETURN VALID IP INFORMATION"
puts "#{json_response}"
end
end
start