Permalink
Browse files

Add command line interface.

  • Loading branch information...
1 parent e14e9b7 commit 1bd5a78a2528dc8ac70964b20e584a2f15dc19f2 @alexreisner committed Apr 22, 2011
Showing with 78 additions and 0 deletions.
  1. +5 −0 bin/geocode
  2. +1 −0 geocoder.gemspec
  3. +72 −0 lib/geocoder/cli.rb
View
@@ -0,0 +1,5 @@
+#!/usr/bin/env ruby
+
+require 'geocoder/cli'
+
+Geocoder::Cli.run ARGV
View
@@ -12,4 +12,5 @@ Gem::Specification.new do |s|
s.description = "Provides object geocoding (by street or IP address), reverse geocoding (coordinates to street address), and distance queries for ActiveRecord and Mongoid. Designed for Rails but works with other Rack frameworks too."
s.files = `git ls-files`.split("\n") - %w[geocoder.gemspec Gemfile init.rb]
s.require_paths = ["lib"]
+ s.executables = ["geocode"]
end
View
@@ -0,0 +1,72 @@
+require 'geocoder'
+require 'optparse'
+
+module Geocoder
+ class Cli
+
+ def self.run(args, out = STDOUT)
+ url_only = false
+
+ OptionParser.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)") do |key|
+ Geocoder::Configuration.api_key = key
+ end
+
+ opts.on("-l <language>", "--language <language>",
+ "Language of output (see API docs for valid choices)") do |language|
+ Geocoder::Configuration.language = language
+ end
+
+ lookups = Geocoder.valid_lookups - [:freegeoip]
+ opts.on("-s <service>", lookups, "--service <service>",
+ "Geocoding service: #{lookups.join(', ')}") do |service|
+ Geocoder::Configuration.lookup = service.to_sym
+ end
+
+ opts.on("-t <seconds>", lookups, "--timeout <seconds>",
+ "Maximum number of seconds to wait for API response") do |timeout|
+ Geocoder::Configuration.timeout = timeout.to_i
+ end
+
+ opts.on("-u", "--url", "Print URL for API instead of result") do
+ url_only = true
+ end
+
+ opts.on_tail("-v", "--version", "Print version number") do
+ puts "Geocoder #{Geocoder.version}"
+ exit
+ end
+
+ opts.on_tail("-h", "--help", "Print this help") do
+ puts opts
+ exit
+ end
+ }.parse!(args)
+
+ query = args.join(" ")
+
+ if query == ""
+ out << "Please specify a location to search for.\n"
+ exit 1
+ end
+
+ if url_only
+ out << Geocoder.send(:lookup).send(:query_url, 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
+ end
+ end
+ end
+end

0 comments on commit 1bd5a78

Please sign in to comment.