Class: GeeoCode

Inherits:
Object
  • Object
show all
Defined in:
lib/geeo_code.rb,
lib/geeo_code/version.rb

Constant Summary collapse

GOOGLE_ENDPOINT =
"http://maps.googleapis.com/maps/api/geocode/".freeze
VALID_OPTIONS =
[:proxy, :sensor, :format]
VERSION =
"0.0.1"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GeeoCode

Returns a new instance of GeeoCode.



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

def initialize( options={} )
  options.map {|k,v| send("#{k}=".to_sym, v)}
end

Instance Method Details

#configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (GeeoCode)

    the object that the method was called on



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

def configure
  yield self    
end

#reverse(address) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/geeo_code.rb', line 22

def reverse(address)
  begin  
    url = URI.parse("#{GOOGLE_ENDPOINT}#{format}?address=#{CGI::escape(address)}&sensor=#{sensor}")
    content = open(url, :proxy => proxy)
    doc = Yajl::Parser.parse(content, :symbolize_keys => true)

    address = {}
    geometry = {}
    match_type = "none"
    # puts doc

    if doc[:status] == "OK"
      results = doc[:results].first

      data = results[:address_components]
      data.each do |piece|
        address[:street_number] = piece[:short_name] if piece[:types].include? "street_number"
        address[:street_name] = piece[:short_name] if piece[:types].include? "route"
        address[:city] = piece[:short_name] if piece[:types].include? "locality"
        address[:state] = piece[:short_name] if piece[:types].include? "administrative_area_level_1"
        address[:zip_code] = piece[:short_name] if piece[:types].include? "postal_code"
      end

      geometry = results[:geometry]
      lat_lang = results[:geometry][:location]
      match_type = results[:partial_match] ? "partial" : "exact"

    end

    return {:address => address, :geometry => geometry, :match_type => match_type}
  rescue Object => err
    msg = "Error! #{err.message}"
    puts msg 
  end
end