Re-namespacing the redis+ruby stuff ... because it was bothering me (I guess?)

This commit is contained in:
Dan Buch
2012-03-14 21:39:28 -04:00
parent 4d88027be7
commit 47d9b54b10
7 changed files with 0 additions and 0 deletions

1
redis/with-ruby/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.rdb

7
redis/with-ruby/Gemfile Normal file
View File

@@ -0,0 +1,7 @@
source :rubygems
gem 'fastercsv'
gem 'hiredis'
gem 'rake'
gem 'redis'
gem 'slop'

View File

@@ -0,0 +1,18 @@
GEM
remote: http://rubygems.org/
specs:
fastercsv (1.5.4)
hiredis (0.4.4)
rake (0.9.2.2)
redis (2.2.2)
slop (3.0.4)
PLATFORMS
ruby
DEPENDENCIES
fastercsv
hiredis
rake
redis
slop

21
redis/with-ruby/Rakefile Normal file
View File

@@ -0,0 +1,21 @@
desc 'drop all of the earthquake data'
task :drop do
get_earthquakes.drop_all
end
desc 'load all of the earthquake data'
task :load do
get_earthquakes.load_data
end
desc 'drop, then load all of the earthquake data'
task :reload => [:drop, :load]
task :default => :load
def get_earthquakes
require File.expand_path('../earthquakes', __FILE__)
Earthquakes.new
end

View File

@@ -0,0 +1,139 @@
require 'csv'
require 'redis/connection/hiredis'
require 'redis'
require 'logger'
require 'open-uri'
class Earthquakes
attr_accessor :id_set, :region_set, :magnitude_set
attr_accessor :magnitude_scores_set, :data_url
def initialize
@redis = Redis.new
@id_set = 'earthquake-ids'
@region_set = 'earthquake-regions'
@magnitude_set = 'earthquake-magnitudes'
@magnitude_scores_set = 'earthquakes-magnitudes-scores'
@data_url =
'http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt'
@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_in_range(min_magnitude, max_magnitude)
min_mag, max_mag =
magnitude_as_int(min_magnitude), magnitude_as_int(max_magnitude)
@logger.debug(
"Fetching magnitudes with scores in range #{min_mag}-#{max_mag}")
@redis.zrange(@magnitude_scores_set, min_mag, max_mag)
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)
@logger.info('Removing earthquake magnitude scores set')
@redis.del(@magnitude_scores_set)
end
def load_data
data = CSV.parse(
open(@data_url).read, :headers => true, :header_converters => [:symbol]
)
data.each do |row|
@logger.info("Adding earthquake #{row[:eqid]}")
load_one_row(row)
end
end
private
def load_one_row(row)
@redis.multi do |redis_multi|
id_key = with_prefix(row[:eqid].downcase, 'eqid')
load_id_and_attrs_for_row(row, id_key, redis_multi)
load_region_for_row(row, id_key, redis_multi)
load_magnitude_for_row(row, id_key, redis_multi)
end
end
def load_id_and_attrs_for_row(row, id_key, redis_multi)
redis_multi.sadd(@id_set, id_key)
row.headers.each do |header|
redis_multi.hset(id_key, header.downcase, row[header].downcase)
end
end
def load_region_for_row(row, id_key, redis_multi)
region_key = with_prefix(row[:region].downcase, 'eqregion')
redis_multi.sadd(region_key, id_key)
redis_multi.sadd(@region_set, region_key)
end
def load_magnitude_for_row(row, id_key, redis_multi)
magnitude_key = with_prefix(row[:magnitude], 'eqmagnitude')
redis_multi.sadd(magnitude_key, id_key)
redis_multi.sadd(@magnitude_set, magnitude_key)
score = magnitude_as_int(row[:magnitude])
@logger.info("Setting score of #{score} for row #{row.inspect}")
redis_multi.zadd(@magnitude_scores_set, score, id_key)
end
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
def magnitude_as_int(magnitude)
magnitude.to_s.gsub(/\./, '').to_i
end
end
if $0 == __FILE__
Earthquakes.new.send(ARGV.first)
end

80
redis/with-ruby/eq-info.rb Executable file
View File

@@ -0,0 +1,80 @@
#!/usr/bin/env ruby
require 'json'
require 'slop'
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_regions, 'List available earthquake regions.'
on :M, :listall_magnitudes, 'List available earthquakes magnitudes.'
on :r, :region=, 'Show earthquakes for region.'
on :m, :magnitude_range=, 'Show earthquakes of magnitude in range, e.g. "1.1-2.9".'
on :i, :eqid=, 'Show all available info for earthquake with id.'
end
if ARGV.length < 1
puts opts
return 2
end
earthquakes = Earthquakes.new
if opts.listall?
earthquakes.all_ids.each { |i| puts i }
return 0
end
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 JSON.pretty_generate(results)
return 0
end
if opts[:magnitude_range]
puts "/* Earthquakes with magnitude #{opts[:magnitude]} */"
min_mag, max_mag = opts[:magnitude_range].split('-')
results = []
earthquakes.with_magnitude_in_range(min_mag, max_mag).each do |i|
results << earthquakes.get_info(i)
end
puts JSON.pretty_generate(results)
return 0
end
if opts[:eqid]
puts "/* Earthquake with id of #{opts[:eqid]} */"
puts JSON.pretty_generate(earthquakes.get_info(opts[:eqid]))
return 0
end
return 1
end
if $0 == __FILE__
exit(main)
end

View File

@@ -0,0 +1,38 @@
require 'redis/connection/hiredis'
require 'redis'
require 'pp'
require 'yaml'
class Fancy
attr_accessor :has_pants, :likes_to_dance
def initialize(has_pants)
@has_pants = has_pants
@likes_to_dance = true
end
end
def main
redis = Redis.new
inst = Fancy.new('yup!')
['foo', 'derp', 'hamsters'].each { |k| redis.del(k) }
puts redis.set('foo', 'bar')
puts redis.set('derp', inst.to_yaml)
puts redis.sadd('hamsters', 'albert')
puts redis.sadd('hamsters', 'walter')
puts redis.get('foo')
pp YAML.load_documents(redis.get('derp'))[0]
puts redis.smembers('hamsters')
end
if $0 == __FILE__
main
end