Module: Geogov

Extended by:
Geogov
Included in:
Geogov
Defined in:
lib/geogov.rb,
lib/geogov/utils.rb,
lib/geogov/version.rb,
lib/geogov/geo_stack.rb,
lib/geogov/fuzzy_point.rb,
lib/geogov/providers/mapit.rb,
lib/geogov/providers/google.rb,
lib/geogov/providers/hostip.rb,
lib/geogov/providers/geonames.rb,
lib/geogov/providers/open_street_map.rb,
lib/geogov/providers/dracos_gazetteer.rb

Defined Under Namespace

Classes: DracosGazetteer, FuzzyPoint, GeoStack, Geonames, Google, Hostip, LruCache, Mapit, OpenStreetMap, SimpleCache, UnrecognizedLocationError

Constant Summary collapse

VERSION =
'0.0.12'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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

Yields:

  • (_self)

Yield Parameters:

  • _self (Geogov)

    the object that the method was called on



37
38
39
# File 'lib/geogov.rb', line 37

def self.configure
  yield self
end

.provider_for(method, instance) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/geogov.rb', line 18

def self.provider_for(method, instance)
  unless instance.respond_to?(method)
    raise ArgumentError.new("#{instance.class} doesn't respond to #{method}")
  end

  # caching_instance = SimpleCache.new(instance)
  @@methods ||= {}
  @@methods[method] = instance

  unless self.methods().include?(method)
    dispatcher = <<-EOS
      def #{method}(*args, &block)
        @@methods[:#{method}].__send__(#{method.inspect}, *args, &block)
      end
    EOS
    module_eval(dispatcher)
  end
end

Instance Method Details

#get(url) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/geogov/utils.rb', line 6

def get(url)
  url = URI.parse(url) unless url.is_a? URI
  response = Net::HTTP.start(url.host, url.port) { |http|
    request = Net::HTTP::Get.new(url.request_uri)
    http.request(request)
  }
  return nil if response.code != '200'
  return response.body
end

#get_json(url) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/geogov/utils.rb', line 16

def get_json(url)
  response = self.get(url)
  if response
    return JSON.parse(response)
  else
    return nil
  end
end

#hash_to_params(hash) ⇒ Object



25
26
27
# File 'lib/geogov/utils.rb', line 25

def hash_to_params(hash)
  hash.map { |k,v| CGI.escape(k.to_s) + "=" + CGI.escape(v.to_s) }.join("&")
end