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 CHANGED
@@ -1,22 +1,20 @@
1
- module Woefoo
2
- module Config
3
- # appid is the user's Yahoo app id (http://developer.yahoo.com/geo/placefinder/)
4
- attr :appid, true
5
- # the number of results to return for each request
6
- attr :placefinder_result_count, true
7
- # the minimum input quality for a canonical town match
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
- def placefinder_result_count
11
- (@placefinder_result_count && @placefinder_result_count > 0) ? @placefinder_result_count : 3
12
- end
9
+ def placefinder_result_count
10
+ (@placefinder_result_count && @placefinder_result_count > 0) ? @placefinder_result_count : 3
11
+ end
13
12
 
14
- def min_input_quality
15
- (@min_input_quality && @min_input_quality > 0) ? @min_input_quality : 40
16
- end
13
+ def min_input_quality
14
+ (@min_input_quality && @min_input_quality > 0) ? @min_input_quality : 40
15
+ end
17
16
 
18
- def min_result_quality
19
- (@min_result_quality && @min_result_quality > 0) ? @min_result_quality : 80
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
- module Woefoo
2
- class GeoplanetAdjacency < ActiveRecord::Base
3
- set_table_name "geoplanet_adjacencies"
4
- belongs_to :place, :class_name => 'GeoplanetPlace', :foreign_key => 'woeid', :primary_key => 'woeid'
5
- belongs_to :adjacent_place, :class_name => 'GeoplanetPlace', :foreign_key => 'neighbour_woeid', :primary_key => 'woeid'
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
- module Woefoo
2
- class GeoplanetAlias < ActiveRecord::Base
3
- set_table_name "geoplanet_aliases"
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
- module Woefoo
2
- class GeoplanetPlace < ActiveRecord::Base
3
- set_table_name "geoplanet_places"
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
- has_many :aliases, :class_name => 'GeoplanetAlias', :foreign_key => 'woeid'
7
- has_many :adjacencies, :class_name => 'GeoplanetAdjacency', :foreign_key => 'woeid'
8
- has_many :adjacent_places, :through => :adjacencies
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
- belongs_to :parent,
11
- :class_name => "GeoplanetPlace",
12
- :foreign_key => 'parent_woeid',
13
- :primary_key => 'woeid'
9
+ belongs_to :parent,
10
+ :class_name => "GeoplanetPlace",
11
+ :foreign_key => 'parent_woeid',
12
+ :primary_key => 'woeid'
14
13
 
15
- has_ancestry
14
+ has_ancestry
16
15
 
17
- def town
18
- self.ancestors.find(:first, :conditions => {:place_type => "Town"})
19
- end
16
+ def town
17
+ self.ancestors.find(:first, :conditions => {:place_type => "Town"})
18
+ end
20
19
 
21
- def region
22
- self.ancestors.find(:first, :conditions => {:place_type => "State"})
23
- end
20
+ def region
21
+ self.ancestors.find(:first, :conditions => {:place_type => "State"})
22
+ end
24
23
 
25
- def country
26
- self.ancestors.find(:first, :conditions => {:place_type => "Country"})
27
- end
24
+ def country
25
+ self.ancestors.find(:first, :conditions => {:place_type => "Country"})
26
+ end
28
27
 
29
- def continent
30
- self.ancestors.find(:first, :conditions => {:place_type => "Continent"})
31
- end
28
+ def continent
29
+ self.ancestors.find(:first, :conditions => {:place_type => "Continent"})
30
+ end
32
31
 
33
- def full_name
34
- if !@full_name
35
- @full_name = self.name
36
- @full_name = "#{@full_name}, #{self.region.name}" if self.region
37
- @full_name = "#{@full_name}, #{self.country.name}" if self.country
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
- def full_name_parts
44
- ret = {:name => self.name}
45
- ret[:region_name] = self.region.name if self.region
46
- ret[:country_name] = self.country.name if self.country
47
- ret[:continent_name] = self.continent.name if self.continent
48
- return ret
49
- end
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
- def self.build_ancestry_from_parent_ids! parent_id = nil, ancestry = nil
52
- parent_id = parent_id || 0
53
- self.base_class.all(:conditions => {:parent_woeid => parent_id}).each do |node|
54
- node.without_ancestry_callbacks do
55
- node.update_attribute ancestry_column, ancestry
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
- module TownMatchValidators
8
- extend self
6
+ module Woefoo::TownMatchValidators
7
+ extend self
9
8
 
10
- def default_validators
11
- [{
12
- :conditions => Proc.new {|response|
13
- true # If all else fails, use this catch all validator
14
- },
15
- :validator => Proc.new {|response|
16
- best_match=nil
17
- if response.input_quality >= min_input_quality
18
- response.results.each do |r|
19
- if r[:quality] >= response.input_quality && r[:quality] >= min_result_quality
20
- place=Woefoo::GeoplanetPlace.find_by_woeid(r[:woeid])
21
- if place.place_type=="Town"
22
- # If the query included a country code, make sure the place we're picking is in the same country!
23
- next if (response.query_options.include?(:country_code) &&
24
- response.query_options[:country_code].downcase != place.country_code.downcase)
25
- best_match=r[:woeid]
26
- elsif ["Zip", "Suburb"].include?(place.place_type) && place.town
27
- next if (response.query_options.include?(:country_code) &&
28
- response.query_options[:country_code].downcase != place.country_code.downcase)
29
- best_match=place.town.woeid
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
- best_match
36
- }
37
- }]
38
- end
33
+ end
34
+ best_match
35
+ }
36
+ }]
39
37
  end
40
38
  end
@@ -1,3 +1,3 @@
1
1
  module Woefoo
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
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
- def lookup(opts={})
17
+
18
+ def lookup_town_woeid(opts={})
18
19
  validate!(opts)
19
- perform_lookup(opts)
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
- def build_query(opts={})
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
- query = build_query(opts)
55
- url = "http://where.yahooapis.com/geocode?appid=#{@appid}&count=#{placefinder_result_count}&q=#{query}"
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.lookup(COPTER_WORLD_HQ)
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.lookup
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.lookup({:query => ""})
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
- should "use default placefinder count if none is defined" do
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: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
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-05-25 00:00:00 -04:00
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.5.2
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