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.
-
#ip_address? ⇒ Boolean
Does the Query text look like an IP address?.
-
#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?.
-
#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.
48 49 50 51 52 53 |
# File 'lib/geocoder/query.rb', line 48 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.
86 87 88 |
# File 'lib/geocoder/query.rb', line 86 def coordinates sanitized_text.split(',') if coordinates? end |
#coordinates? ⇒ Boolean
Does the given string look like latitude/longitude coordinates?
75 76 77 78 79 80 |
# File 'lib/geocoder/query.rb', line 75 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 |
#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.
61 62 63 |
# File 'lib/geocoder/query.rb', line 61 def ip_address? !!text.to_s.match(/\A(::ffff:)?(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\z/) end |
#lookup ⇒ Object
Get a Lookup object (which communicates with the remote geocoding API) appropriate to the Query text.
30 31 32 33 34 35 36 37 |
# File 'lib/geocoder/query.rb', line 30 def lookup if ip_address? name = Configuration.ip_lookup || Geocoder::Lookup.ip_services.first else name = Configuration.lookup || Geocoder::Lookup.street_services.first end Lookup.get(name) end |
#loopback_ip_address? ⇒ Boolean
Is the Query text a loopback IP address?
68 69 70 |
# File 'lib/geocoder/query.rb', line 68 def loopback_ip_address? !!(self.ip_address? and (text == "0.0.0.0" or text.to_s.match(/\A127/))) end |
#reverse_geocode? ⇒ Boolean
Should reverse geocoding be performed for this query?
93 94 95 |
# File 'lib/geocoder/query.rb', line 93 def reverse_geocode? coordinates? end |
#sanitized_text ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/geocoder/query.rb', line 18 def sanitized_text if coordinates? text.split(/\s*,\s*/).join(',') else text end end |
#to_s ⇒ Object
14 15 16 |
# File 'lib/geocoder/query.rb', line 14 def to_s text end |
#url ⇒ Object
39 40 41 |
# File 'lib/geocoder/query.rb', line 39 def url lookup.query_url(self) end |