Last active
December 14, 2015 17:08
-
-
Save peterhellberg/5119679 to your computer and use it in GitHub Desktop.
A small scraper for the list of attendees on http://barcampstockholm.com/
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 BarCampStockholm | |
| class << self | |
| def url | |
| 'http://barcampstockholm.com/' | |
| end | |
| def scrape | |
| doc = Nokogiri::HTML(open(url)) | |
| @companies = { nil => Company.new('Okänt') } | |
| @attendees = doc.css('.attendees li').map do |li| | |
| name, company = li.text.strip.split(', ') | |
| Attendee.new name, @companies[company] ||= Company.new(company) | |
| end | |
| end | |
| def attendees | |
| @attendees || scrape | |
| end | |
| def companies | |
| @companies || scrape && @companies | |
| end | |
| def all(filter = //) | |
| attendees.select { |a| a.name.match filter } | |
| end | |
| def one(filter = //) | |
| all(filter).sample | |
| end | |
| end | |
| class Attendee | |
| attr_accessor :name, :company | |
| def initialize(name, company) | |
| @name = name | |
| @company = company | |
| company << self | |
| end | |
| def inspect | |
| "#{name}, #{company}" | |
| end | |
| end | |
| class Company | |
| attr_accessor :name, :attendees | |
| def initialize(name) | |
| @name = name | |
| @attendees = [] | |
| end | |
| def <<(attendee) | |
| @attendees << attendee | |
| end | |
| def size | |
| attendees.size | |
| end | |
| def to_s | |
| name | |
| end | |
| def inspect | |
| to_s | |
| end | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage