Class: ZipSearch::LocationsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/zip_search/locations_controller.rb

Instance Method Summary collapse

Instance Method Details

#location_searchObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/zip_search/locations_controller.rb', line 6

def location_search
  q = params[:q]; by = params[:by]
  results = if by.present?
              case by
              when 'state'
                US_STATES.map{|s| { name: s[0], id: s[1], param: by } }
              when 'city'
                search = Location.search_by_city q
                search.to_a.uniq{|r| [ r[:city], r[:state] ] }.map{|r|
                  { id: r[:id], name: r[:city], param: by,
                    desc: "City in #{r[:state]}" } }
              when 'county'
                search = Location.search_by_county q
                search.to_a.uniq{|r| [ r[:county], r[:state] ] }.map{|r|
                  { id: r[:id], name: r[:county], param: by,
                    desc: "County in #{r[:city]}, #{r[:state]}" } }
              when 'zip'
                search = Location.search_by_zip q
                search.to_a.uniq(&:zip).map{|r|
                  { id: r[:id], name: r[:zip], param: by,
                    desc: "#{r[:county]}, #{r[:city]}, #{r[:state]}" } }
              end
            else [] end
  render json: results
end

#zipcode_searchObject



32
33
34
35
36
37
38
39
40
# File 'app/controllers/zip_search/locations_controller.rb', line 32

def zipcode_search
  results = do_zipcode_search params[:q]
  if results.any?
    results[:status] ||= 'ok'
    render json: results
  else
    render json: { status: 'not ok', error: 'No results' }
  end
end