lib/geonames_api/country.rb in geonames_api-0.0.1 vs lib/geonames_api/country.rb in geonames_api-0.0.2
- old
+ new
@@ -1,47 +1,16 @@
module GeoNamesAPI
- class Country
+ class Country < GeoNamesAPI::Object
METHOD = "countryInfoJSON"
+ ID = ["country"]
def self.all
- JSON.load(open(url).read)["geonames"].collect do |response|
- new.parse(response)
- end
+ where.collect { |response| new(response) }
end
-
- attr_accessor :country_code
-
- def initialize(country_code=nil)
- self.country_code = country_code
- end
-
- def find
- parse(JSON.load(open(url).read)["geonames"].first)
- self
- end
-
- def parse(response)
- response.each do |key, value|
- attr_name = key.underscore.to_sym
- self.class.send(:attr_accessor, attr_name) unless respond_to? attr_name
- value = GeoNamesAPI.set_default_type(value)
- send("#{attr_name}=", value)
- end
- self
- end
-
- def params
- GeoNamesAPI.params.merge({ country: country_code })
- end
-
- def url
- self.class.url(params)
- end
- def self.url(params={})
- params = GeoNamesAPI.params.merge(params)
- GeoNamesAPI::BASE_URL + METHOD + GeoNamesAPI.to_url_params(params)
+ def self.where(params={})
+ JSON.load(open(url(params)).read)["geonames"]
end
-
+
end
end
\ No newline at end of file