Class: PostcodeSoftware::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/postcode_software/response.rb

Overview

Groups together addresses and status information returned from the PostcodeSoftware.net web SDK.

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Response

Initialise with the XML returned from the PostcodeSoftware service.

The xml parameter can be a filename, string or input stream as compatible with Nokogiri.



11
12
13
14
# File 'lib/postcode_software/response.rb', line 11

def initialize(xml)
  @doc = Nokogiri::XML(xml)
  @doc.remove_namespaces!
end

Instance Method Details

#address_1Object

Street name of the postcode.



27
28
29
# File 'lib/postcode_software/response.rb', line 27

def address_1
  try_content '//Address//Address1'
end

#address_2Object

Locality of the postcode or dependent street name if exists.



32
33
34
# File 'lib/postcode_software/response.rb', line 32

def address_2
  try_content '//Address//Address2'
end

#address_3Object

Dependent Locality of the postcode.



37
38
39
# File 'lib/postcode_software/response.rb', line 37

def address_3
  try_content '//Address//Address3'
end

#address_4Object

Double Dependent Locality of the postcode.



42
43
44
# File 'lib/postcode_software/response.rb', line 42

def address_4
  try_content '//Address//Address4'
end

#countyObject

County of the postcode.



52
53
54
# File 'lib/postcode_software/response.rb', line 52

def county
  try_content '//Address//County'
end

#error_messageObject

Returns the error message.



22
23
24
# File 'lib/postcode_software/response.rb', line 22

def error_message
  @doc.at_xpath('//Address//ErrorMessage').content
end

#error_numberObject

Returns the error number.



17
18
19
# File 'lib/postcode_software/response.rb', line 17

def error_number
  @doc.at_xpath('//Address//ErrorNumber').content.to_i
end

#postcodeObject

Postcode that has been searched.



57
58
59
# File 'lib/postcode_software/response.rb', line 57

def postcode
  try_content '//Address//Postcode'
end

#premisesObject

Returns premise data for this postcode.

[
  {
    :organisation=>'Organisation',
    :building_details=>'Bulding Details',
    :number=>'123'
  },
  ...
]


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/postcode_software/response.rb', line 71

def premises
  if @doc.at_xpath('//Address//PremiseData')
    str = @doc.at_xpath('//Address//PremiseData').content
    str
      .split(';')
      .map do |x|
        y = x.split('|', -1)
        {organisation:y[0], building_details:y[1], number:y[2]}
      end
  else
    []
  end
end

#townObject

Town of the postcode.



47
48
49
# File 'lib/postcode_software/response.rb', line 47

def town
  try_content '//Address//Town'
end