Skip to content

Instantly share code, notes, and snippets.

@Erreon
Created January 31, 2012 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Erreon/1709018 to your computer and use it in GitHub Desktop.
Save Erreon/1709018 to your computer and use it in GitHub Desktop.
pull info from craigslist for picker app
class Product < ActiveRecord::Base
validates_presence_of :price, :description, :votes
serialize :images
def self.new_from_url(url)
doc = Nokogiri(open(url))
price = doc.search('h2').to_s[/\$(\d+)/].gsub('$',"")
title = doc.title
description = doc.search('#userbody').to_s.split('<!--')[0].gsub(%r{</?[^>]+?>},'').gsub(/\n/," ").strip
if doc.to_s.match('maps.google.com')
map_url = doc.search('small a')[0]['href'].match('maps.google.com').string
end
images = doc.search('table img').map {|i| i['src']}
new({ :price => price , :title => title, :description => description, :map_url => map_url, :images => images })
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment