Last active
April 24, 2023 06:44
-
-
Save dkam/4956cf770a9e4a604f8cea8eb467f254 to your computer and use it in GitHub Desktop.
Basic http client which can process html and xml
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
#!/usr/bin/env ruby | |
require 'open-uri' | |
require 'nokogiri' | |
VERSION = 0.1 | |
def getter(url) | |
URI.open(url, "User-Agent" => "Curlr/#{VERSION}") | |
rescue OpenURI::HTTPError => e | |
e.io | |
end | |
url = ARGV[0] | |
abort("#{File.basename(__FILE__)} url") if url.nil? || url.empty? | |
if url.start_with?('http') | |
client = getter(url) | |
case client.meta['content-type'] | |
when %r{text/xml} | |
puts Nokogiri::XML(client.read) | |
when %r{text/html} | |
puts Nokogiri::HTML(client.read) | |
else | |
puts client.read | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment