Sha256: 1fd46727284fd2e10d667e95659583fd3c233edaf1a474aea0d0d990abc2ab9b

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
weather_in_poland-0.0.2 lib/weather_in_poland.rb