From 293006314b446663e7a9acbf4b1ec07248586357 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 29 Feb 2012 01:00:28 -0500 Subject: [PATCH] Renaming script, using JSON for data presentation and handling zero-arg run --- redis-with-ruby/{eq-info => eq-info.rb} | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) rename redis-with-ruby/{eq-info => eq-info.rb} (77%) diff --git a/redis-with-ruby/eq-info b/redis-with-ruby/eq-info.rb similarity index 77% rename from redis-with-ruby/eq-info rename to redis-with-ruby/eq-info.rb index 38acfe5..c1795bf 100755 --- a/redis-with-ruby/eq-info +++ b/redis-with-ruby/eq-info.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require 'pp' +require 'json' require 'slop' require File.expand_path('../earthquakes', __FILE__) @@ -17,6 +17,11 @@ def main 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? @@ -35,34 +40,32 @@ def main end if opts[:region] - puts "Earthquakes in region #{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 + puts JSON.pretty_generate(results) return 0 end if opts[:magnitude] - puts "Earthquakes with magnitude #{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 + puts JSON.pretty_generate(results) 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 + puts "/* Earthquake with id of #{opts[:eqid]} */" + puts JSON.pretty_generate(earthquakes.get_info(opts[:eqid])) return 0 end