Module: Woefoo
Defined Under Namespace
Modules: Config, TownMatchValidators
Classes: GeoplanetAdjacency, GeoplanetAlias, GeoplanetPlace, Response
Constant Summary
collapse
- VERSION =
"0.0.14"
Instance Attribute Summary
Attributes included from Config
#appid, #min_input_quality, #min_result_quality, #placefinder_result_count
Instance Method Summary
collapse
#default_validators
Instance Method Details
#build_geo_url(opts = {}) ⇒ Object
89
90
91
|
# File 'lib/woefoo.rb', line 89
def build_geo_url(opts={})
"http://where.yahooapis.com/geocode?appid=#{@appid}&count=#{placefinder_result_count}&gflags=R&q=#{opts[:lat]},+#{opts[:lng]}"
end
|
#build_parameterized_url(opts = {}) ⇒ Object
Build a parameterized query url
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/woefoo.rb', line 77
def build_parameterized_url(opts={})
s = ""
build_query_keys(opts).each do |l|
if opts.include?(l)
val = ::CGI.escape(opts[l])
s << "&#{l.to_s}=#{val}"
end
end
"http://where.yahooapis.com/geocode?appid=#{@appid}&count=#{placefinder_result_count}#{s}"
end
|
#build_query_keys(opts = {}) ⇒ Object
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/woefoo.rb', line 111
def build_query_keys(opts={})
k = [:line1, :city, :state, :postal, :country]
if opts.include?(:country)
if opts[:country] != "US" && opts[:country] != "CA"
k.delete(:state)
k.delete(:postal)
end
end
k
end
|
#build_query_url(opts = {}) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/woefoo.rb', line 63
def build_query_url(opts={})
s = ""
if opts.include?(:query)
s = opts[:query]
else
[:line1, :city, :state, :postal, :country].each do |l|
s << "#{opts[l]} " if opts.include?(l)
end
end
s = ::CGI.escape(s)
"http://where.yahooapis.com/geocode?appid=#{@appid}&count=#{placefinder_result_count}&q=#{s}"
end
|
#canonical_town_match(response) ⇒ Object
Takes a Woefoo Response, and figures out a Town-level or equivalent woe id that identifies where the queried address is
53
54
55
56
57
58
59
|
# File 'lib/woefoo.rb', line 53
def canonical_town_match(response)
default_validators.each do |v|
if v[:conditions].call(response)
return v[:validator].call(response)
end
end
end
|
#lookup_town_woeid(opts = {}) ⇒ Object
Fires a placefinder request to yahoo, returns with suggested places that match the query
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/woefoo.rb', line 18
def lookup_town_woeid(opts={})
validate!(opts)
results = {:woeid=>nil}
if opts.length > 1
results[:parameterized_response] = perform_parameterized_lookup(opts)
results[:woeid] = canonical_town_match(results[:parameterized_response])
end
if !results[:woeid]
results[:query_response] = perform_lookup(opts)
results[:woeid] = canonical_town_match(results[:query_response])
end
results
end
|
#lookup_town_woeid_by_geo(opts = {}) ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/woefoo.rb', line 35
def lookup_town_woeid_by_geo(opts={})
results = {:woeid=>nil}
results[:geo_response] = perform_geo_lookup(opts)
results[:woeid] = canonical_town_match(results[:geo_response])
results
end
|
105
106
107
108
109
|
# File 'lib/woefoo.rb', line 105
def perform_geo_lookup(opts)
url = build_geo_url(opts)
tr = Typhoeus::Request.get(url)
Woefoo::Response.new(opts, tr) if tr
end
|
93
94
95
96
97
|
# File 'lib/woefoo.rb', line 93
def perform_lookup(opts)
url = build_query_url(opts)
tr = Typhoeus::Request.get(url)
Woefoo::Response.new(opts, tr) if tr
end
|
99
100
101
102
103
|
# File 'lib/woefoo.rb', line 99
def perform_parameterized_lookup(opts)
url = build_parameterized_url(opts)
tr = Typhoeus::Request.get(url)
Woefoo::Response.new(opts, tr) if tr
end
|
#validate!(opts) ⇒ Object
43
44
45
46
47
48
|
# File 'lib/woefoo.rb', line 43
def validate!(opts)
raise "Woefoo.appid is not defined" if !@appid
if opts.length == 0 || opts.include?(:query) && (opts[:query] == "")
raise "Invalid query"
end
end
|