Doing slightly more interesting stuff with some real data
This commit is contained in:
parent
5a9fd820d4
commit
1747424598
@ -1,4 +1,5 @@
|
||||
source :rubygems
|
||||
|
||||
gem 'fastercsv'
|
||||
gem 'hiredis'
|
||||
gem 'redis'
|
||||
|
@ -1,6 +1,7 @@
|
||||
GEM
|
||||
remote: http://rubygems.org/
|
||||
specs:
|
||||
fastercsv (1.5.4)
|
||||
hiredis (0.4.4)
|
||||
redis (2.2.2)
|
||||
|
||||
@ -8,5 +9,6 @@ PLATFORMS
|
||||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
fastercsv
|
||||
hiredis
|
||||
redis
|
||||
|
54
redis-with-ruby/earthquakes.rb
Normal file
54
redis-with-ruby/earthquakes.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user