woefoo 0.0.14 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +15 -3
- data/lib/woefoo.rb +22 -22
- data/lib/woefoo/config.rb +2 -2
- data/lib/woefoo/response.rb +0 -2
- data/lib/woefoo/town_match_validators.rb +25 -5
- data/lib/woefoo/version.rb +1 -1
- data/test/woefoo_test.rb +3 -3
- metadata +4 -4
data/Gemfile
CHANGED
@@ -1,9 +1,21 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
2
|
|
3
|
+
gem "rake", "0.9.2.2"
|
4
|
+
gem "nokogiri", "1.5.0"
|
5
|
+
gem "geokit", "1.5.0"
|
6
|
+
gem "typhoeus", "0.1.27"
|
7
|
+
gem "json", "1.6.1", :require => "json/pure"
|
8
|
+
gem "activerecord", "2.3.14"
|
9
|
+
gem "ancestry", "1.2.4"
|
10
|
+
|
3
11
|
# Specify your gem's dependencies in woefoo.gemspec
|
4
12
|
gemspec
|
5
13
|
|
14
|
+
group :development do
|
15
|
+
gem "mysql", "2.8.1"
|
16
|
+
end
|
17
|
+
|
6
18
|
group :test do
|
7
|
-
gem "mocha", "
|
8
|
-
gem "shoulda"
|
9
|
-
end
|
19
|
+
gem "mocha", "0.10.0"
|
20
|
+
gem "shoulda", "2.11.3"
|
21
|
+
end
|
data/lib/woefoo.rb
CHANGED
@@ -1,17 +1,21 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require 'woefoo/town_match_validators'
|
7
|
-
require 'woefoo/response'
|
8
|
-
require 'woefoo/geoplanet_place'
|
9
|
-
require 'woefoo/geoplanet_alias'
|
10
|
-
require 'woefoo/geoplanet_adjacency'
|
1
|
+
require "cgi"
|
2
|
+
require "typhoeus"
|
3
|
+
require "nokogiri"
|
4
|
+
require "activerecord"
|
5
|
+
require "ancestry"
|
11
6
|
|
12
7
|
module Woefoo
|
8
|
+
|
9
|
+
autoload :Config, "woefoo/config"
|
10
|
+
autoload :TownMatchValidators, "woefoo/town_match_validators"
|
11
|
+
autoload :Response, "woefoo/response"
|
12
|
+
autoload :GeoplanetPlace, "woefoo/geoplanet_place"
|
13
|
+
autoload :GeoplanetAlias, "woefoo/geoplanet_alias"
|
14
|
+
autoload :GeoplanetAdjacency, "woefoo/geoplanet_adjacency"
|
15
|
+
|
13
16
|
include Woefoo::Config
|
14
17
|
include Woefoo::TownMatchValidators
|
18
|
+
|
15
19
|
extend self
|
16
20
|
# Fires a placefinder request to yahoo, returns with suggested places that match the query
|
17
21
|
|
@@ -21,22 +25,20 @@ module Woefoo
|
|
21
25
|
# First try the parameterized query, if the user bothered to separate out the fields
|
22
26
|
if opts.length > 1
|
23
27
|
results[:parameterized_response] = perform_parameterized_lookup(opts)
|
24
|
-
results[:woeid] = canonical_town_match(results[:parameterized_response])
|
28
|
+
results[:woeid] = canonical_town_match(results[:parameterized_response], opts)
|
25
29
|
end
|
26
30
|
|
27
31
|
if !results[:woeid]
|
28
32
|
results[:query_response] = perform_lookup(opts)
|
29
|
-
results[:woeid] = canonical_town_match(results[:query_response])
|
33
|
+
results[:woeid] = canonical_town_match(results[:query_response], opts)
|
30
34
|
end
|
31
|
-
|
32
35
|
results
|
33
36
|
end
|
34
37
|
|
35
38
|
def lookup_town_woeid_by_geo(opts={})
|
36
39
|
results = {:woeid=>nil}
|
37
40
|
results[:geo_response] = perform_geo_lookup(opts)
|
38
|
-
results[:woeid] = canonical_town_match(results[:geo_response])
|
39
|
-
|
41
|
+
results[:woeid] = canonical_town_match(results[:geo_response], opts)
|
40
42
|
results
|
41
43
|
end
|
42
44
|
|
@@ -50,15 +52,14 @@ module Woefoo
|
|
50
52
|
|
51
53
|
# Takes a Woefoo Response, and figures out a Town-level or equivalent
|
52
54
|
# woe id that identifies where the queried address is
|
53
|
-
def canonical_town_match(response)
|
55
|
+
def canonical_town_match(response, input_opts = {})
|
54
56
|
default_validators.each do |v|
|
55
|
-
if v[:conditions].call(response)
|
56
|
-
return v[:validator].call(response)
|
57
|
+
if v[:conditions].call(response, input_opts)
|
58
|
+
return v[:validator].call(response, input_opts)
|
57
59
|
end
|
58
60
|
end
|
59
61
|
end
|
60
62
|
|
61
|
-
|
62
63
|
# Build a single query url
|
63
64
|
def build_query_url(opts={})
|
64
65
|
s = ""
|
@@ -85,7 +86,6 @@ module Woefoo
|
|
85
86
|
"http://where.yahooapis.com/geocode?appid=#{@appid}&count=#{placefinder_result_count}#{s}"
|
86
87
|
end
|
87
88
|
|
88
|
-
|
89
89
|
def build_geo_url(opts={})
|
90
90
|
"http://where.yahooapis.com/geocode?appid=#{@appid}&count=#{placefinder_result_count}&gflags=R&q=#{opts[:lat]},+#{opts[:lng]}"
|
91
91
|
end
|
@@ -111,11 +111,11 @@ module Woefoo
|
|
111
111
|
def build_query_keys(opts={})
|
112
112
|
k = [:line1, :city, :state, :postal, :country]
|
113
113
|
if opts.include?(:country)
|
114
|
-
|
114
|
+
unless ["US", "CA", "GB", "UK"].include? opts[:country]
|
115
115
|
k.delete(:state)
|
116
116
|
k.delete(:postal)
|
117
117
|
end
|
118
118
|
end
|
119
119
|
k
|
120
120
|
end
|
121
|
-
end
|
121
|
+
end
|
data/lib/woefoo/config.rb
CHANGED
@@ -14,11 +14,11 @@ module Woefoo
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def min_input_quality
|
17
|
-
(@min_input_quality && @min_input_quality > 0) ? @min_input_quality :
|
17
|
+
(@min_input_quality && @min_input_quality > 0) ? @min_input_quality : 39
|
18
18
|
end
|
19
19
|
|
20
20
|
def min_result_quality
|
21
|
-
(@min_result_quality && @min_result_quality > 0) ? @min_result_quality :
|
21
|
+
(@min_result_quality && @min_result_quality > 0) ? @min_result_quality : 39
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
data/lib/woefoo/response.rb
CHANGED
@@ -1,22 +1,38 @@
|
|
1
1
|
# A town match validator is a hash of two procs, the first proc is
|
2
2
|
# a procedure for conditions in which this validator should be used,
|
3
3
|
# and the second proc is the actual validator to run on the place results
|
4
|
-
require 'geokit'
|
5
4
|
|
6
5
|
module Woefoo::TownMatchValidators
|
6
|
+
|
7
|
+
# is this needed?
|
7
8
|
extend self
|
8
9
|
|
10
|
+
|
11
|
+
# np1 = [51.591049,-2.988250]
|
12
|
+
# np2 = [52.016029,-4.833970]
|
13
|
+
# puts D.distance_between(np1, np2)
|
14
|
+
#
|
15
|
+
# D basically gives a way to call GeoKit::Mappable::ClassMethods.distance_between
|
16
|
+
class D
|
17
|
+
extend GeoKit::Mappable::ClassMethods
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
def close_enough_to_input_lat_lng? a,b
|
22
|
+
D.distance_between(a, b) <= 15.0
|
23
|
+
end
|
24
|
+
|
9
25
|
def default_validators
|
10
26
|
[{
|
11
|
-
:conditions => Proc.new {|response|
|
27
|
+
:conditions => Proc.new {|response, input_opts|
|
12
28
|
true # If all else fails, use this catch all validator
|
13
29
|
},
|
14
|
-
:validator => Proc.new {|response|
|
30
|
+
:validator => Proc.new {|response, input_opts|
|
15
31
|
best_match=nil
|
16
32
|
if response.input_quality >= min_input_quality
|
17
33
|
response.results.each do |r|
|
18
|
-
if r[:quality] >=
|
19
|
-
place=Woefoo::GeoplanetPlace.find_by_woeid(r[:woeid])
|
34
|
+
if r[:quality] >= min_result_quality
|
35
|
+
place = Woefoo::GeoplanetPlace.find_by_woeid(r[:woeid])
|
20
36
|
if place
|
21
37
|
if place.place_type=="Town"
|
22
38
|
# If the query included a country code, make sure the place we're picking is in the same country!
|
@@ -32,6 +48,10 @@ module Woefoo::TownMatchValidators
|
|
32
48
|
puts "Encountered a woeid without a geoplanet place #{r[:woeid]}"
|
33
49
|
end
|
34
50
|
end
|
51
|
+
if input_opts[:lat] && input_opts[:lng]
|
52
|
+
best_match = nil unless
|
53
|
+
close_enough_to_input_lat_lng?([input_opts[:lat], input_opts[:lng]], [r[:latitude], r[:longitude]])
|
54
|
+
end
|
35
55
|
break if best_match
|
36
56
|
end
|
37
57
|
end
|
data/lib/woefoo/version.rb
CHANGED
data/test/woefoo_test.rb
CHANGED
@@ -59,8 +59,8 @@ class WoefooTest < Test::Unit::TestCase
|
|
59
59
|
should "use default validator if no users validators are specified" do
|
60
60
|
response = mock()
|
61
61
|
place = mock()
|
62
|
-
Woefoo::GeoplanetPlace.
|
63
|
-
response.expects(:input_quality).
|
62
|
+
Woefoo::GeoplanetPlace.stubs(:find_by_woeid).with(12345).returns(place)
|
63
|
+
response.expects(:input_quality).once.returns(75)
|
64
64
|
response.expects(:results).returns([{:quality => 80, :woeid=>12345, :woetype=>7}])
|
65
65
|
response.expects(:query_options).returns({})
|
66
66
|
place.expects(:place_type).returns("Town")
|
@@ -69,4 +69,4 @@ class WoefooTest < Test::Unit::TestCase
|
|
69
69
|
assert_equal 12345, best_woeid, "WOEID town match mismatch!"
|
70
70
|
end
|
71
71
|
end
|
72
|
-
end
|
72
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: woefoo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
|
+
- 1
|
7
8
|
- 0
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.14
|
10
|
+
version: 1.0.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- smnirven
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-11-
|
18
|
+
date: 2011-11-24 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: typhoeus
|