Sha256: 8125011131abc35a8b24f0eadb7687c8c50bc1d197b27f610ecf72ca0c03ec02

Contents?: true

Size: 963 Bytes

Versions: 2

Compression:

Stored size: 963 Bytes

Contents

require 'geocoder/results/base'

module Geocoder::Result
  class IpinfoIo < Base

    def address(format = :full)
      "#{city} #{postal_code}, #{country}".sub(/^[ ,]*/, "")
    end

    def latitude
      @data['loc'].split(',')[0].to_f
    end

    def longitude
      @data['loc'].split(',')[1].to_f
    end

    def coordinates
        [@data['loc'].split(',')[0].to_f, @data['loc'].split(',')[1].to_f]
    end

    def city
      @data['city']
    end

    def state
      @data['region']
    end

    def country
      @data['country']
    end

    def postal_code
      @data['postal']
    end

    def country_code
      @data.fetch('country', '')
    end

    def state_code
      @data.fetch('region_code', '')
    end

    def self.response_attributes
      %w['ip', 'city', 'region', 'country', 'latitude', 'longitude', 'postal_code']
    end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
geocoder-1.3.3 lib/geocoder/results/ipinfo_io.rb
geocoder-1.3.2 lib/geocoder/results/ipinfo_io.rb