Sha256: b3bf9b0d8d787ac8f532481d76c3eac3112a03268820fbee148ef93616873aab

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

require 'geocoder2/results/base'

module Geocoder2::Result
  class GeocoderCa < Base

    def coordinates
      [@data['latt'].to_f, @data['longt'].to_f]
    end

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

    def street_address
      "#{@data['stnumber']} #{@data['staddress']}"
    end

    def city
      @data['city']
    end

    def state
      @data['prov']
    end

    alias_method :state_code, :state

    def postal_code
      @data['postal']
    end

    def country
      country_code == 'CA' ? 'Canada' : 'United States'
    end

    def country_code
      return nil if state.nil? || state == ""
      canadian_province_abbreviations.include?(state) ? "CA" : "US"
    end

    def self.response_attributes
      %w[latt longt inlatt inlongt distance stnumber staddress prov
        NearRoad NearRoadDistance betweenRoad1 betweenRoad2
        intersection major_intersection]
    end

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


    private # ----------------------------------------------------------------

    def canadian_province_abbreviations
      %w[ON QC NS NB MB BC PE SK AB NL]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
geocoder2-0.1.1 lib/geocoder2/results/geocoder_ca.rb
geocoder2-0.1.0 lib/geocoder2/results/geocoder_ca.rb