Skip to content

Instantly share code, notes, and snippets.

@danchoi
Created January 28, 2009 04:09
Show Gist options
  • Save danchoi/53805 to your computer and use it in GitHub Desktop.
Save danchoi/53805 to your computer and use it in GitHub Desktop.
yelp neighborhoods to yaml
#!/usr/bin/env ruby
require 'rubygems'
require 'hpricot'
require 'open-uri'
url = "http://www.yelp.com/developers/documentation/neighborhood_list"
doc = open(url) {|f| Hpricot(f.read)}
def nest(li_elem)
next_sibling = li_elem.next_sibling
if next_sibling && next_sibling.name == "ul"
label = li_elem.inner_text
{label => next_sibling.children_of_type("li").map {|e| nest(e)}}
else
li_elem.inner_text
end
end
ul_root = doc.at("ul.attr_list")
result = ul_root.children_of_type("li").map do |country|
nest(country)
end
puts result.to_yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment