-
-
Save jeroenroodnat/279342 to your computer and use it in GitHub Desktop.
6PP KVDB with activeresource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Based on example of http://gist.github.com/276455 but change to not use nokogiri but Activeresource | |
# define file in model directory | |
class Sixpp < ActiveResource::Base | |
class << self | |
# 6pp returns a xml where first item is hash. We need to enter hash["item"] and then check if there is 1 or more records. | |
def instantiate_collection(collection, prefix_options = {}) | |
unless collection["item"].kind_of? Array | |
[instantiate_record(collection["item"], prefix_options)] | |
else | |
collection["item"].collect! { |record| instantiate_record(record, prefix_options) } | |
end | |
end | |
end | |
self.site = "http://6pp.kvdb.net/" | |
def self.lookup(params = {}) | |
begin | |
if params[:postcode] | |
if params[:postcode] =~ /^[1-9]{1}[0-9]{3} ?[a-zA-Z]{2}$/ | |
params[:postcode] = params[:postcode].gsub(/ /,"") | |
else | |
return nil | |
end | |
end | |
find(:all, :from => "/services/lookup", :params => params) | |
rescue | |
return nil | |
end | |
end | |
end | |
# now you can use: Sixpp.lookup(:postcode=>'9999 xx') which returns the results in an array | |
# Sixpp.lookup(:postcode=>'1111 xx')[0].street |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment