Class: PostcodeAnywhere::PostcodeSearch

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/ruby-postcodeanywhere.rb

Constant Summary collapse

ADDRESS_LOOKUP =
"/Find/v1.10/xmla.ws"
ADDRESS_FETCH =
"/RetrieveById/v1.20/xmla.ws"
RETRIEVE_BY_PARTS_URL =
"/RetrieveByParts/v1.00/xmla.ws"

Instance Method Summary collapse

Instance Method Details

#fetch(id) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/ruby-postcodeanywhere.rb', line 76

def fetch(id)
  options={ :id => id }
   options.merge!(self.license_information)

	data = PostcodeSearch.get( ADDRESS_FETCH, {:query => options} )

	process_address(data)
end

#fetch_by_parts(options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby-postcodeanywhere.rb', line 64

def fetch_by_parts(options={})
   options.merge!(self.license_information)

   if options['postcode']
     options['postcode'] = options['postcode'].gsub(/\s/, '')
   end

	data = PostcodeSearch.get( RETRIEVE_BY_PARTS_URL, {:query => options} )

	process_address(data)
end

#license_informationObject



85
86
87
# File 'lib/ruby-postcodeanywhere.rb', line 85

def license_information
	{:account_code => PostcodeAnywhere., :license_code => PostcodeAnywhere.license_code}
end

#lookup(postcode) ⇒ Object



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
61
62
# File 'lib/ruby-postcodeanywhere.rb', line 26

def lookup(postcode)

  raise "Postcode is Required" if postcode.blank? || postcode.nil?

  options={ "SearchTerm" => postcode.gsub(/\s/, '') }
   options.merge!(self.license_information)

	data = PostcodeSearch.get( ADDRESS_LOOKUP, {:query => options} )
	formatted_data = []

  return formatted_data if data.parsed_response['Table']['Columns']['Column'][0]['Name'] == "Error"

  puts formatted_data

	unless data.parsed_response['Table']['Columns']['Column'][0]['Name'] == "Error"

	  begin
  	  data.parsed_response["Table"]["Rows"]["Row"].each do |item|
 		    data_item = AddressListItem.new
 		    data_item.id = item['Id']
 		    data_item.street_address = item['StreetAddress']
 		    data_item.place = item['Place']

 		    formatted_data << data_item
	    end
    rescue
      item = data.parsed_response["Table"]["Rows"]["Row"]
	    data_item = AddressListItem.new
	    data_item.id = item['Id']
	    data_item.street_address = item['StreetAddress']
	    data_item.place = item['Place']

	    formatted_data << data_item
     end
	end
	formatted_data
end