Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/auto_location/string.rb

Instance Method Summary collapse

Instance Method Details

#city(city) ⇒ Object

Find a city with the search string



34
35
36
37
38
39
40
41
42
# File 'lib/auto_location/string.rb', line 34

def city(city)
  city = city.upcase
  results = []
  AutoLocation.cities.find_all do |row|
    results << row unless city[row[0]] == nil
  end
  result = results == [] ? nil : results.max {|a,b| a[1].length <=> b[1].length}
  result == nil ? false : { location: {city: result[1], state: result[2]}, type: 'city' }
end

#city_or_county(input) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/auto_location/string.rb', line 25

def city_or_county(input)
  city_res = city(input)
  county_res = county(input)
  return county_res if city_res == false
  return city_res if county_res == false
  city_res[:location][:city].split(' ').length >= county_res[:location][:county].split(' ').length ? city_res : county_res
end

#city_perfect_match(city) ⇒ Object



19
20
21
22
23
# File 'lib/auto_location/string.rb', line 19

def city_perfect_match(city)
  city = city.upcase
  result = AutoLocation.cities_hash[city]
  result == nil ? false : { location: {city: result[0], state: result[1]}, type: 'city' }
end

#county(county) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/auto_location/string.rb', line 55

def county(county)
  county = county.upcase
  results = []
  AutoLocation.counties.find_all do |row|
    results << row unless county[row[0]] == nil
  end
  result = results == [] ? nil : results.max {|a,b| a[1].length <=> b[1].length}
  result == nil ? false : { location: {county: result[1], state: result[2]}, type: 'county' }
end

#county_perfect_match(county) ⇒ Object



65
66
67
68
69
# File 'lib/auto_location/string.rb', line 65

def county_perfect_match(county)
  county = county.upcase
  result = AutoLocation.counties_hash[county]
  result == nil ? false : { location: {county: result[0], state: result[1]}, type: 'county' }
end

#state(state) ⇒ Object

Find a state with the search string



45
46
47
48
49
50
51
52
53
# File 'lib/auto_location/string.rb', line 45

def state(state)
  state = state.upcase
  # add a white space for easier regex match
  state.split(/[\s\,]/).each do |token|
    found_state = AutoLocation.states[token]
    return { location: found_state, type: 'state' } unless AutoLocation.states[token] == nil
  end
  false
end

#validated_locationObject

Add methods to String class



3
4
5
6
7
8
# File 'lib/auto_location/string.rb', line 3

def validated_location
  # If search includes valid zipcode, return zipcode
  # If zipcode doesn't exist, do city search.
  # If city doesn't exist, do state search.
  zipcode(self) || city_perfect_match(self) || county_perfect_match(self) || city_or_county(self) || state(self) || AutoLocation.not_found_location
end

#zipcode(zipcode) ⇒ Object

Check if zipcode exists



11
12
13
14
15
16
17
# File 'lib/auto_location/string.rb', line 11

def zipcode(zipcode)
  zipcode.scan(/\d{5}/).each do |token|
    result = AutoLocation.zips[token.to_i]
    return { location: {zipcode: token, city: result[0], state: result[1]}, type: 'zipcode' } unless result == nil
  end
  false
end