Skip to content
Snippets Groups Projects
Commit 2a80bc91 authored by Alex Reisner's avatar Alex Reisner
Browse files

Add -j/--json option.

parent 42373e14
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,8 @@ module Geocoder
class Cli
def self.run(args, out = STDOUT)
url_only = false
show_url = false
show_json = false
OptionParser.new{ |opts|
opts.banner = "Usage:\n geocode [options] location"
......@@ -32,8 +33,12 @@ module Geocoder
Geocoder::Configuration.timeout = timeout.to_i
end
opts.on("-j", "--json", "Print API's raw JSON response") do
show_json = true
end
opts.on("-u", "--url", "Print URL for API instead of result") do
url_only = true
show_url = true
end
opts.on_tail("-v", "--version", "Print version number") do
......@@ -54,18 +59,28 @@ module Geocoder
exit 1
end
if url_only
if show_url and show_json
out << "You can only specify one of -j and -u.\n"
exit 2
end
if show_url
out << Geocoder.send(:lookup).send(:query_url, query) + "\n"
exit 0
end
if show_json
out << Geocoder.send(:lookup).send(:fetch_raw_data, query) + "\n"
exit 0
end
if (result = Geocoder.search(query).first)
out << result.coordinates.join(',') + "\n"
out << result.address + "\n"
exit 0
else
out << "Location '#{query}' not found.\n"
exit 2
exit 1
end
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment