Sha256: 302a9ddd4e66d9ef593a6c146a25f492d23ce4b3bf8c8323841a686fb0c10b8a

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

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?
        text.split(/\s*,\s*/).join(',')
      else
        text
      end
    end

    ##
    # Get a Lookup object (which communicates with the remote geocoding API)
    # appropriate to the Query text.
    #
    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

    ##
    # Is the Query text blank? (ie, should we not bother searching?)
    #
    def blank?
      # check whether both coordinates given
      if text.is_a?(Array)
        text.compact.size < 2
      # else assume a string
      else
        !!text.to_s.match(/^\s*$/)
      end
    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?
      !!text.to_s.match(/^(::ffff:)?(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
    end

    ##
    # Is the Query text a loopback IP address?
    #
    def loopback_ip_address?
      !!(text == "0.0.0.0" or text.to_s.match(/^127/))
    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(/^-?[0-9\.]+, *-?[0-9\.]+$/)
      )
    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
geocoder-1.1.5 lib/geocoder/query.rb