Sha256: 378091f0f557db99fefc5d1fe2630e922ee7075e65dae1c1a1499a733243af56

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

require 'geocoder/results/base'

module Geocoder
  module Result
    class Geoip2 < Base
      def address(format = :full)
        s = state.to_s == '' ? '' : ", #{state_code}"
        "#{city}#{s} #{postal_code}, #{country}".sub(/^[ ,]*/, '')
      end

      def coordinates
        [latitude, longitude]
      end

      def latitude
        data.fetch('location', {}).fetch('latitude', 0.0)
      end

      def longitude
        data.fetch('location', {}).fetch('longitude', 0.0)
      end

      def city
        data.fetch('city', {}).fetch('names', {}).fetch(locale, '')
      end

      def state
        data.fetch('subdivisions', []).fetch(0, {}).fetch('names', {}).fetch(locale, '')
      end

      def state_code
        data.fetch('subdivisions', []).fetch(0, {}).fetch('iso_code', '')
      end

      def country
        data.fetch('country', {}).fetch('names', {}).fetch(locale, '')
      end

      def country_code
        data.fetch('country', {}).fetch('iso_code', '')
      end

      def postal_code
        data.fetch('postal', {}).fetch('code', '')
      end

      def self.response_attributes
        %w[ip]
      end

      response_attributes.each do |a|
        define_method a do
          @data[a]
        end
      end

      private

      def data
        @data.to_hash
      end

      def locale
        @locale ||= Geocoder.config[:language].to_s
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
geocoder-1.3.4 lib/geocoder/results/geoip2.rb
geocoder-1.3.3 lib/geocoder/results/geoip2.rb
geocoder-1.3.2 lib/geocoder/results/geoip2.rb
geocoder-1.3.1 lib/geocoder/results/geoip2.rb
geocoder-1.3.0 lib/geocoder/results/geoip2.rb
geocoder-1.2.14 lib/geocoder/results/geoip2.rb
geocoder-1.2.13 lib/geocoder/results/geoip2.rb