Module: Geonozzle::Locstring
- Defined in:
- lib/geonozzle/locstring.rb
Constant Summary collapse
- STREET_ENDINGS =
%w{ st rd pl av ave street road blvd circle cir dr drive loop ln lane park terr pkwy }.to_set
- ISO2_CODES =
COUNTRY_CODES.keys.map(&:downcase).to_set
- COUNTRY_NAMES =
COUNTRY_CODES.values.map do |name| name.downcase.sub(/\s*\(.*\)\s*/,'') end.to_set
- CITY_ENDINGS =
{}
Instance Method Summary collapse
- #blank? ⇒ Boolean
- #city ⇒ Object
- #ends_with_country? ⇒ Boolean
- #last_two_words ⇒ Object
- #last_word ⇒ Object
- #lat_lng? ⇒ Boolean
- #looks_local? ⇒ Boolean
- #noncity_part ⇒ Object
- #normalized ⇒ Object
- #obviously_worldwide? ⇒ Boolean
- #words ⇒ Object
- #zipcode? ⇒ Boolean
Instance Method Details
#blank? ⇒ Boolean
20 21 22 |
# File 'lib/geonozzle/locstring.rb', line 20 def blank? !normalized or normalized == '' end |
#city ⇒ Object
37 38 39 |
# File 'lib/geonozzle/locstring.rb', line 37 def city CITY_ENDINGS[last_two_words] || CITY_ENDINGS[last_word] end |
#ends_with_country? ⇒ Boolean
67 68 69 70 |
# File 'lib/geonozzle/locstring.rb', line 67 def ends_with_country? return false if looks_local? COUNTRY_NAMES.include? last_word or ISO2_CODES.include? last_word end |
#last_two_words ⇒ Object
32 33 34 35 |
# File 'lib/geonozzle/locstring.rb', line 32 def last_two_words return last_word unless words.size > 1 return words[-2..-1].join(' ') end |
#last_word ⇒ Object
28 29 30 |
# File 'lib/geonozzle/locstring.rb', line 28 def last_word words.last end |
#lat_lng? ⇒ Boolean
61 62 63 64 65 |
# File 'lib/geonozzle/locstring.rb', line 61 def lat_lng? if self =~ /^(.*\s+)?(-?\d+\.\d+)\s*,\s*(-?\d+\.\d+)\s*$/ [$2.to_f, $3.to_f] end end |
#looks_local? ⇒ Boolean
72 73 74 |
# File 'lib/geonozzle/locstring.rb', line 72 def looks_local? STREET_ENDINGS.include? last_word end |
#noncity_part ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/geonozzle/locstring.rb', line 41 def noncity_part x = normalized.split words = if CITY_ENDINGS[last_two_words]; x[0..-3] elsif CITY_ENDINGS[last_word]; x[0..-2] end return self unless words str = words.join(' ') str.extend Locstring str end |
#normalized ⇒ Object
16 17 18 |
# File 'lib/geonozzle/locstring.rb', line 16 def normalized @normalized ||= downcase.gsub(/\W+/, ' ').gsub(/ +/, ' ').strip end |
#obviously_worldwide? ⇒ Boolean
53 54 55 |
# File 'lib/geonozzle/locstring.rb', line 53 def obviously_worldwide? lat_lng? or zipcode? or ends_with_country? end |
#words ⇒ Object
24 25 26 |
# File 'lib/geonozzle/locstring.rb', line 24 def words @words ||= normalized.split end |
#zipcode? ⇒ Boolean
57 58 59 |
# File 'lib/geonozzle/locstring.rb', line 57 def zipcode? last_word =~ /^\d{4,5}$/ end |