class MassShootings::Tracker::Page
Attributes
page_id[R]
wiki[R]
Public Class Methods
new(page_id, data)
click to toggle source
# File lib/mass_shootings/tracker/page.rb, line 14 def initialize(page_id, data) @page_id = page_id @wiki = Nokogiri::HTML.parse(data).css '.wiki' end
Private Instance Methods
format()
click to toggle source
# File lib/mass_shootings/tracker/page.rb, line 21 def format @format ||= /\A\s*Number\s*(?<number>.+?):\s* (?<date>.+?),\s* (?<alleged_shooters>.+),\s* (?<casualties>.*?\d.*?),\s* (?<location>.+)\s*\Z/x end
indexed()
click to toggle source
# File lib/mass_shootings/tracker/page.rb, line 29 def indexed @indexed ||= Hash[shootings.map { |shooting| [shooting.id, shooting] }] end
parse(elements)
click to toggle source
# File lib/mass_shootings/tracker/page.rb, line 33 def parse(elements) h2, as = elements[0], elements[1..-1] format.match h2.content do |match| alleged_shooters = parse_alleged_shooters match['alleged_shooters'] attributes = { id: "#{page_id}-#{match['number']}", casualties: parse_casualties(match['casualties']), date: parse_date(match['date']), location: match['location'], references: as.map { |a| URI(a[:href]) } } if alleged_shooters.any? attributes[:alleged_shooters] = alleged_shooters end Shooting.new attributes end end
parse_alleged_shooters(alleged_shooters)
click to toggle source
# File lib/mass_shootings/tracker/page.rb, line 54 def parse_alleged_shooters(alleged_shooters) alleged_shooters. split(/\s*(?:,?\s+(?:and|&)\s+|[,;](?!\s*Jr\.))\s*/). reject do |shooter| shooter =~ /identified|identity|unnamed|unkn?own|unreported/i end end
parse_casualties(casualties)
click to toggle source
# File lib/mass_shootings/tracker/page.rb, line 62 def parse_casualties(casualties) Hash[ casualties. scan(/(\d+)\s+(.+?)\b/). map { |count, type| [type.to_sym, count.to_i] } ] end
parse_date(date)
click to toggle source
# File lib/mass_shootings/tracker/page.rb, line 70 def parse_date(date) result = Date.strptime date, '%m/%d/%Y' result.year < 100 ? Date.strptime(date, '%m/%d/%y') : result rescue end
shootings()
click to toggle source
# File lib/mass_shootings/tracker/page.rb, line 77 def shootings @shootings ||= wiki. xpath('child::h2 | child::h2/following-sibling::p/a'). slice_before { |element| element.node_name == 'h2' }. map(&method(:parse)). tap