Last active
August 29, 2015 14:22
-
-
Save krists/7967e6718a9cd611e31a to your computer and use it in GitHub Desktop.
What is my external IP address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "net/http" | |
require "json" | |
uri = URI('https://duckduckgo.com/?q=what+is+my+ip&format=json&no_html=1') | |
response = Net::HTTP.get_response(uri) | |
if response.is_a?(Net::HTTPSuccess) | |
parsed_response = JSON.parse(response.body) | |
answer = parsed_response["Answer"] | |
ip_regexp = %r/\A.*(\b(?:\d{1,3}\.){3}\d{1,3}\b).*\z/ | |
extract_results = answer.match(ip_regexp) | |
if extract_results | |
puts extract_results.captures[0] | |
else | |
raise "Could not find IP in answer \"#{answer}\"" | |
end | |
else | |
raise "Could not reach DuckDuckGo" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment