Class: CitySearch

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

Instance Method Summary collapse

Instance Method Details

#dbObject



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

def db
  @all ||= Dawg.load(File.join(data_path, 'russia.bin'))
end

#search(q) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/city_search.rb', line 4

def search(q)
  results = db.query(q.downcase).reject {|r| r.empty? }
  results.map do |r|
    result = r.split(' ')
    code = result.last
    result.pop

    city_name = result.join(' ')
                      .split
                      .map(&:capitalize)
                      .join(' ')
    state = states[code.to_i]

    [city_name, state]
  end
end

#statesObject



25
26
27
28
29
# File 'lib/city_search.rb', line 25

def states
  @states ||= Marshal.load(
    File.read(File.join(data_path, 'subdivisions.bin'))
  )
end