Module: Woefoo::TownMatchValidators

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

Overview

A town match validator is a hash of two procs, the first proc is a procedure for conditions in which this validator should be used, and the second proc is the actual validator to run on the place results

Defined Under Namespace

Classes: D

Instance Method Summary collapse

Instance Method Details

#close_enough_to_input_lat_lng?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


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

def close_enough_to_input_lat_lng? a,b
  D.distance_between(a, b) <= 15.0
end

#default_validatorsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/woefoo/town_match_validators.rb', line 25

def default_validators
  [{
    :conditions => Proc.new {|response, input_opts|
       true # If all else fails, use this catch all validator
    },
    :validator => Proc.new {|response, input_opts|
      best_match=nil
      if response.input_quality >= min_input_quality
        response.results.each do |r|
          if 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
          if input_opts[:lat] && input_opts[:lng]
            best_match = nil unless
              close_enough_to_input_lat_lng?([input_opts[:lat], input_opts[:lng]], [r[:latitude], r[:longitude]])
          end
          break if best_match
        end
      end
      best_match
    }
  }]
end