Module: Auspost::Postie

Defined in:
lib/auspost/postie.rb,
lib/auspost/active_record/validations.rb

Defined Under Namespace

Modules: ActiveRecord Classes: Cache, Error, Result, Suburb

Instance Method Summary collapse

Instance Method Details

#location?(attrs = {}) ⇒ Boolean

This is the method that returns whether a location is correct or not. It requires three attributes :postcode, :suburb & :state such as location?(:postcode => 2038, :suburb => “Annandale”, :state => “NSW”) #=> Result#status => true location?(:postcode => 2010, :suburb => “Annandale”, :state => “NSW”) #=> Result#status => false Result#errors => Error#accessor => :suburb && Error#message => “Annandale does not match 2010 postcode” The results of a request to a given postcode is cached in memcached if available or in your Rails.cache store if you’re in a Rails project.

Returns:

  • (Boolean)


97
98
99
100
101
102
# File 'lib/auspost/postie.rb', line 97

def location?(attrs = {})
  map_attributes!(attrs)
  url       = "http://www1.auspost.com.au/postcodes/index.asp?Locality=&sub=1&State=&Postcode=#{CGI::escape(@postcode)}&submit1=Search"
  @content  = get(url)
  check_results(attrs)
end