Skip to content

Instantly share code, notes, and snippets.

@dkam
Last active April 24, 2023 06:44
Show Gist options
  • Save dkam/4956cf770a9e4a604f8cea8eb467f254 to your computer and use it in GitHub Desktop.
Save dkam/4956cf770a9e4a604f8cea8eb467f254 to your computer and use it in GitHub Desktop.
Basic http client which can process html and xml
#!/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