require'geocoder'require'optparse'moduleGeocoderclassClidefself.run(args,out=STDOUT)show_url=falseshow_json=falseOptionParser.new{|opts|opts.banner="Usage:\n geocode [options] <location>"opts.separator"\nOptions: "opts.on("-k <key>","--key <key>","Key for geocoding API (optional for most). For Google Premier use 'key client channel' separated by spaces")do|key|premier_key=key.split(' ')ifpremier_key.length==3Geocoder::Configuration.api_key=premier_keyelseGeocoder::Configuration.api_key=keyendendopts.on("-l <language>","--language <language>","Language of output (see API docs for valid choices)")do|language|Geocoder::Configuration.language=languageendopts.on("-p <proxy>","--proxy <proxy>","HTTP proxy server to use (user:pass@host:port)")do|proxy|Geocoder::Configuration.http_proxy=proxyendopts.on("-s <service>",Geocoder.street_lookups,"--service <service>","Geocoding service: #{Geocoder.street_lookups*', '}")do|service|Geocoder::Configuration.lookup=service.to_symendopts.on("-t <seconds>","--timeout <seconds>","Maximum number of seconds to wait for API response")do|timeout|Geocoder::Configuration.timeout=timeout.to_iendopts.on("-j","--json","Print API's raw JSON response")doshow_json=trueendopts.on("-u","--url","Print URL for API query instead of result")doshow_url=trueendopts.on_tail("-v","--version","Print version number")dorequire"geocoder/version"out<<"Geocoder #{Geocoder::VERSION}\n"exitendopts.on_tail("-h","--help","Print this help")doout<<"Look up geographic information about a location.\n\n"out<<optsout<<"\nCreated and maintained by Alex Reisner, available under the MIT License.\n"out<<"Report bugs and contribute at http://github.com/alexreisner/geocoder\n"exitend}.parse!(args)query=args.join(" ")ifquery==""out<<"Please specify a location (run `geocode -h` for more info).\n"exit1endifshow_urlandshow_jsonout<<"You can only specify one of -j and -u.\n"exit2endifshow_urllookup=Geocoder.send(:lookup,query)reverse=lookup.send(:coordinates?,query)out<<lookup.send(:query_url,query,reverse)+"\n"exit0endifshow_jsonlookup=Geocoder.send(:lookup,query)reverse=lookup.send(:coordinates?,query)out<<lookup.send(:fetch_raw_data,query,reverse)+"\n"exit0endif(result=Geocoder.search(query).first)lookup=Geocoder.send(:get_lookup,:google)lines=[["Latitude",result.latitude],["Longitude",result.longitude],["Full address",result.address],["City",result.city],["State/province",result.state],["Postal code",result.postal_code],["Country",result.country],["Google map",lookup.map_link_url(result.coordinates)],]lines.eachdo|line|out<<(line[0]+": ").ljust(18)+line[1].to_s+"\n"endexit0elseout<<"Location '#{query}' not found.\n"exit1endendendend