Renaming script, using JSON for data presentation and handling zero-arg run

This commit is contained in:
Dan Buch 2012-02-29 01:00:28 -05:00
parent 3ebd72f6ad
commit 293006314b

View File

@ -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