Sha256: 6158478631160a51466b16f03b9fa4a20b6acb6c5affe2fafe7d8766e86fb618

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 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'
      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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
watir-performance-0.7.0 lib/idl_extractor.rb
watir-performance-0.6.4 lib/idl_extractor.rb
watir-performance-0.6.3 lib/idl_extractor.rb