Module: RedisGeo::Locstring

Defined in:
lib/redis_geo/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

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/redis_geo/locstring.rb', line 20

def blank?
  !normalized or normalized == ''
end

#cityObject



37
38
39
# File 'lib/redis_geo/locstring.rb', line 37

def city
  CITY_ENDINGS[last_two_words] || CITY_ENDINGS[last_word]
end

#ends_with_country?Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/redis_geo/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_wordsObject



32
33
34
35
# File 'lib/redis_geo/locstring.rb', line 32

def last_two_words
  return last_word unless words.size > 1
  return words[-2..-1].join(' ')
end

#last_wordObject



28
29
30
# File 'lib/redis_geo/locstring.rb', line 28

def last_word
  words.last
end

#lat_lng?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/redis_geo/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

Returns:

  • (Boolean)


72
73
74
# File 'lib/redis_geo/locstring.rb', line 72

def looks_local?
  STREET_ENDINGS.include? last_word
end

#noncity_partObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/redis_geo/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

#normalizedObject



16
17
18
# File 'lib/redis_geo/locstring.rb', line 16

def normalized
  @normalized ||= downcase.gsub(/\W+/, ' ').gsub(/  +/, ' ').strip
end

#obviously_worldwide?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/redis_geo/locstring.rb', line 53

def obviously_worldwide?
  lat_lng? or zipcode? or ends_with_country?
end

#wordsObject



24
25
26
# File 'lib/redis_geo/locstring.rb', line 24

def words
  @words ||= normalized.split
end

#zipcode?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/redis_geo/locstring.rb', line 57

def zipcode?
  last_word =~ /^\d{4,5}$/
end