Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Splint3r7/602c5b922942d67b3cc3b3653c4e59cf to your computer and use it in GitHub Desktop.
Save Splint3r7/602c5b922942d67b3cc3b3653c4e59cf to your computer and use it in GitHub Desktop.
4-line Ruby script that uses open-uri to fetch the contents of a URL & displays it in the console. This is the foundation for all web requests, whether to scrape a page, request a JSON response, and more.
# http://ruby-doc.org/stdlib-2.0.0/libdoc/open-uri/rdoc/OpenURI.html
require 'open-uri'
# Go fetch the contents of a URL & store them as a String
response = open('http://www.example.com').read
# "Pretty prints" the result to look like a web page instead of one long string of HTML
URI.parse(response).class
# Print the contents of the website to the console
puts "\n\n#{response.inspect}\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment