#!/usr/bin/env ruby # Script created by and licensed to David Smalley (http://davidsmalley.com) under the MIT license # Please feel free to make modifications require 'rubygems' require 'xmlsimple' require 'fileutils' raise ArgumentError.new("Usage: ruby opml_spec.rb ") unless ARGV.size == 1 def parse_opml(opml) opml.delete("head") opml["body"][0]["outline"].each do |root| spec_file = [] spec_root_text = root["text"] root["outline"] && root["outline"].each do |child| spec_sub_text = child["text"] spec_file << "describe #{spec_root_text}, \"#{spec_sub_text}\" do\n" spec_file << "\n" child["outline"].each do |sub_child| spec_text = sub_child["text"] spec_file << " it \"#{spec_text}\" do\n" spec_file << "\n" spec_file << " end\n" spec_file << "\n" end spec_file << "\n" spec_file << "end\n" spec_file << "\n" end write_spec_file(spec_file, spec_root_text) end end def underscore(camel_cased_word) camel_cased_word.to_s.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end def write_spec_file(spec_file_data, root_text) file_name = root_text.gsub(/\s/, "") file_name = underscore(file_name) path = FileUtils.mkdir_p("./specs") file = File.new("#{path}/#{file_name}_spec.rb", "w") file.puts("#{spec_file_data}") file.close end opml = XmlSimple.xml_in(File.read(ARGV[0])) parse_opml(opml)