Sha256: d0ccfd883f20aafcbe4e33e83784658407d7ba96b1750f78d97448af5dd6899f

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

require 'nokogiri'
require 'open-uri'
require 'zomg'
require 'activesupport'

doc = Nokogiri::HTML(open('http://w3c-test.org/webperf/specs/NavigationTiming/'))
idl = []
doc.css('pre.idl').each do |content|
  idl << content.text
end
nodes = []
idl.each do |spec|
  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"
      results[:interfaces][interface_name][:constants][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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
watir-performance-0.6.1 lib/idl_extractor.rb
watir-performance-0.6.0 lib/idl_extractor.rb