Sha256: cd5d155c11af7159eb71905817f38817e1aaa12b195a67692c52c56d72105662

Contents?: true

Size: 1.28 KB

Versions: 11

Compression:

Stored size: 1.28 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
        %w[latitude longitude].map do |l|
          data.fetch('location', {}).fetch(l, 0.0)
        end
      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

11 entries across 11 versions & 3 rubygems

Version Path
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/geocoder-1.4.3/lib/geocoder/results/geoip2.rb
geocoder-1.4.3 lib/geocoder/results/geoip2.rb
geocoder-1.4.2 lib/geocoder/results/geoip2.rb
geocoder-1.4.1 lib/geocoder/results/geoip2.rb
geocoder-1.4.0 lib/geocoder/results/geoip2.rb
geocoder-1.3.7 lib/geocoder/results/geoip2.rb
geocoder-1.3.6 lib/geocoder/results/geoip2.rb
geocoder-1.3.5 lib/geocoder/results/geoip2.rb
broken-geocoder-1.3.6 lib/geocoder/results/geoip2.rb
broken-geocoder-1.3.5 lib/geocoder/results/geoip2.rb
broken-geocoder-1.3.4 lib/geocoder/results/geoip2.rb