Sha256: 2c47d35bf88b74297a266bec59ed69b4b8bad94b5ff23813c637d21997f7ee7f

Contents?: true

Size: 1.24 KB

Versions: 15

Compression:

Stored size: 1.24 KB

Contents

require "rubygems"
require "nokogiri"
require "hpricot"
require "parsley"
require "benchmark"
require "pp"

YELP_HTML = File.dirname(__FILE__) + "/yelp.html"

def noko
  parse Nokogiri.Hpricot(File.open(YELP_HTML))
end

def hpri
  parse Hpricot(File.open(YELP_HTML))  
end

def parse(doc)
  out = {}
  out["name"] = (doc / "h1").first.inner_text
  out["phone"] = (doc / "#bizPhone").first.inner_text
  out["address"] = (doc / "address").first.inner_text
  out["reviews"] = (doc / ".nonfavoriteReview").map do |node|
    review = {}
    review["date"] = (node / ".ieSucks .smaller").first.inner_text
    review["user_name"] = (node / ".reviewer_info a").first.inner_text
    review["comment"] = (node / ".review_comment").first.inner_text
    review
  end
end

def pars
  parslet = Parsley.new({
    "name" => "h1",
    "phone" => "#bizPhone",
    "address" => "address",
    "reviews(.nonfavoriteReview)" => [
      {
        "date" => ".ieSucks .smaller",
        "user_name" => ".reviewer_info a",
        "comment" => ".review_comment"
      }
    ]
  })
  pp parslet.parse(:file => YELP_HTML)
end

Benchmark.bm do |x|
  x.report("nokogiri: ")  { 3.times { noko } }
  x.report("hpricot: ")   { 3.times { hpri } }
  x.report("parsley: ") { 3.times { pars } }
end

Version data entries

15 entries across 15 versions & 5 rubygems

Version Path
fizx-parsley-ruby-0.1.2 test/yelp-benchmark.rb
fizx-parsley-ruby-0.2.0 test/yelp-benchmark.rb
fizx-parsley-ruby-0.3.0 test/yelp-benchmark.rb
fizx-parsley-ruby-0.3.1 test/yelp-benchmark.rb
fizx-parsley-ruby-0.3.2 test/yelp-benchmark.rb
fizx-parsley-ruby-0.4.0 test/yelp-benchmark.rb
gtl-parsley-ruby-0.5.0 test/yelp-benchmark.rb
le1t0-parsley-ruby-0.4.5.001 test/yelp-benchmark.rb
parsley-ruby-0.4.5 test/yelp-benchmark.rb
edge-parsley-ruby-0.4.5 test/yelp-benchmark.rb
parsley-ruby-0.4.4 test/yelp-benchmark.rb
edge-parsley-ruby-0.4.3 test/yelp-benchmark.rb
parsley-ruby-0.4.3 test/yelp-benchmark.rb
parsley-ruby-0.4.2 test/yelp-benchmark.rb
parsley-ruby-0.4.1 test/yelp-benchmark.rb