require 'csv' require 'redis/connection/hiredis' require 'redis' require 'logger' require 'open-uri' class Earthquakes DATA_URL = 'http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt' attr_accessor :id_set, :region_set, :magnitude_set def initialize @redis = Redis.new @id_set = 'earthquake-ids' @region_set = 'earthquake-regions' @magnitude_set = 'earthquake-magnitudes' @logger = Logger.new(STDOUT) @logger.formatter = proc do |severity, datetime, progname, msg| "#{datetime}: #{msg}\n" end end def get_info(earthquake_id) @redis.hgetall(with_prefix(earthquake_id, 'eqid')) end def in_region(region) @redis.smembers(with_prefix(region, 'eqregion')) end def with_magnitude(magnitude) @redis.smembers(with_prefix(magnitude, 'eqmagnitude')) end def all_ids @redis.smembers(@id_set).map { |i| sans_prefix(i, 'eqid') } end def all_regions @redis.smembers(@region_set).map { |r| sans_prefix(r, 'eqregion') } end def all_magnitudes @redis.smembers(@magnitude_set).map { |m| sans_prefix(m, 'eqmagnitude') } end def drop_all @logger.info('Removing data for each earthquake') all_ids.each do |i| @logger.info("Removing data for earthquake #{sans_prefix(i, 'eqid')}") @redis.del(i) end @logger.info('Removing earthquake id set') @redis.del(@id_set) @logger.info('Removing earthquake region set') @redis.del(@region_set) @logger.info('Removing earthquake magnitude set') @redis.del(@magnitude_set) end def load_data data = CSV.parse( open(DATA_URL).read, :headers => true, :header_converters => [:symbol] ) data.each do |row| @redis.multi do |r| @logger.info("Adding earthquake #{row[:eqid]}") id_key = with_prefix(row[:eqid].downcase, 'eqid') r.sadd(@id_set, id_key) row.headers.each do |header| r.hset(id_key, header.downcase, row[header].downcase) end region_key = with_prefix(row[:region].downcase, 'eqregion') r.sadd(region_key, id_key) r.sadd(@region_set, region_key) magnitude_key = with_prefix(row[:magnitude], 'eqmagnitude') r.sadd(magnitude_key, id_key) r.sadd(@magnitude_set, magnitude_key) end end end private def with_prefix(key, prefix) if not /^#{prefix}:/.match(key) return "#{prefix}:#{key}" end key end def sans_prefix(key, prefix) key.gsub(/^#{prefix}:/, '') end end if $0 == __FILE__ Earthquakes.new.send(ARGV.first) end