1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
require 'geocoder/results/base'
module Geocoder::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'] or (@data['standard'] and @data['standard']['city']) or ""
end
def state
@data['prov'] or (@data['standard'] and @data['standard']['prov']) or ""
end
alias_method :state_code, :state
def postal_code
@data['postal'] or (@data['standard'] and @data['standard']['postal']) or ""
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
|