module WeatherInPoland attr_reader :city attr_reader :code attr_reader :image attr_reader :thumb def self.find_city(name='Gdynia') agent = Mechanize.new page = agent.get("http://weather.yahoo.com/poland/") form = page.form('pform') form.fields.find {|f| f.name == 'location'}.value = name page = agent.submit(form) url = page.uri.to_s near_city = page.parser.xpath('//div[@id="yw-nearstation"]/text()').to_s if near_city.empty? return false else @city = near_city.split(':').last.split(',').first.strip @code = url.split('-').last.to_i return true end end def self.get_weather(code) client = YahooWeather::Client.new response = client.lookup_by_woeid(code, YahooWeather::Units::CELSIUS) return response end def self.get_image(dane) sun_rise = dane.astronomy.sunrise sun_set = dane.astronomy.sunset if sun_rise.past? && sun_set.future? time_of_day = 'd'; else time_of_day = 'n'; end @thumb = "http://l.yimg.com/a/i/us/nws/weather/gr/#{dane.condition.code.to_s+time_of_day}s.png" @image = "http://l.yimg.com/a/i/us/nws/weather/gr/#{dane.condition.code.to_s+time_of_day}.png" end end