Class: Geocoder::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/geocoder/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  self.text = text
  self.options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/geocoder/query.rb', line 3

def options
  @options
end

#textObject

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 text blank? (ie, should we not bother searching?)

Returns:

  • (Boolean)


42
43
44
# File 'lib/geocoder/query.rb', line 42

def blank?
  !!text.to_s.match(/^\s*$/)
end

#coordinatesObject

Return the latitude/longitude coordinates specified in the query, or nil if none.



77
78
79
# File 'lib/geocoder/query.rb', line 77

def coordinates
  sanitized_text.split(',') if coordinates?
end

#coordinates?Boolean

Does the given string look like latitude/longitude coordinates?

Returns:

  • (Boolean)


66
67
68
69
70
71
# File 'lib/geocoder/query.rb', line 66

def coordinates?
  text.is_a?(Array) or (
    text.is_a?(String) and
    !!text.to_s.match(/^-?[0-9\.]+, *-?[0-9\.]+$/)
  )
end

#executeObject



10
11
12
# File 'lib/geocoder/query.rb', line 10

def execute
  lookup.search(text, options)
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.

Returns:

  • (Boolean)


52
53
54
# File 'lib/geocoder/query.rb', line 52

def ip_address?
  !!text.to_s.match(/^(::ffff:)?(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
end

#lookupObject

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?

Returns:

  • (Boolean)


59
60
61
# File 'lib/geocoder/query.rb', line 59

def loopback_ip_address?
  !!(text == "0.0.0.0" or text.to_s.match(/^127/))
end

#reverse_geocode?Boolean

Should reverse geocoding be performed for this query?

Returns:

  • (Boolean)


84
85
86
# File 'lib/geocoder/query.rb', line 84

def reverse_geocode?
  coordinates?
end

#sanitized_textObject



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_sObject



14
15
16
# File 'lib/geocoder/query.rb', line 14

def to_s
  text
end