Learning Enough to be Dangerous – Go From Being a Novice to an Advanced Developer

current courses at Learn Enough to be Dangerous

While I think the venerable Ruby on Rails Tutorial is still the best way to learn Rails, jumping into this tutorial can be challenging for those who are new to programming.

With the fourth and latest beta of Rails 5 being released this past week, it is appropriate that Michael Hartl, the creator of the Rails Tutorial, has devised yet another tool to help new and experienced developers learn enough to make the most of it.

Appropriately, the title of this site is “Learn Enough to Be Dangerous”.

current courses at Learn Enough to be Dangerous

As of the end of April 2016, only three “Learn Enough” courses are available: Command Line, Text Editor, and Git. However, future courses including HTML, CSS & Layout, JavaScript, Ruby, Sinatra, and the Ruby on Rails Tutorial for Rails 5 are scheduled to be added.

The cost is $29 per month, the same as Code School (if you subscribe on a per-month basis to CS). Unlike CS, there is not yet a discounted yearly rate.

Since I’m excited to see the new Rails Tutorial as soon as it’s released, I’ve decided to sign up. Were it not for the access to this mammoth resource, I don’t know that I would pay this price – but considering that the Rails Tutorial with screencasts generally costs about $150 by itself, it seems like a reasonable deal. At any rate, I’ve come to expect only the highest quality learning materials from Dr. Hartl, so I am definitely looking forward to the upcoming classes.

Using Ruby to Get All Links from a Sitemap XML File

a sitemap.xml file

I was looking at the cached pages on the Wayback Machine and decided to find out if that site had an API. (It does.) I wanted to find a way to use this API to submit links to the Wayback Machine database. As it turns out, a Ruby gem called WaybackArchiver has already been written for just this purpose!

Now that I had an easy way to submit multiple URLs, I got to thinking about how this could be more easily automated. What if I had a sitemap that contained all of the links I wanted to submit? What if there is already a gem to parse a sitemaps.org-compliant sitemap, such as the one on WordPress sites that use the Google Sitemap Generator Plugin?

a sitemap.xml file
My WordPress sitemap, generated by the Google Sitemap Generator Plugin

Even though all of these things exist, I have not found where they have been combined, so I did just that. The Ruby script I wrote requires several gems:
1. WaybackArchiver, for submitting links, sitemaps, or pages to the Wayback Machine
2. Sitemap-Parser, for parsing sitemaps
3. OpenURI, for opening websites
4. Nokogiri, for parsing XML (also HTML, SAX, and Reader) files

If these gems are not installed already, you can install them at the Ruby prompt with “gem install” and the name of each gem.

The script, which I named map.rb, is below.

require 'wayback_archiver'
require 'sitemap-parser'
require 'open-uri'
require 'nokogiri'

mainSitemapURL = ARGV[0]
if not mainSitemapURL.nil?
  puts 'Running...' #+ mainSitemapURL

  #mainSitemap = SitemapParser.new mainSitemapURL
  mainSitemap = Nokogiri::HTML(open(mainSitemapURL))
  #puts mainSitemap
  mainSitemap.xpath("//sitemap/loc").each do |node|
    #puts node.content
    subSitemapURL = node.content
    subSitemap = SitemapParser.new subSitemapURL
    arraySubSitemap = subSitemap.to_a
    (0..arraySubSitemap.length-1).each do |j|
      #puts arraySubSitemap[j]
      WaybackArchiver.archive(arraySubSitemap[j], :url)
    end
  end
end
puts 'Finished.'

This script works unaltered with WordPress sitemaps created by the plugin mentioned above. These sitemaps actually produce a sitemap index, with individual sitemaps being linked here – so each of the linked sitemaps are also parsed. The above script can be run at the Ruby prompt with “ruby map.rb URL“, substituting the URL for the sitemap.

Should your sitemap be organized differently, this code may not work as-is, but may require changes depending on the node tag names.

Other pages that were helpful in developing this tool:
Looping through each xml node (on Stack Overflow)
Web Scraping with Ruby and Nokogiri for Beginners
Parsing an HTML/XML Document

The Third Edition of Michael Hartl’s Ruby on Rails Tutorial is Available on Amazon!

Ruby on Rails logo

The most widely known way (which is also often described as the best way) to learn Ruby on Rails is the Rails Tutorial by Michael Hartl. Its third edition, which covers Rails 4, has been available as both a digital book and video screencasts for several months on Hartl’s Rails Tutorial website, but was just released in print form yesterday. I received mine from Amazon today, and was pleased to find my review (of the Second Edition, though it certainly holds true for the Third even more so!) printed just before the title page.