76 lines
1.6 KiB
Ruby
Executable File
76 lines
1.6 KiB
Ruby
Executable File
#!/usr/bin/env ruby
|
|
|
|
require 'pp'
|
|
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=, 'Show earthquakes of magnitude.'
|
|
on :i, :eqid=, 'Show all available info for earthquake with id.'
|
|
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 results.to_yaml
|
|
return 0
|
|
end
|
|
|
|
if 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
|
|
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
|
|
return 0
|
|
end
|
|
|
|
return 1
|
|
end
|
|
|
|
|
|
if $0 == __FILE__
|
|
exit(main)
|
|
end
|