MacCracken/watir-performance

View on GitHub
lib/idl_extractor.rb

Summary

Maintainability
A
0 mins
Test Coverage
require 'nokogiri'
require 'open-uri'
require 'zomg'
require 'active_support/all'

# http://w3c-test.org/webperf/specs/NavigationTiming no longer available
# archived https://web.archive.org/web/20110322032757/http://w3c-test.org/webperf/specs/NavigationTiming/
w3c_nav = 'https://www.w3.org/TR/navigation-timing-2/'
w3c_nav = 'https://web.archive.org/web/20110322032757/http://w3c-test.org/webperf/specs/NavigationTiming/'
doc = Nokogiri::HTML(URI.open(w3c_nav))
idl = []
doc.css('pre.idl').each { |content| idl << content.text }

nodes = []
idl.each do |spec|
  puts spec.inspect
  puts '----'
  #spec = spec.gsub('[Exposed=Window]', '')
  nodes << ZOMG::IDL.parse(spec) unless ZOMG::IDL.parse(spec).nil?
end

results = {}
results[:class] = 'Performance'
results[:interfaces] = {}
nodes.each do |node|
  interface_name = node.children.first.header.name.gsub('Performance', '')
  results[:interfaces][interface_name] = {}
  results[:interfaces][interface_name][:attrs] = []
  results[:interfaces][interface_name][:constants] = {}
  node.children.first.children.each do |child|
    case child.class.to_s
    when 'ZOMG::IDL::Nodes::Attribute'
      results[:interfaces][interface_name][:attrs] <<
        child.children.first.name.underscore
    when 'ZOMG::IDL::Nodes::Constant'
      consts = results[:interfaces][interface_name][:constants]
      consts[child.name] = child.children.children
    end
  end
end

open(File.expand_path(File.dirname(__FILE__)) + "/#{results[:class].underscore}.rb", 'w') do |f|
  f.puts '# auto-generated by idl_extractor.rb'
  f.puts "class #{results[:class]}"
  results[:interfaces].each_key do |interface_name|
    f.puts ' ' * 2 + "class #{interface_name}"
    results[:interfaces][interface_name][:constants].each do |k, v|
      f.puts ' ' * 4 + "#{k} = #{v.to_i}"
    end
    f.puts ' ' * 4 + 'attr_reader ' +
      results[:interfaces][interface_name][:attrs].collect! {|x| ":"+x}.join(", ")
    f.puts <<-'eos'
    def initialize(args)
      args.each do |k,v|
        instance_variable_set("@#{k}", v) if respond_to?(k.to_sym) unless v.nil?
      end
    end
    eos
    f.puts ' ' * 2 + 'end'
  end
  f.puts 'end'
end