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 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.

Returns:

  • (Boolean)


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

def blank?
  !params_given? and (
    (text.is_a?(Array) and text.compact.size < 2) or
    text.to_s.match(/\A\s*\z/)
  )
end

#coordinatesObject

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



90
91
92
# File 'lib/geocoder/query.rb', line 90

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

#coordinates?Boolean

Does the given string look like latitude/longitude coordinates?

Returns:

  • (Boolean)


79
80
81
82
83
84
# File 'lib/geocoder/query.rb', line 79

def coordinates?
  text.is_a?(Array) or (
    text.is_a?(String) and
    !!text.to_s.match(/\A-?[0-9\.]+, *-?[0-9\.]+\z/)
  )
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)


65
66
67
# File 'lib/geocoder/query.rb', line 65

def ip_address?
  IpAddress.new(text).valid? rescue false
end

#languageObject



101
102
103
# File 'lib/geocoder/query.rb', line 101

def language
  options[:language]
end

#lookupObject

Get a Lookup object (which communicates with the remote geocoding API) appropriate to the Query text.



34
35
36
37
38
39
40
41
# File 'lib/geocoder/query.rb', line 34

def lookup
  if 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

#loopback_ip_address?Boolean

Is the Query text a loopback IP address?

Returns:

  • (Boolean)


72
73
74
# File 'lib/geocoder/query.rb', line 72

def loopback_ip_address?
  ip_address? && IpAddress.new(text).loopback?
end

#reverse_geocode?Boolean

Should reverse geocoding be performed for this query?

Returns:

  • (Boolean)


97
98
99
# File 'lib/geocoder/query.rb', line 97

def reverse_geocode?
  coordinates?
end

#sanitized_textObject



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_sObject



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

def to_s
  text
end

#urlObject



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

def url
  lookup.query_url(self)
end