Sha256: 44610bbfcbd69437e2a2a0651515ed83b68fbfad87278d38fe98b274dc4c23eb

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

class WeatherInPoland
  attr_reader :city
  attr_reader :code
  attr_reader :image
  attr_reader :thumb

  def 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 get_weather(code)
    client = YahooWeather::Client.new
    response = client.lookup_by_woeid(code, YahooWeather::Units::CELSIUS)
    return response
  end

  def 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

2 entries across 2 versions & 1 rubygems

Version Path
weather_in_poland-0.0.4 lib/weather_in_poland.rb
weather_in_poland-0.0.3 lib/weather_in_poland.rb