2012-02-29 04:35:36 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2012-02-29 06:00:28 +00:00
|
|
|
require 'json'
|
2012-02-29 04:35:36 +00:00
|
|
|
require 'slop'
|
|
|
|
|
|
|
|
require File.expand_path('../earthquakes', __FILE__)
|
|
|
|
|
|
|
|
|
|
|
|
def main
|
|
|
|
opts = Slop.parse(:help => true, :strict => true) do
|
|
|
|
banner "#{File.basename(__FILE__)} [options]"
|
2012-02-29 04:35:55 +00:00
|
|
|
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.'
|
2012-02-29 04:35:36 +00:00
|
|
|
on :i, :eqid=, 'Show all available info for earthquake with id.'
|
|
|
|
end
|
|
|
|
|
2012-02-29 06:00:28 +00:00
|
|
|
if ARGV.length < 1
|
|
|
|
puts opts
|
|
|
|
return 2
|
|
|
|
end
|
|
|
|
|
2012-02-29 04:35:36 +00:00
|
|
|
earthquakes = Earthquakes.new
|
|
|
|
|
|
|
|
if opts.listall?
|
|
|
|
earthquakes.all_ids.each { |i| puts i }
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2012-02-29 04:35:55 +00:00
|
|
|
if opts.listall_regions?
|
|
|
|
earthquakes.all_regions.sort.each { |r| puts r }
|
2012-02-29 04:35:36 +00:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2012-02-29 04:35:55 +00:00
|
|
|
if opts.listall_magnitudes?
|
|
|
|
earthquakes.all_magnitudes.sort.each { |m| puts m }
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts[:region]
|
2012-02-29 06:00:28 +00:00
|
|
|
puts "/* Earthquakes in region #{opts[:region]} */"
|
2012-02-29 04:35:55 +00:00
|
|
|
|
|
|
|
results = []
|
|
|
|
earthquakes.in_region(opts[:region]).each do |i|
|
|
|
|
results << earthquakes.get_info(i)
|
|
|
|
end
|
|
|
|
|
2012-02-29 06:00:28 +00:00
|
|
|
puts JSON.pretty_generate(results)
|
2012-02-29 04:35:55 +00:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts[:magnitude]
|
2012-02-29 06:00:28 +00:00
|
|
|
puts "/* Earthquakes with magnitude #{opts[:magnitude]} */"
|
2012-02-29 04:35:55 +00:00
|
|
|
|
|
|
|
results = []
|
|
|
|
earthquakes.with_magnitude(opts[:magnitude]).each do |i|
|
|
|
|
results << earthquakes.get_info(i)
|
|
|
|
end
|
|
|
|
|
2012-02-29 06:00:28 +00:00
|
|
|
puts JSON.pretty_generate(results)
|
2012-02-29 04:35:55 +00:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts[:eqid]
|
2012-02-29 06:00:28 +00:00
|
|
|
puts "/* Earthquake with id of #{opts[:eqid]} */"
|
|
|
|
puts JSON.pretty_generate(earthquakes.get_info(opts[:eqid]))
|
2012-02-29 04:35:55 +00:00
|
|
|
return 0
|
2012-02-29 04:35:36 +00:00
|
|
|
end
|
|
|
|
|
2012-02-29 04:35:55 +00:00
|
|
|
return 1
|
2012-02-29 04:35:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
if $0 == __FILE__
|
|
|
|
exit(main)
|
|
|
|
end
|