Class: PostcodeAnywhere::PostcodeSearch
- Inherits:
-
Request
- Object
- Request
- PostcodeAnywhere::PostcodeSearch
show all
- Includes:
- HTTParty
- Defined in:
- lib/postcodeanywhere.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Request
account_code, account_code=, license_code, license_code=
Constructor Details
Returns a new instance of PostcodeSearch.
30
31
32
33
34
|
# File 'lib/postcodeanywhere.rb', line 30
def initialize(args={})
self.postcode = args[:postcode]
self.country_code = args[:country_code]
self.fetch_id = args[:fetch_id]
end
|
Instance Attribute Details
#country_code ⇒ Object
Returns the value of attribute country_code.
28
29
30
|
# File 'lib/postcodeanywhere.rb', line 28
def country_code
@country_code
end
|
#fetch_id ⇒ Object
Returns the value of attribute fetch_id.
28
29
30
|
# File 'lib/postcodeanywhere.rb', line 28
def fetch_id
@fetch_id
end
|
#postcode ⇒ Object
Returns the value of attribute postcode.
28
29
30
|
# File 'lib/postcodeanywhere.rb', line 28
def postcode
@postcode
end
|
Instance Method Details
#address_fetch_id ⇒ Object
85
86
87
|
# File 'lib/postcodeanywhere.rb', line 85
def address_fetch_id
"id="+self.fetch_id
end
|
#fetch ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/postcodeanywhere.rb', line 45
def fetch
data = PostcodeSearch.get self.fetch_url
formatted_data = data["NewDataSet"]["Data"]
@address_lookup = AddressLookup.new
if self.country_code == 'GB'
@address_lookup.postcode = formatted_data["postcode"]
@address_lookup.address_line_1 = formatted_data["line1"]
@address_lookup.address_line_2 = formatted_data["line2"]
@address_lookup.address_line_3 = formatted_data["line3"]
@address_lookup.post_town = formatted_data["post_town"]
@address_lookup.county = formatted_data["county"].blank? ? formatted_data["post_town"] : formatted_data["county"]
elsif self.country_code == 'US'
@address_lookup.postcode = formatted_data["zip4"].blank? ? @postcode_search.postcode : formatted_data["zip4"]
@address_lookup.address_line_1 = formatted_data["line1"]
@address_lookup.address_line_2 = formatted_data["line2"]
@address_lookup.address_line_3 = formatted_data["line3"]
@address_lookup.post_town = formatted_data["city"]
@address_lookup.county = formatted_data["county_name"]+", "+formatted_data["state"]
else
end
@address_lookup
end
|
#fetch_url ⇒ Object
73
74
75
|
# File 'lib/postcodeanywhere.rb', line 73
def fetch_url
ADDRESS_FETCH+"&"+self.address_fetch_id+self.selected_country+"&"+self.license_information
end
|
#lookup ⇒ Object
36
37
38
39
40
41
42
43
|
# File 'lib/postcodeanywhere.rb', line 36
def lookup
data = PostcodeSearch.get self.lookup_url
formatted_data = []
unless data["PostcodeAnywhere"]["Schema"]["Field"][0]["Name"] == "error_number"
formatted_data = data["PostcodeAnywhere"]["Data"]["Item"]
end
formatted_data
end
|
#lookup_type ⇒ Object
89
90
91
92
93
94
95
96
97
|
# File 'lib/postcodeanywhere.rb', line 89
def lookup_type
if self.country_code == "GB"
"action=lookup&type=by_postcode"
elsif self.country_code == "US"
"action=lookup&type=by_zip"
else
"action=international&type=fetch_streets"
end
end
|
#lookup_url ⇒ Object
69
70
71
|
# File 'lib/postcodeanywhere.rb', line 69
def lookup_url
ADDRESS_LOOKUP+"?"+self.lookup_type+"&"+self.postcode_with_no_spaces+self.selected_country+"&"+self.license_information
end
|
#postcode_with_no_spaces ⇒ Object
99
100
101
|
# File 'lib/postcodeanywhere.rb', line 99
def postcode_with_no_spaces
(self.country_code=="US" ? "zip=" : "postcode=")+self.postcode.gsub(/\s/, '')
end
|
#selected_country ⇒ Object
77
78
79
80
81
82
83
|
# File 'lib/postcodeanywhere.rb', line 77
def selected_country
if self.country_code == "GB"
""
else
"&country="+self.country_code
end
end
|