Class: AddressGeocoder::Client Abstract
- Inherits:
-
Object
- Object
- AddressGeocoder::Client
- Defined in:
- lib/address_geocoder/client.rb
Overview
Abstract base class for interacting with maps APIs
Direct Known Subclasses
Instance Attribute Summary collapse
-
#address ⇒ Hash
readonly
Our address object.
-
#api_key ⇒ String
The user’s key to the chosen maps API.
-
#former_address ⇒ Hash
readonly
The address that was last called from the maps API.
-
#language ⇒ String
The language in which to return the address.
-
#response ⇒ Hash
readonly
The response from the maps API.
Instance Method Summary collapse
- #assign_initial(args) ⇒ void abstract
-
#city=(str) ⇒ String?
Assigns the given city to the address object if it passes verification.
-
#country=(str) ⇒ Hash?
Matches the given alpha2 to a yaml country and assigns it to the address object.
-
#initialize(args = {}) ⇒ Client
constructor
A new instance of Client.
-
#postal_code=(str) ⇒ String?
Assigns the given postal code to the address object if it passes verification.
-
#state=(str) ⇒ String?
Assigns the given state to the address object if it passes verification.
-
#street=(str) ⇒ String?
Assigns the given street to the address object if it passes verification.
-
#suggested_addresses ⇒ Array<Hash>
Gathers a list of matching addresses from the maps API.
-
#valid_address? ⇒ Boolean
Determines whether an address is likely to be valid or not.
Constructor Details
#initialize(args = {}) ⇒ Client
Returns a new instance of Client.
23 24 25 26 |
# File 'lib/address_geocoder/client.rb', line 23 def initialize(args = {}) @address = {} assign_initial(args) end |
Instance Attribute Details
#address ⇒ Hash (readonly)
Returns our address object. It contains country, state, city, postal code, and street.
15 16 17 |
# File 'lib/address_geocoder/client.rb', line 15 def address @address end |
#api_key ⇒ String
Returns the user’s key to the chosen maps API.
8 9 10 |
# File 'lib/address_geocoder/client.rb', line 8 def api_key @api_key end |
#former_address ⇒ Hash (readonly)
Returns the address that was last called from the maps API.
21 22 23 |
# File 'lib/address_geocoder/client.rb', line 21 def former_address @former_address end |
#language ⇒ String
Returns the language in which to return the address.
11 12 13 |
# File 'lib/address_geocoder/client.rb', line 11 def language @language end |
#response ⇒ Hash (readonly)
Returns the response from the maps API.
18 19 20 |
# File 'lib/address_geocoder/client.rb', line 18 def response @response end |
Instance Method Details
#assign_initial(args) ⇒ void
Assigns the entered variables to their proper instance variables
This method returns an undefined value.
67 68 69 70 71 72 73 74 75 |
# File 'lib/address_geocoder/client.rb', line 67 def assign_initial(args) raise NeedToOveride, 'assign_initial' unless @requester && @parser Client.instance_methods(false).each do |var| next if var.to_s[/\=/].nil? value = args[var.to_s.tr('=', '').to_sym].to_s next unless value send(var, value) end end |
#city=(str) ⇒ String?
Assigns the given city to the address object if it passes verification
104 105 106 |
# File 'lib/address_geocoder/client.rb', line 104 def city=(str) @address[:city] = simple_check_and_assign!(str) end |
#country=(str) ⇒ Hash?
Matches the given alpha2 to a yaml country and assigns it to the address object
82 83 84 85 86 87 88 89 90 |
# File 'lib/address_geocoder/client.rb', line 82 def country=(str) if COUNTRIES[str] @address[:country] = COUNTRIES[str] @address[:country][:alpha2] = str else @address[:country] = nil end @address[:country] end |
#postal_code=(str) ⇒ String?
Assigns the given postal code to the address object if it passes verification
113 114 115 |
# File 'lib/address_geocoder/client.rb', line 113 def postal_code=(str) @address[:postal_code] = pc_check_and_assign!(str) end |
#state=(str) ⇒ String?
Assigns the given state to the address object if it passes verification
96 97 98 |
# File 'lib/address_geocoder/client.rb', line 96 def state=(str) @address[:state] = simple_check_and_assign!(str) end |
#street=(str) ⇒ String?
Assigns the given street to the address object if it passes verification
122 123 124 125 |
# File 'lib/address_geocoder/client.rb', line 122 def street=(str) @address[:street] = nil @address[:street] = str unless str.empty? end |
#suggested_addresses ⇒ Array<Hash>
Gathers a list of matching addresses from the maps API
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/address_geocoder/client.rb', line 42 def suggested_addresses check_country if values_changed? reset @requester.make_call end return false unless @requester.success? @requester.array_result.map do |result| @parser.fields = result @parser.parse_response end end |
#valid_address? ⇒ Boolean
.certain? should be a parser method
Determines whether an address is likely to be valid or not
31 32 33 34 35 36 37 38 |
# File 'lib/address_geocoder/client.rb', line 31 def valid_address? check_country if values_changed? reset @requester.make_call end @requester.success? && @requester.certain? end |