Created
March 12, 2013 09:54
-
-
Save peterhellberg/5141654 to your computer and use it in GitHub Desktop.
A simple little scraper for the YC List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'open-uri' | |
| require 'nokogiri' | |
| module YCList | |
| class << self | |
| def url | |
| 'http://yclist.com/' | |
| end | |
| def scrape | |
| doc = Nokogiri::HTML(open(url)) | |
| @companies = doc.css('#companies table tbody tr').map do |row| | |
| Company.new(*row.children.map(&:text)[1..-1]) | |
| end | |
| end | |
| def companies | |
| @companies || scrape && @companies | |
| end | |
| def all(filter = //, field = :name) | |
| companies.select { |c| c.send(field).match filter } | |
| end | |
| def one(*args) | |
| all(*args).sample | |
| end | |
| def alive | |
| status '' | |
| end | |
| def dead | |
| status 'Dead' | |
| end | |
| def exited | |
| status 'Exited' | |
| end | |
| def method_missing(m, *args, &block) | |
| fields = %i(name url yc_class status description year) | |
| all(args.first, m) if fields.include?(m) | |
| end | |
| end | |
| class Company | |
| attr_reader :name, :url, :yc_class, :status, :description, :year | |
| def initialize(name, url, yc_class, status, description) | |
| @name = name | |
| @url = url | |
| @yc_class = yc_class | |
| @status = status | |
| @description = description | |
| @year = yc_class.gsub(/^W|S/, "20").to_i | |
| end | |
| def to_s | |
| name | |
| end | |
| alias :inspect :to_s | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage