Sha256: d0a2857ada0097f0e1b535691fd3bc54e43f58e4b8182aa604afde2e3193824d

Contents?: true

Size: 838 Bytes

Versions: 1

Compression:

Stored size: 838 Bytes

Contents

module GeoNamesAPI
  class Weather
    METHOD = "findNearByWeatherJSON"
    
    attr_accessor :latitude, :longitude
    
    def initialize(latitude, longitude)
      self.latitude, self.longitude = latitude, longitude
    end
    
    def find
      parse(JSON.load(open(url).read)["weatherObservation"])
      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({ lat: latitude, lng: longitude })
    end
        
    def url
      GeoNamesAPI::BASE_URL + METHOD + GeoNamesAPI.to_url_params(params)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
geonames_api-0.0.1 lib/geonames_api/weather.rb