module MassShootings::Tracker
Retrieves mass shootings from the [r/GunsAreCool shooting tracker] (www.reddit.com/r/GunsAreCool/wiki/2015massshootings).
Public Class Methods
get(id)
click to toggle source
Retrieves a ‘Shooting` by unique ID. @param [String] id the `Shooting`’s unique identifier. This is opaque;
you should not depend on its format.
# File lib/mass_shootings/tracker.rb, line 18 def get(id) year, number = id.split '-', 2 page(year)[id] end
in_date_range(date_range)
click to toggle source
Retrieves all ‘Shooting`s that occurred within a date range. @param [Range<Date>] date_range the date range to search. Inclusive and
exclusive ranges are both supported.
# File lib/mass_shootings/tracker.rb, line 28 def in_date_range(date_range) pages_in_date_range(date_range). map(&method(:page)). flat_map(&:to_a). select { |shooting| date_range.cover? shooting.date } end
reset()
click to toggle source
Invalidates the in-memory cache. In a long-running process, you should arrange for this method to be called approximately every 24 hours and 36 minutes.
# File lib/mass_shootings/tracker.rb, line 40 def reset @pages = {} end
Private Class Methods
page(year)
click to toggle source
# File lib/mass_shootings/tracker.rb, line 46 def page(year) pages[year] ||= Page.new year, Net::HTTP.get(uri(year)) end
pages()
click to toggle source
# File lib/mass_shootings/tracker.rb, line 50 def pages @pages || reset end
pages_in_date_range(date_range)
click to toggle source
# File lib/mass_shootings/tracker.rb, line 54 def pages_in_date_range(date_range) result = [] year = date_range.begin.year while date_range.overlaps? Date.new(year, 1, 1)...Date.new(year + 1, 1, 1) result << year.to_s year += 1 end result end
uri(year)
click to toggle source
# File lib/mass_shootings/tracker.rb, line 64 def uri(year) URI "https://www.reddit.com/r/GunsAreCool/wiki/#{year}massshootings" end