Class: AddressGeocoder::Client Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/address_geocoder/client.rb

Overview

This class is abstract.

Abstract base class for interacting with maps APIs

Since:

  • 0.0.1

Direct Known Subclasses

MapsApi::Google::Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.

Since:

  • 0.0.1



23
24
25
26
# File 'lib/address_geocoder/client.rb', line 23

def initialize(args = {})
  @address = {}
  assign_initial(args)
end

Instance Attribute Details

#addressHash (readonly)

Returns our address object. It contains country, state, city, postal code, and street.

Returns:

  • (Hash)

    our address object. It contains country, state, city, postal code, and street.

Since:

  • 0.0.1



15
16
17
# File 'lib/address_geocoder/client.rb', line 15

def address
  @address
end

#api_keyString

Returns the user’s key to the chosen maps API.

Returns:

  • (String)

    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_addressHash (readonly)

Returns the address that was last called from the maps API.

Returns:

  • (Hash)

    the address that was last called from the maps API

Since:

  • 0.0.1



21
22
23
# File 'lib/address_geocoder/client.rb', line 21

def former_address
  @former_address
end

#languageString

Returns the language in which to return the address.

Returns:

  • (String)

    the language in which to return the address



11
12
13
# File 'lib/address_geocoder/client.rb', line 11

def language
  @language
end

#responseHash (readonly)

Returns the response from the maps API.

Returns:

  • (Hash)

    the response from the maps API

Since:

  • 0.0.1



18
19
20
# File 'lib/address_geocoder/client.rb', line 18

def response
  @response
end

Instance Method Details

#assign_initial(args) ⇒ void

This method is abstract.

Assigns the entered variables to their proper instance variables

This method returns an undefined value.

Parameters:

  • args (Hash)

    arguments to pass to the class

Options Hash (args):

  • :country (String)

    a country’s alpha2

  • :api_key (String)

    the user’s key to the chosen maps API

  • :state (String)

    the state of the address to be validated

  • :city (String)

    the city of the address to be validated

  • :postal_code (String)

    the postal code of the address to be validated

  • :street (String)

    the street of the address to be validated

  • :language (String) — default: en

    the language in which to return the address

Raises:

Since:

  • 0.0.1



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

Parameters:

  • str (String)

    a city name

Returns:

  • (String, nil)

    the entered city, or nil if the provided string could not be verified.

Since:

  • 0.0.1



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

Parameters:

  • str (String)

    a country’s alpha2

Returns:

  • (Hash, nil)

    a country object from the yaml, or nil if the provided alpha2 could not be matched.

Since:

  • 0.0.1



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

Parameters:

  • str (String)

    a postal code

Returns:

  • (String, nil)

    the entered postal code, or nil if the provided string could not be verified.

Since:

  • 0.0.1



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

Parameters:

  • str (String)

    a state name

Returns:

  • (String, nil)

    the entered state, or nil if the provided string could not be verified.

Since:

  • 0.0.1



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

Parameters:

  • str (String)

    a street

Returns:

  • (String, nil)

    the entered street, or nil if the provided string was empty.

Since:

  • 0.0.1



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_addressesArray<Hash>

Gathers a list of matching addresses from the maps API

Returns:

  • (Array<Hash>)

    a list of matching addresses

Since:

  • 0.0.1



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

TODO:

.certain? should be a parser method

Determines whether an address is likely to be valid or not

Returns:

  • (Boolean)

    true, or false if address is likely to be invalid.

Since:

  • 0.0.1



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