Module: Geocoder

Extended by:
Geocoder
Included in:
Geocoder
Defined in:
lib/geocoder.rb,
lib/geocoder/sql.rb,
lib/geocoder/cli.rb,
lib/geocoder/cache.rb,
lib/geocoder/query.rb,
lib/geocoder/lookup.rb,
lib/geocoder/version.rb,
lib/geocoder/request.rb,
lib/geocoder/railtie.rb,
lib/geocoder/exceptions.rb,
lib/geocoder/stores/base.rb,
lib/geocoder/models/base.rb,
lib/geocoder/results/test.rb,
lib/geocoder/lookups/test.rb,
lib/geocoder/results/base.rb,
lib/geocoder/lookups/base.rb,
lib/geocoder/calculations.rb,
lib/geocoder/configuration.rb,
lib/geocoder/models/mongoid.rb,
lib/geocoder/models/mongo_base.rb,
lib/geocoder/configuration_hash.rb,
lib/geocoder/models/mongo_mapper.rb,
lib/geocoder/models/active_record.rb,
lib/generators/geocoder/config/config_generator.rb

Defined Under Namespace

Modules: Calculations, Lookup, Model, Request, Result, Sql, Store Classes: Cache, Cli, ConfigGenerator, Configuration, ConfigurationError, ConfigurationHash, Error, InvalidApiKey, InvalidRequest, OverQueryLimitError, Query, Railtie, RequestDenied

Constant Summary

VERSION =
"1.1.8"

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) config

Read-only access to the singleton's config data.



31
32
33
# File 'lib/geocoder/configuration.rb', line 31

def self.config
  Configuration.instance.data
end

+ (Object) config_for_lookup(lookup_name)

Read-only access to lookup-specific config data.



38
39
40
41
42
43
44
45
# File 'lib/geocoder/configuration.rb', line 38

def self.config_for_lookup(lookup_name)
  data = config.clone
  data.reject!{ |key,value| !Configuration::OPTIONS.include?(key) }
  if config.has_key?(lookup_name)
    data.merge!(config[lookup_name])
  end
  data
end

+ (Object) configure(options = nil, &block)

Configuration options should be set by passing a hash:

Geocoder.configure(
  :timeout  => 5,
  :lookup   => :yandex,
  :api_key  => "2a9fsa983jaslfj982fjasd",
  :units    => :km
)


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/geocoder/configuration.rb', line 16

def self.configure(options = nil, &block)
  if block_given?
    warn "WARNING: Passing a block to Geocoder.configure is DEPRECATED. Please pass a hash instead (eg: Geocoder.configure(:units => ..., :api_key => ...))."
    block.call(Configuration.instance)
  elsif !options.nil?
    Configuration.instance.configure(options)
  else
    warn "WARNING: Use of Geocoder.configure to read or write single config options is DEPRECATED. To write to the config please pass a hash (eg: Geocoder.configure(:units => ...)). To read config options please use the Geocoder.config object (eg: Geocoder.config.units)."
    Configuration.instance
  end
end

Instance Method Details

- (Object) address(query, options = {})

Look up the address of the given coordinates ([lat,lon]) or IP address (string).



36
37
38
39
40
# File 'lib/geocoder.rb', line 36

def address(query, options = {})
  if (results = search(query, options)).size > 0
    results.first.address
  end
end

- (Object) cache

The working Cache object, or nil if none configured.



45
46
47
48
# File 'lib/geocoder.rb', line 45

def cache
  warn "WARNING: Calling Geocoder.cache is DEPRECATED. The #cache method now belongs to the Geocoder::Lookup object."
  Geocoder::Lookup.get(Geocoder.config.lookup).cache
end

- (Object) coordinates(address, options = {})

Look up the coordinates of the given street or IP address.



26
27
28
29
30
# File 'lib/geocoder.rb', line 26

def coordinates(address, options = {})
  if (results = search(address, options)).size > 0
    results.first.coordinates
  end
end

- (Object) search(query, options = {})

Search for information about an address or a set of coordinates.



18
19
20
21
# File 'lib/geocoder.rb', line 18

def search(query, options = {})
  query = Geocoder::Query.new(query, options) unless query.is_a?(Geocoder::Query)
  query.blank? ? [] : query.execute
end