Doing slightly more interesting stuff with some real data

This commit is contained in:
Dan Buch 2012-02-28 21:39:56 -05:00
parent 5a9fd820d4
commit 1747424598
3 changed files with 57 additions and 0 deletions

View File

@ -1,4 +1,5 @@
source :rubygems source :rubygems
gem 'fastercsv'
gem 'hiredis' gem 'hiredis'
gem 'redis' gem 'redis'

View File

@ -1,6 +1,7 @@
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
specs: specs:
fastercsv (1.5.4)
hiredis (0.4.4) hiredis (0.4.4)
redis (2.2.2) redis (2.2.2)
@ -8,5 +9,6 @@ PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
fastercsv
hiredis hiredis
redis redis

View File

@ -0,0 +1,54 @@
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'
def initialize
@redis = Redis.new
@logger = Logger.new(STDOUT)
@logger.formatter = proc do |severity, datetime, progname, msg|
"#{datetime}: #{msg}\n"
end
end
def load_data
open(DATA_URL) do |f|
CSV.parse(f.read, :headers => true) do |row|
@redis.multi do |r|
@logger.info("Adding earthquake #{row['Eqid']}")
r.sadd('earthquakes', row['Eqid'])
row.headers.each do |header|
r.hset("eqid:#{row['Eqid']}", header, row[header])
end
end
end
end
end
def dump_data
@redis.smembers('earthquakes').each do |eqid|
@logger.info(
"Earthquake #{eqid} 'Src': #{
@redis.hget("eqid:#{eqid}", 'Src')}"
)
end
end
end
def main
eq = Earthquakes.new
eq.load_data
eq.dump_data
end
if $0 == __FILE__
main
end