From 2a80bc912c0dc09c83d364a7d0fba3650b9c9b71 Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Fri, 22 Apr 2011 11:50:56 -0400
Subject: [PATCH] Add -j/--json option.

---
 lib/geocoder/cli.rb | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/lib/geocoder/cli.rb b/lib/geocoder/cli.rb
index e31f10a9..88e12e58 100644
--- a/lib/geocoder/cli.rb
+++ b/lib/geocoder/cli.rb
@@ -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
-- 
GitLab