woefoo 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/woefoo/config.rb +15 -17
- data/lib/woefoo/geoplanet_adjacency.rb +5 -6
- data/lib/woefoo/geoplanet_alias.rb +3 -5
- data/lib/woefoo/geoplanet_place.rb +45 -46
- data/lib/woefoo/town_match_validators.rb +27 -29
- data/lib/woefoo/version.rb +1 -1
- data/lib/woefoo.rb +37 -8
- data/test/woefoo_test.rb +4 -12
- metadata +5 -7
data/lib/woefoo/config.rb
CHANGED
@@ -1,22 +1,20 @@
|
|
1
|
-
module Woefoo
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
attr :min_input_quality, true
|
1
|
+
module Woefoo::Config
|
2
|
+
# appid is the user's Yahoo app id (http://developer.yahoo.com/geo/placefinder/)
|
3
|
+
attr :appid, true
|
4
|
+
# the number of results to return for each request
|
5
|
+
attr :placefinder_result_count, true
|
6
|
+
# the minimum input quality for a canonical town match
|
7
|
+
attr :min_input_quality, true
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
def placefinder_result_count
|
10
|
+
(@placefinder_result_count && @placefinder_result_count > 0) ? @placefinder_result_count : 3
|
11
|
+
end
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
def min_input_quality
|
14
|
+
(@min_input_quality && @min_input_quality > 0) ? @min_input_quality : 40
|
15
|
+
end
|
17
16
|
|
18
|
-
|
19
|
-
|
20
|
-
end
|
17
|
+
def min_result_quality
|
18
|
+
(@min_result_quality && @min_result_quality > 0) ? @min_result_quality : 80
|
21
19
|
end
|
22
20
|
end
|
@@ -1,7 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
end
|
1
|
+
class Woefoo::GeoplanetAdjacency < ActiveRecord::Base
|
2
|
+
set_table_name "geoplanet_adjacencies"
|
3
|
+
belongs_to :place, :class_name => 'GeoplanetPlace', :foreign_key => 'woeid', :primary_key => 'woeid'
|
4
|
+
belongs_to :adjacent_place, :class_name => 'GeoplanetPlace', :foreign_key => 'neighbour_woeid', :primary_key => 'woeid'
|
5
|
+
|
7
6
|
end
|
@@ -1,9 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
belongs_to :place,
|
1
|
+
class Woefoo::GeoplanetAlias < ActiveRecord::Base
|
2
|
+
set_table_name "geoplanet_aliases"
|
3
|
+
belongs_to :place,
|
5
4
|
:class_name => 'GeoplanetPlace',
|
6
5
|
:foreign_key => 'woeid',
|
7
6
|
:primary_key => 'woeid'
|
8
|
-
end
|
9
7
|
end
|
@@ -1,61 +1,60 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
set_primary_key 'woeid'
|
1
|
+
class Woefoo::GeoplanetPlace < ActiveRecord::Base
|
2
|
+
set_table_name "geoplanet_places"
|
3
|
+
set_primary_key 'woeid'
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
has_many :aliases, :class_name => 'GeoplanetAlias', :foreign_key => 'woeid'
|
6
|
+
has_many :adjacencies, :class_name => 'GeoplanetAdjacency', :foreign_key => 'woeid'
|
7
|
+
has_many :adjacent_places, :through => :adjacencies
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
belongs_to :parent,
|
10
|
+
:class_name => "GeoplanetPlace",
|
11
|
+
:foreign_key => 'parent_woeid',
|
12
|
+
:primary_key => 'woeid'
|
14
13
|
|
15
|
-
|
14
|
+
has_ancestry
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
def town
|
17
|
+
self.ancestors.find(:first, :conditions => {:place_type => "Town"})
|
18
|
+
end
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
def region
|
21
|
+
self.ancestors.find(:first, :conditions => {:place_type => "State"})
|
22
|
+
end
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
def country
|
25
|
+
self.ancestors.find(:first, :conditions => {:place_type => "Country"})
|
26
|
+
end
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
def continent
|
29
|
+
self.ancestors.find(:first, :conditions => {:place_type => "Continent"})
|
30
|
+
end
|
32
31
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
return @full_name
|
32
|
+
def full_name
|
33
|
+
if !@full_name
|
34
|
+
@full_name = self.name
|
35
|
+
@full_name = "#{@full_name}, #{self.region.name}" if self.region
|
36
|
+
@full_name = "#{@full_name}, #{self.country.name}" if self.country
|
41
37
|
end
|
38
|
+
|
39
|
+
return @full_name
|
40
|
+
end
|
42
41
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
42
|
+
def full_name_parts
|
43
|
+
ret = {:name => self.name}
|
44
|
+
ret[:region_name] = self.region.name if self.region
|
45
|
+
ret[:country_name] = self.country.name if self.country
|
46
|
+
ret[:continent_name] = self.continent.name if self.continent
|
47
|
+
return ret
|
48
|
+
end
|
50
49
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
57
|
-
build_ancestry_from_parent_ids! node.id, if ancestry.nil? then "#{node.id}" else "#{ancestry}/#{node.id}" end
|
50
|
+
def self.build_ancestry_from_parent_ids! parent_id = nil, ancestry = nil
|
51
|
+
parent_id = parent_id || 0
|
52
|
+
self.base_class.all(:conditions => {:parent_woeid => parent_id}).each do |node|
|
53
|
+
node.without_ancestry_callbacks do
|
54
|
+
node.update_attribute ancestry_column, ancestry
|
58
55
|
end
|
56
|
+
build_ancestry_from_parent_ids! node.id, if ancestry.nil? then "#{node.id}" else "#{ancestry}/#{node.id}" end
|
59
57
|
end
|
60
58
|
end
|
59
|
+
|
61
60
|
end
|
@@ -3,38 +3,36 @@
|
|
3
3
|
# and the second proc is the actual validator to run on the place results
|
4
4
|
require 'geokit'
|
5
5
|
|
6
|
-
module Woefoo
|
7
|
-
|
8
|
-
extend self
|
6
|
+
module Woefoo::TownMatchValidators
|
7
|
+
extend self
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
9
|
+
def default_validators
|
10
|
+
[{
|
11
|
+
:conditions => Proc.new {|response|
|
12
|
+
true # If all else fails, use this catch all validator
|
13
|
+
},
|
14
|
+
:validator => Proc.new {|response|
|
15
|
+
best_match=nil
|
16
|
+
if response.input_quality >= min_input_quality
|
17
|
+
response.results.each do |r|
|
18
|
+
if r[:quality] >= response.input_quality && r[:quality] >= min_result_quality
|
19
|
+
place=Woefoo::GeoplanetPlace.find_by_woeid(r[:woeid])
|
20
|
+
if place.place_type=="Town"
|
21
|
+
# If the query included a country code, make sure the place we're picking is in the same country!
|
22
|
+
next if (response.query_options.include?(:country_code) &&
|
23
|
+
response.query_options[:country_code].downcase != place.country_code.downcase)
|
24
|
+
best_match=r[:woeid]
|
25
|
+
elsif ["Zip", "Suburb"].include?(place.place_type) && place.town
|
26
|
+
next if (response.query_options.include?(:country_code) &&
|
27
|
+
response.query_options[:country_code].downcase != place.country_code.downcase)
|
28
|
+
best_match=place.town.woeid
|
31
29
|
end
|
32
|
-
break if best_match
|
33
30
|
end
|
31
|
+
break if best_match
|
34
32
|
end
|
35
|
-
|
36
|
-
|
37
|
-
}
|
38
|
-
|
33
|
+
end
|
34
|
+
best_match
|
35
|
+
}
|
36
|
+
}]
|
39
37
|
end
|
40
38
|
end
|
data/lib/woefoo/version.rb
CHANGED
data/lib/woefoo.rb
CHANGED
@@ -14,11 +14,25 @@ module Woefoo
|
|
14
14
|
include Woefoo::TownMatchValidators
|
15
15
|
extend self
|
16
16
|
# Fires a placefinder request to yahoo, returns with suggested places that match the query
|
17
|
-
|
17
|
+
|
18
|
+
def lookup_town_woeid(opts={})
|
18
19
|
validate!(opts)
|
19
|
-
|
20
|
+
woeid=nil
|
21
|
+
# First try the parameterized query, if the user bothered to separate out the fields
|
22
|
+
if opts.length > 1
|
23
|
+
response = perform_parameterized_lookup(opts)
|
24
|
+
woeid = canonical_town_match(response)
|
25
|
+
end
|
26
|
+
|
27
|
+
if !woeid
|
28
|
+
response = perform_lookup(opts)
|
29
|
+
woeid = canonical_town_match(response)
|
30
|
+
end
|
31
|
+
|
32
|
+
woeid
|
20
33
|
end
|
21
34
|
|
35
|
+
|
22
36
|
# Takes a Woefoo Response, and figures out a Town-level or equivalent
|
23
37
|
# woe id that identifies where the queried address is
|
24
38
|
def canonical_town_match(response)
|
@@ -29,8 +43,6 @@ module Woefoo
|
|
29
43
|
end
|
30
44
|
end
|
31
45
|
|
32
|
-
private
|
33
|
-
|
34
46
|
def validate!(opts)
|
35
47
|
raise "Woefoo.appid is not defined" if !@appid
|
36
48
|
if opts.length == 0 || opts.include?(:query) && (opts[:query] == "")
|
@@ -38,7 +50,8 @@ module Woefoo
|
|
38
50
|
end
|
39
51
|
end
|
40
52
|
|
41
|
-
|
53
|
+
# Build a single query url
|
54
|
+
def build_query_url(opts={})
|
42
55
|
s = ""
|
43
56
|
if opts.include?(:query)
|
44
57
|
s = opts[:query]
|
@@ -47,12 +60,28 @@ module Woefoo
|
|
47
60
|
s << "#{opts[l]} " if opts.include?(l)
|
48
61
|
end
|
49
62
|
end
|
50
|
-
::CGI.escape(s)
|
63
|
+
s = ::CGI.escape(s)
|
64
|
+
"http://where.yahooapis.com/geocode?appid=#{@appid}&count=#{placefinder_result_count}&q=#{s}"
|
65
|
+
end
|
66
|
+
|
67
|
+
# Build a parameterized query url
|
68
|
+
def build_parameterized_url(opts={})
|
69
|
+
s = ""
|
70
|
+
[:line1, :city, :state, :postal, :country].each do |l|
|
71
|
+
s << "&#{l.to_s}=#{opts[l]} " if opts.include?(l)
|
72
|
+
end
|
73
|
+
s = ::CGI.escape(s)
|
74
|
+
"http://where.yahooapis.com/geocode?appid=#{@appid}&count=#{placefinder_result_count}#{s}"
|
51
75
|
end
|
52
76
|
|
53
77
|
def perform_lookup(opts)
|
54
|
-
|
55
|
-
|
78
|
+
url = build_query_url(opts)
|
79
|
+
tr = Typhoeus::Request.get(url)
|
80
|
+
Woefoo::Response.new(opts, tr) if tr
|
81
|
+
end
|
82
|
+
|
83
|
+
def perform_parameterized_lookup(opts)
|
84
|
+
url = build_parameterized_url(opts)
|
56
85
|
tr = Typhoeus::Request.get(url)
|
57
86
|
Woefoo::Response.new(opts, tr) if tr
|
58
87
|
end
|
data/test/woefoo_test.rb
CHANGED
@@ -5,7 +5,7 @@ class WoefooTest < Test::Unit::TestCase
|
|
5
5
|
should "throw an exception if appid is not defined" do
|
6
6
|
assert_raises RuntimeError do
|
7
7
|
begin
|
8
|
-
Woefoo.
|
8
|
+
Woefoo.lookup_town_woeid(COPTER_WORLD_HQ)
|
9
9
|
rescue RuntimeError => ex
|
10
10
|
assert_equal ex.message, "Woefoo.appid is not defined"
|
11
11
|
raise
|
@@ -17,7 +17,7 @@ class WoefooTest < Test::Unit::TestCase
|
|
17
17
|
assert_raises RuntimeError do
|
18
18
|
begin
|
19
19
|
Woefoo.appid=TEST_APPID
|
20
|
-
Woefoo.
|
20
|
+
Woefoo.lookup_town_woeid
|
21
21
|
rescue RuntimeError => ex
|
22
22
|
assert ex.message.include?("Invalid query")
|
23
23
|
raise
|
@@ -29,7 +29,7 @@ class WoefooTest < Test::Unit::TestCase
|
|
29
29
|
assert_raises RuntimeError do
|
30
30
|
begin
|
31
31
|
Woefoo.appid=TEST_APPID
|
32
|
-
Woefoo.
|
32
|
+
Woefoo.lookup_town_woeid({:query => ""})
|
33
33
|
rescue RuntimeError => ex
|
34
34
|
assert ex.message.include?("Invalid query")
|
35
35
|
raise
|
@@ -39,15 +39,7 @@ class WoefooTest < Test::Unit::TestCase
|
|
39
39
|
end
|
40
40
|
|
41
41
|
context "lookup url generation" do
|
42
|
-
|
43
|
-
typh_resp = Typhoeus::Response.new
|
44
|
-
Woefoo.appid=TEST_APPID
|
45
|
-
url_should_be="#{YAHOO_BASE_URL}?appid=#{TEST_APPID}&count=3&q=#{::CGI.escape(COPTER_WORLD_HQ)}"
|
46
|
-
Typhoeus::Request.expects(:get).with(url_should_be).returns(typh_resp).at_least_once
|
47
|
-
Woefoo::Response.expects(:new).with({:query => COPTER_WORLD_HQ}, typh_resp).at_least_once
|
48
|
-
|
49
|
-
Woefoo.lookup({:query => COPTER_WORLD_HQ})
|
50
|
-
end
|
42
|
+
|
51
43
|
end
|
52
44
|
|
53
45
|
context "canonical town matching" do
|
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: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 7
|
10
|
+
version: 0.0.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- smnirven
|
@@ -15,8 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
19
|
-
default_executable:
|
18
|
+
date: 2011-11-08 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
21
|
name: typhoeus
|
@@ -126,7 +125,6 @@ files:
|
|
126
125
|
- test/test_helper.rb
|
127
126
|
- test/woefoo_test.rb
|
128
127
|
- woefoo.gemspec
|
129
|
-
has_rdoc: true
|
130
128
|
homepage: http://smnirven.com
|
131
129
|
licenses: []
|
132
130
|
|
@@ -156,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
154
|
requirements: []
|
157
155
|
|
158
156
|
rubyforge_project: woefoo
|
159
|
-
rubygems_version: 1.
|
157
|
+
rubygems_version: 1.8.10
|
160
158
|
signing_key:
|
161
159
|
specification_version: 3
|
162
160
|
summary: Lookup yahoo woe (where on earth) ids
|