Created
March 3, 2011 02:49
-
-
Save nutrun/852228 to your computer and use it in GitHub Desktop.
Ruby strings that do web things
This file contains hidden or 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
# Earl: Strings that do web things | |
# | |
# | |
# Examples: | |
# | |
# text = "Blah blah http://tinyurl.com/4bnjzbu blah http://tinyurl.com/4tefu9f" | |
# | |
# text.urls # => ["http://tinyurl.com/4bnjzbu", "http://tinyurl.com/4tefu9f"] | |
# | |
# text.locations # => ["http://nutrun.com/weblog/2010/11/17/supercharged-ruby-console-output.html", "http://nutrun.com"] | |
# | |
# text.linkify # => "Blah blah <a href=\"http://tinyurl.com/4bnjzbu\">http://tinyurl.com/4bnjzbu</a> blah <a href=\"http://tinyurl.com/4tefu9f\">http://tinyurl.com/4tefu9f</a>" | |
# | |
# "http://tinyurl.com/4bnjzbu".location # => "http://nutrun.com/weblog/2010/11/17/supercharged-ruby-console-output.html" | |
# | |
# puts "http://nutrun.com/".get | |
# | |
# | |
# Public domain. Contact twitter.com/nutrun | |
# - | |
require 'uri' | |
require 'net/http' | |
class String | |
def urls | |
URI.extract(self, ['http', 'https']) | |
end | |
def location | |
final_url_destination(self) | |
end | |
def locations | |
self.urls.map { |url| url.location } | |
end | |
def linkify | |
self.urls.inject(self) { |s, u| s.gsub(u, %Q{<a href="#{u}">#{u}</a>}) } | |
end | |
def linkify! | |
self.urls.each { |u| self.gsub!(url, %Q{<a href="#{u}">#{u}</a>}) } | |
end | |
def get | |
uri = URI.parse(self) | |
path = uri.path | |
path = '/' if path.empty? | |
path = "#{path}?#{uri.query}" if uri.query.any? | |
Net::HTTP.start(uri.host, uri.port) { |http| http.get(path) }.body | |
end | |
private | |
def final_url_destination(url, limit=10) | |
raise ArgumentError, 'HTTP redirect too deep' if limit == 0 | |
uri = URI.parse(url) | |
path = uri.path | |
path = '/' if path.empty? | |
path = "#{path}?#{uri.query}" if uri.query | |
response = nil | |
Net::HTTP.start(uri.host, uri.port) {|http| response = http.head(path) } | |
case response | |
when Net::HTTPSuccess | |
url | |
when Net::HTTPRedirection | |
final_url_destination(response['location'], limit - 1) | |
else | |
STDERR.puts url | |
response.error! | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment