Sha256: 0fb1f2f3b2b7dc7c843c36ad3f9e6dfe27d44736028c897e4f6552146033ddf8

Contents?: true

Size: 1.99 KB

Versions: 6

Compression:

Stored size: 1.99 KB

Contents

# encoding: UTF-8
require 'graticule'
require 'optparse'

module Graticule

  # A command line interface for geocoding.  From the command line, run:
  #
  #   geocode 49423
  #
  # Outputs:
  #
  #   # Holland, MI 49423 US
  #   # latitude: 42.7654, longitude: -86.1085
  #
  # == Usage: geocode [options] location
  #
  # Options:
  #     -s, --service service            Geocoding service
  #     -a, --apikey apikey              API key for the selected service
  #     -h, --help                       Help
  class Cli

    def self.start(args, out = STDOUT)
      options = { :service => :yahoo, :api_key => 'YahooDemo' }
      supported_services = %w(yahoo google yandex geocoder_us metacarta)

      OptionParser.new do |opts|
        opts.banner = "Usage: geocode [options] location"
        opts.separator ""
        opts.separator "Options: "

        opts.on("-s service", supported_services, "--service service",
                "Geocoding service.", "Currently supported services: #{supported_services.join(", ")}") do |service|
          options[:service] = service
        end

        opts.on("-a apikey", "--apikey apikey", "API key for the selected service") do |apikey|
          options[:api_key] = apikey
        end

        opts.on_tail("-h", "--help", "Help") do
          puts opts
          exit
        end
      end.parse! args

      options[:location] = args.join(" ")

      result = Graticule.service(options[:service]).new(*options[:api_key].split(',')).locate(options[:location])
      location = (result.is_a?(Array) ? result.first : result)
      if location
        out << location.to_s(:coordinates => true) + "\n"
        exit 0
      else
        out << "Location not found"
        exit 1
      end
    rescue Graticule::CredentialsError
      $stderr.puts "Invalid API key. Pass your #{options[:service]} API key using the -a option. "
    rescue OptionParser::InvalidArgument, OptionParser::InvalidOption,
        Graticule::Error => error
      $stderr.puts error.message
    end


  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
graticule-2.7.2 lib/graticule/cli.rb
graticule-2.7.1 lib/graticule/cli.rb
graticule-2.7.0 lib/graticule/cli.rb
graticule-2.6.0 lib/graticule/cli.rb
graticule-2.5.0 lib/graticule/cli.rb
graticule-2.4.0 lib/graticule/cli.rb