Module: Woefoo
- Extended by:
- Woefoo
- Includes:
- Config, TownMatchValidators
- Included in:
- Woefoo
- Defined in:
- lib/woefoo.rb,
lib/woefoo/config.rb,
lib/woefoo/version.rb,
lib/woefoo/response.rb
Defined Under Namespace
Modules: Config, TownMatchValidators Classes: GeoplanetAdjacency, GeoplanetAlias, GeoplanetPlace, Response
Constant Summary collapse
- VERSION =
"1.0.0"
Instance Attribute Summary
Attributes included from Config
#appid, #min_input_quality, #min_result_quality, #placefinder_result_count
Instance Method Summary collapse
- #build_geo_url(opts = {}) ⇒ Object
-
#build_parameterized_url(opts = {}) ⇒ Object
Build a parameterized query url.
- #build_query_keys(opts = {}) ⇒ Object
-
#build_query_url(opts = {}) ⇒ Object
Build a single query url.
-
#canonical_town_match(response, input_opts = {}) ⇒ Object
Takes a Woefoo Response, and figures out a Town-level or equivalent woe id that identifies where the queried address is.
-
#lookup_town_woeid(opts = {}) ⇒ Object
Fires a placefinder request to yahoo, returns with suggested places that match the query.
- #lookup_town_woeid_by_geo(opts = {}) ⇒ Object
- #perform_geo_lookup(opts) ⇒ Object
- #perform_lookup(opts) ⇒ Object
- #perform_parameterized_lookup(opts) ⇒ Object
- #validate!(opts) ⇒ Object
Methods included from TownMatchValidators
#close_enough_to_input_lat_lng?, #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
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/woefoo.rb', line 78 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) unless ["US", "CA", "GB", "UK"].include? opts[:country] k.delete(:state) k.delete(:postal) end end k end |
#build_query_url(opts = {}) ⇒ Object
Build a single query url
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/woefoo.rb', line 64 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, input_opts = {}) ⇒ Object
Takes a Woefoo Response, and figures out a Town-level or equivalent woe id that identifies where the queried address is
55 56 57 58 59 60 61 |
# File 'lib/woefoo.rb', line 55 def canonical_town_match(response, input_opts = {}) default_validators.each do |v| if v[:conditions].call(response, input_opts) return v[:validator].call(response, input_opts) end end end |
#lookup_town_woeid(opts = {}) ⇒ Object
Fires a placefinder request to yahoo, returns with suggested places that match the query
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/woefoo.rb', line 22 def lookup_town_woeid(opts={}) validate!(opts) results = {:woeid=>nil} # First try the parameterized query, if the user bothered to separate out the fields if opts.length > 1 results[:parameterized_response] = perform_parameterized_lookup(opts) results[:woeid] = canonical_town_match(results[:parameterized_response], opts) end if !results[:woeid] results[:query_response] = perform_lookup(opts) results[:woeid] = canonical_town_match(results[:query_response], opts) end results end |
#lookup_town_woeid_by_geo(opts = {}) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/woefoo.rb', line 38 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], opts) results end |
#perform_geo_lookup(opts) ⇒ Object
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 |
#perform_lookup(opts) ⇒ Object
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 |
#perform_parameterized_lookup(opts) ⇒ Object
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
45 46 47 48 49 50 |
# File 'lib/woefoo.rb', line 45 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 |