Allowing for query by region and magnitude, too
This commit is contained in:
parent
e84f11dfba
commit
4c2a9ce0b2
@ -9,12 +9,13 @@ require 'open-uri'
|
||||
class Earthquakes
|
||||
DATA_URL = 'http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt'
|
||||
|
||||
attr_accessor :id_set, :region_set
|
||||
attr_accessor :id_set, :region_set, :magnitude_set
|
||||
|
||||
def initialize
|
||||
@redis = Redis.new
|
||||
@id_set = 'earthquakes'
|
||||
@id_set = 'earthquake-ids'
|
||||
@region_set = 'earthquake-regions'
|
||||
@magnitude_set = 'earthquake-magnitudes'
|
||||
|
||||
@logger = Logger.new(STDOUT)
|
||||
@logger.formatter = proc do |severity, datetime, progname, msg|
|
||||
@ -23,25 +24,33 @@ class Earthquakes
|
||||
end
|
||||
|
||||
def get_info(earthquake_id)
|
||||
if not /^eqid:/.match(earthquake_id)
|
||||
earthquake_id = "eqid:#{earthquake_id}"
|
||||
@redis.hgetall(with_prefix(earthquake_id, 'eqid'))
|
||||
end
|
||||
|
||||
@redis.hgetall(earthquake_id)
|
||||
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| i.gsub(/^eqid:/, '') }
|
||||
@redis.smembers(@id_set).map { |i| sans_prefix(i, 'eqid') }
|
||||
end
|
||||
|
||||
def all_regions
|
||||
@redis.smembers(@region_set).map { |r| r.gsub(/^eqregion:/, '') }
|
||||
@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 #{i.gsub(/^eqid:/, '')}")
|
||||
@logger.info("Removing data for earthquake #{sans_prefix(i, 'eqid')}")
|
||||
@redis.del(i)
|
||||
end
|
||||
|
||||
@ -50,6 +59,9 @@ class Earthquakes
|
||||
|
||||
@logger.info('Removing earthquake region set')
|
||||
@redis.del(@region_set)
|
||||
|
||||
@logger.info('Removing earthquake magnitude set')
|
||||
@redis.del(@magnitude_set)
|
||||
end
|
||||
|
||||
def load_data
|
||||
@ -60,19 +72,35 @@ class Earthquakes
|
||||
data.each do |row|
|
||||
@redis.multi do |r|
|
||||
@logger.info("Adding earthquake #{row[:eqid]}")
|
||||
key = "eqid:#{row[:eqid].downcase}"
|
||||
r.sadd(@id_set, key)
|
||||
id_key = with_prefix(row[:eqid].downcase, 'eqid')
|
||||
r.sadd(@id_set, id_key)
|
||||
row.headers.each do |header|
|
||||
r.hset(key, header.downcase, row[header])
|
||||
r.hset(id_key, header.downcase, row[header].downcase)
|
||||
end
|
||||
|
||||
region_key = "eqregion:#{row[:region].downcase}"
|
||||
|
||||
r.sadd(region_key, key)
|
||||
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
|
||||
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
require 'pp'
|
||||
require 'slop'
|
||||
|
||||
require File.expand_path('../earthquakes', __FILE__)
|
||||
@ -8,12 +9,11 @@ require File.expand_path('../earthquakes', __FILE__)
|
||||
def main
|
||||
opts = Slop.parse(:help => true, :strict => true) do
|
||||
banner "#{File.basename(__FILE__)} [options]"
|
||||
on :l, :listall, 'List all available earthquake ids.'
|
||||
on :r, :listall_by_region, 'List available earthquakes by region'
|
||||
#on :l, :'list-by-region', 'List available earthquakes by region'
|
||||
#on :L, :'list-by-magnitude', 'List available earthquakes by magnitude'
|
||||
#on :r, :region=, 'Show earthquakes for region'
|
||||
#on :m, :magnitude=, 'Show earthquakes of magnitude'
|
||||
on :L, :listall, 'List all available earthquake ids.'
|
||||
on :R, :listall_regions, 'List available earthquake regions.'
|
||||
on :M, :listall_magnitudes, 'List available earthquakes magnitudes.'
|
||||
on :r, :region=, 'Show earthquakes for region.'
|
||||
on :m, :magnitude=, 'Show earthquakes of magnitude.'
|
||||
on :i, :eqid=, 'Show all available info for earthquake with id.'
|
||||
end
|
||||
|
||||
@ -24,19 +24,51 @@ def main
|
||||
return 0
|
||||
end
|
||||
|
||||
if opts.listall_by_region?
|
||||
earthquakes.all_regions.each { |r| puts r }
|
||||
if opts.listall_regions?
|
||||
earthquakes.all_regions.sort.each { |r| puts r }
|
||||
return 0
|
||||
end
|
||||
|
||||
if opts.listall_magnitudes?
|
||||
earthquakes.all_magnitudes.sort.each { |m| puts m }
|
||||
return 0
|
||||
end
|
||||
|
||||
if opts[:region]
|
||||
puts "Earthquakes in region #{opts[:region]}"
|
||||
|
||||
results = []
|
||||
earthquakes.in_region(opts[:region]).each do |i|
|
||||
results << earthquakes.get_info(i)
|
||||
end
|
||||
|
||||
puts results.to_yaml
|
||||
return 0
|
||||
end
|
||||
|
||||
if opts[:magnitude]
|
||||
puts "Earthquakes with magnitude #{opts[:magnitude]}"
|
||||
|
||||
results = []
|
||||
earthquakes.with_magnitude(opts[:magnitude]).each do |i|
|
||||
results << earthquakes.get_info(i)
|
||||
end
|
||||
|
||||
puts results.to_yaml
|
||||
return 0
|
||||
end
|
||||
|
||||
if opts[:eqid]
|
||||
puts "Earthquake with id of #{opts[:eqid]}"
|
||||
earthquakes.get_info(opts[:eqid]).each do |key, value|
|
||||
puts " #{key}: #{value}"
|
||||
end
|
||||
|
||||
return 0
|
||||
end
|
||||
|
||||
return 1
|
||||
end
|
||||
|
||||
|
||||
if $0 == __FILE__
|
||||
exit(main)
|
||||
|
Loading…
Reference in New Issue
Block a user