1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
module Geocoder
class Query
attr_accessor :text, :options
def initialize(text, options = {})
self.text = text
self.options = options
end
def execute
lookup.search(text, options)
end
def to_s
text
end
def sanitized_text
if coordinates?
if text.is_a?(Array)
text.join(',')
else
text.split(/\s*,\s*/).join(',')
end
else
text
end
end
##
# Get a Lookup object (which communicates with the remote geocoding API)
# appropriate to the Query text.
#
def lookup
if !options[:street_address] and (options[:ip_address] or ip_address?)
name = options[:ip_lookup] || Configuration.ip_lookup || Geocoder::Lookup.ip_services.first
else
name = options[:lookup] || Configuration.lookup || Geocoder::Lookup.street_services.first
end
Lookup.get(name)
end
def url
lookup.query_url(self)
end
##
# Is the Query blank? (ie, should we not bother searching?)
# A query is considered blank if its text is nil or empty string AND
# no URL parameters are specified.
#
def blank?
!params_given? and (
(text.is_a?(Array) and text.compact.size < 2) or
text.to_s.match(/\A\s*\z/)
)
end
##
# Does the Query text look like an IP address?
#
# Does not check for actual validity, just the appearance of four
# dot-delimited numbers.
#
def ip_address?
IpAddress.new(text).valid? rescue false
end
##
# Is the Query text a loopback or private IP address?
#
def internal_ip_address?
ip_address? && IpAddress.new(text).internal?
end
##
# Is the Query text a loopback IP address?
#
def loopback_ip_address?
ip_address? && IpAddress.new(text).loopback?
end
##
# Is the Query text a private IP address?
#
def private_ip_address?
ip_address? && IpAddress.new(text).private?
end
##
# Does the given string look like latitude/longitude coordinates?
#
def coordinates?
text.is_a?(Array) or (
text.is_a?(String) and
!!text.to_s.match(/\A-?[0-9\.]+, *-?[0-9\.]+\z/)
)
end
##
# Return the latitude/longitude coordinates specified in the query,
# or nil if none.
#
def coordinates
sanitized_text.split(',') if coordinates?
end
##
# Should reverse geocoding be performed for this query?
#
def reverse_geocode?
coordinates?
end
def language
options[:language]
end
private # ----------------------------------------------------------------
def params_given?
!!(options[:params].is_a?(Hash) and options[:params].keys.size > 0)
end
end
end
|