You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.8 KiB

#!/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