Class: Terragona::GeoNames::API
Constant Summary collapse
- URL =
'http://api.geonames.org/searchJSON'
Instance Method Summary collapse
- #fetch_geonames(name, country, admin_code_type, admin_code) ⇒ Object
-
#initialize(args = {}) ⇒ API
constructor
A new instance of API.
- #request(name_str, country, admin_code_str) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(args = {}) ⇒ API
Returns a new instance of API.
77 78 79 80 81 82 83 |
# File 'lib/terragona/geonames.rb', line 77 def initialize(args = {}) super @username = args[:geonames_username] cache_expiration_time = args[:cache_expiration_time] || 7200 @cache = Diskcached.new('/tmp/cache',cache_expiration_time,true) @use_cache = args[:use_cache] end |
Instance Method Details
#fetch_geonames(name, country, admin_code_type, admin_code) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/terragona/geonames.rb', line 85 def fetch_geonames(name,country,admin_code_type,admin_code) admin_code_str = admin_code ? "&#{admin_code_type}=#{admin_code}" : '' name_str = name ? "q=#{name}&" : '' if @use_cache @cache.cache("geonames_name=#{name}&country=#{country}#{admin_code_str}&full666") do request(name_str,country,admin_code_str) end else request(name_str,country,admin_code_str) end end |
#request(name_str, country, admin_code_str) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/terragona/geonames.rb', line 98 def request(name_str,country,admin_code_str) url = URI.escape(%Q{#{URL}?#{name_str}country=#{country}#{admin_code_str}&style=FULL&order_by=relevance&maxRows=1000&username=#{@username}}) request = HTTPI::Request.new(url) data = HTTPI.get(request) JSON.parse(data.body,:symbolize_names=>true)[:geonames] end |