Class: GeoPack::GeoNames

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/geo_pack/geo_names.rb

Constant Summary collapse

GEONAMES_API =
{
  'findNearbyPlaceName' => [:lat, :lng],
  'postalCodeSearch' => [:postalcode]
}

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/geo_pack/geo_names.rb', line 23

def method_missing(method, *args)
  if GEONAMES_API.has_key?( method )
    query(method, *args)
  else
    super
  end
end

Class Method Details

.query(options = {}) ⇒ Object



31
32
33
34
# File 'lib/geo_pack/geo_names.rb', line 31

def self.query(options= {})
  geonames = self.new(options[:apikey])
  return geonames.query(options)
end

Instance Method Details

#check_requirements(options) ⇒ Object



110
111
112
113
114
# File 'lib/geo_pack/geo_names.rb', line 110

def check_requirements(options)
  #raise( 'apikey required for geonames' ) if @apikey.nil?
  raise( 'method required for geonames' ) if options[:method].nil?
  raise( "method #{options[:method]} geonames" ) unless GEONAMES_API.has_key?( options[:method] )
end

#munge_key_names(options = {}) ⇒ Object

so we can use prettier options than geonames provides



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/geo_pack/geo_names.rb', line 53

def munge_key_names(options={})
  options_mapping = {
    'latitude' => 'lat',
    'longitude' => 'lng',
    'long' => 'lng',
    'zip' => 'postalcode',
    'zipcode' => 'postalcode',
    'zip_code' => 'postalcode',
    'postalCode' => 'postalcode'}


    options.map do |key,value|
      if options_mapping.has_key? key.to_s
        options[options_mapping[key.to_s]] = options.delete key
      end
    end

    options[:countryBias] = 'US' if options[:method] == 'postalCodeSearch'

    return options
end

#munge_results(results) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/geo_pack/geo_names.rb', line 75

def munge_results(results)
  results = results["geonames"] if results.kind_of?(Hash) && results.has_key?("geonames")
  results = results["postalCodes"] if results.kind_of?(Hash) && results.has_key?("postalCodes")
  return [] if results.nil?

  return munge_single_result(results) if results.kind_of?(Hash)

  results_array = []
  results.each {|result| results_array<<munge_single_result(result)}

  return results_array
end

#munge_single_result(results) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/geo_pack/geo_names.rb', line 88

def munge_single_result(results)
  mapping = {
    'latitude' => 'lat',
    'longitude' => 'lng',
    'long' => 'lng',
    'zip' => 'postalcode',
    'zipcode' => 'postalcode',
    'neighborhoodId' => 'neighborhood_id',
    'adminCode1' => 'state',
    'name' => 'city',
    'geonameId' => 'id',
    'placeName' => 'name'}

    results.map do |key,value|
      if mapping.has_key? key.to_s
        results[mapping[key]] = results[ key ]
      end
    end

    return results
end

#query(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/geo_pack/geo_names.rb', line 37

def query(options={})
  check_requirements(options)
  options.merge!({:style => 'full'}) unless options.has_key? :style
  options = munge_key_names(options)
  method = options.delete(:method)
  results = munge_results( GeoNames.get("http://ws.geonames.org/" + method + "JSON", :query => options) )

  if results.kind_of?(Hash) && results.has_key?('status')
    #throw exception here because there was an error
    return[]
  else
    return results
  end
end