Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Last active December 14, 2015 17:08
Show Gist options
  • Select an option

  • Save peterhellberg/5119679 to your computer and use it in GitHub Desktop.

Select an option

Save peterhellberg/5119679 to your computer and use it in GitHub Desktop.
A small scraper for the list of attendees on http://barcampstockholm.com/
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
@peterhellberg
Copy link
Author

Usage

# All of the attendees
BarCampStockholm.attendees

# All attendees with a last name that ends in "berg"
BarCampStockholm.all /berg$/

# All of the companies
BarCampStockholm.companies

# Companies grouped by number of attendees
BarCampStockholm.companies.values.map.with_object({}) do |c,o|
  o[c.size] ||= []
  o[c.size] << c
end

# The company name for one of the Martins
BarCampStockholm.one('Martin').company.name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment