Class: Geocoder::Query
- Inherits:
-
Object
- Object
- Geocoder::Query
- Defined in:
- lib/geocoder/query.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
-
#blank? ⇒ Boolean
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.
-
#coordinates ⇒ Object
Return the latitude/longitude coordinates specified in the query, or nil if none.
-
#coordinates? ⇒ Boolean
Does the given string look like latitude/longitude coordinates?.
- #execute ⇒ Object
-
#initialize(text, options = {}) ⇒ Query
constructor
A new instance of Query.
-
#internal_ip_address? ⇒ Boolean
Is the Query text a loopback or private IP address?.
-
#ip_address? ⇒ Boolean
Does the Query text look like an IP address?.
- #language ⇒ Object
-
#lookup ⇒ Object
Get a Lookup object (which communicates with the remote geocoding API) appropriate to the Query text.
-
#loopback_ip_address? ⇒ Boolean
Is the Query text a loopback IP address?.
-
#private_ip_address? ⇒ Boolean
Is the Query text a private IP address?.
-
#reverse_geocode? ⇒ Boolean
Should reverse geocoding be performed for this query?.
- #sanitized_text ⇒ Object
- #to_s ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(text, options = {}) ⇒ Query
Returns a new instance of Query.
5 6 7 8 |
# File 'lib/geocoder/query.rb', line 5 def initialize(text, = {}) self.text = text self. = end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/geocoder/query.rb', line 3 def @options end |
#text ⇒ Object
Returns the value of attribute text.
3 4 5 |
# File 'lib/geocoder/query.rb', line 3 def text @text end |
Instance Method Details
#blank? ⇒ Boolean
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.
53 54 55 56 57 58 |
# File 'lib/geocoder/query.rb', line 53 def blank? !params_given? and ( (text.is_a?(Array) and text.compact.size < 2) or text.to_s.match(/\A\s*\z/) ) end |
#coordinates ⇒ Object
Return the latitude/longitude coordinates specified in the query, or nil if none.
105 106 107 |
# File 'lib/geocoder/query.rb', line 105 def coordinates sanitized_text.split(',') if coordinates? end |
#coordinates? ⇒ Boolean
Does the given string look like latitude/longitude coordinates?
94 95 96 97 98 99 |
# File 'lib/geocoder/query.rb', line 94 def coordinates? text.is_a?(Array) or ( text.is_a?(String) and !!text.to_s.match(/\A-?[0-9\.]+, *-?[0-9\.]+\z/) ) end |
#execute ⇒ Object
10 11 12 |
# File 'lib/geocoder/query.rb', line 10 def execute lookup.search(text, ) end |
#internal_ip_address? ⇒ Boolean
Is the Query text a loopback or private IP address?
73 74 75 |
# File 'lib/geocoder/query.rb', line 73 def internal_ip_address? ip_address? && IpAddress.new(text).internal? end |
#ip_address? ⇒ Boolean
Does the Query text look like an IP address?
Does not check for actual validity, just the appearance of four dot-delimited numbers.
66 67 68 |
# File 'lib/geocoder/query.rb', line 66 def ip_address? IpAddress.new(text).valid? rescue false end |
#language ⇒ Object
116 117 118 |
# File 'lib/geocoder/query.rb', line 116 def language [:language] end |
#lookup ⇒ Object
Get a Lookup object (which communicates with the remote geocoding API) appropriate to the Query text.
34 35 36 37 38 39 40 41 42 |
# File 'lib/geocoder/query.rb', line 34 def lookup if ![:streot_address] and ([:ip_address] or ip_address?) name = [:ip_lookup] || Configuration.ip_lookup || Geocoder::Lookup.ip_services.first else name = [:lookup] || Configuration.lookup || Geocoder::Lookup.street_services.first end Lookup.get(name) return end |
#loopback_ip_address? ⇒ Boolean
Is the Query text a loopback IP address?
80 81 82 |
# File 'lib/geocoder/query.rb', line 80 def loopback_ip_address? ip_address? && IpAddress.new(text).loopback? end |
#private_ip_address? ⇒ Boolean
Is the Query text a private IP address?
87 88 89 |
# File 'lib/geocoder/query.rb', line 87 def private_ip_address? ip_address? && IpAddress.new(text).private? end |
#reverse_geocode? ⇒ Boolean
Should reverse geocoding be performed for this query?
112 113 114 |
# File 'lib/geocoder/query.rb', line 112 def reverse_geocode? coordinates? end |
#sanitized_text ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/geocoder/query.rb', line 18 def sanitized_text if coordinates? if text.is_a?(Array) text.join(',') else text.split(/\s*,\s*/).join(',') end else text end end |
#to_s ⇒ Object
14 15 16 |
# File 'lib/geocoder/query.rb', line 14 def to_s text end |
#url ⇒ Object
44 45 46 |
# File 'lib/geocoder/query.rb', line 44 def url lookup.query_url(self) end |