Module: Woefoo::TownMatchValidators

Extended by:
TownMatchValidators
Included in:
Woefoo, TownMatchValidators
Defined in:
lib/woefoo/town_match_validators.rb

Instance Method Summary collapse

Instance Method Details

#default_validatorsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/woefoo/town_match_validators.rb', line 9

def default_validators
  [{
    :conditions => Proc.new {|response|
       true # If all else fails, use this catch all validator
    },
    :validator => Proc.new {|response|
      best_match=nil
      if response.input_quality >= min_input_quality
        response.results.each do |r|
          if r[:quality] >= response.input_quality && r[:quality] >= min_result_quality
            place=Woefoo::GeoplanetPlace.find_by_woeid(r[:woeid])
            if place
              if place.place_type=="Town"
                # If the query included a country code, make sure the place we're picking is in the same country!
                next if (response.query_options.include?(:country_code) && 
                  response.query_options[:country_code].downcase != place.country_code.downcase)
                best_match=r[:woeid] 
              elsif ["Zip", "Suburb"].include?(place.place_type) && place.town
                next if (response.query_options.include?(:country_code) && 
                  response.query_options[:country_code].downcase != place.country_code.downcase)
                best_match=place.town.woeid
              end
            else
              puts "Encountered a woeid without a geoplanet place #{r[:woeid]}"
            end
          end
          break if best_match
        end
      end
      best_match
    }
  }]
end