Sha256: 78a785beca37e9a918028b9fdd30d1b62ef9da3c9dfad1dede69ad2f73915ee4

Contents?: true

Size: 1.58 KB

Versions: 5

Compression:

Stored size: 1.58 KB

Contents

module Geokit
  module Geocoders
    class GeocodioGeocoder < Geocoder
      config :key

      private

      def self.do_geocode(address, _=nil)
        process :json, submit_url(address)
      end

      def self.submit_url(address)
        params = [
          "q=#{Geokit::Inflector.url_escape(address)}",
          "api_key=#{key}",
        ].join('&')

        ['http://api.geocod.io/v1/geocode', params].join('?')
      end

      def self.parse_json(json)
        loc = nil

        json['results'].each do |address|
          if loc.nil?
            loc = create_new_loc(address)
          else
            loc.all.push(create_new_loc(address))
          end
        end
        loc.success = true
        loc
      end

      def self.create_new_loc(json)
        loc = new_loc
        set_address_components(json, loc)
        set_coordinates(json, loc)
        loc
      end

      def self.set_address_components(json, loc)
        loc.street_address  = json['address_components']['street']
        loc.street_number   = json['address_components']['number']
        loc.sub_premise     = json['address_components']['suffix']
        loc.street_name     = json['address_components']['street']
        loc.city            = json['address_components']['city']
        loc.state           = json['address_components']['state']
        loc.zip             = json['address_components']['zip']
        loc.full_address    = json['formatted_address']
      end

      def self.set_coordinates(json, loc)
        loc.lat = json['location']['lat']
        loc.lng = json['location']['lng']
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
geokit-1.14.0 lib/geokit/geocoders/geocodio.rb
geokit-1.13.1 lib/geokit/geocoders/geocodio.rb
geokit-1.13.0 lib/geokit/geocoders/geocodio.rb
geokit-1.12.0 lib/geokit/geocoders/geocodio.rb
geokit-1.11.0 lib/geokit/geocoders/geocodio.rb