class Pyaru

Public Instance Methods

generate_header(city, temperature) click to toggle source
# File lib/pyaru.rb, line 94
def generate_header(city, temperature)
  header = city.colorize(:light_red)
  header << ', '
  header << temperature.colorize(temp_color_selector(temperature.to_i))

  header_shift = ((14 * 5) - header.uncolorize.size) / 2

  "#{' ' * header_shift}#{header}"
end
get_current_temperature(page) click to toggle source
# File lib/pyaru.rb, line 90
def get_current_temperature(page)
  page.css('p[class=temperature-wrapper] span[class=temperature]')[0]['data-t']
end
get_default_city_name() click to toggle source
# File lib/pyaru.rb, line 60
def get_default_city_name
  # Request for http://p.ya.ru/ returns redirect to the city of your current location.
  Net::HTTP.get_response('p.ya.ru', '/')['Location'].gsub(%r{^.*/([^/]+)$}, '\1')
end
get_forecast_data(page) click to toggle source
# File lib/pyaru.rb, line 104
def get_forecast_data(page)
  t_list = page.css('div[class=temperatures] div[class=chart_wrapper]')

  t_list.map do |point|
    [
      point.css('p[class=th]').text.to_i,                  # hour
      point.css('div[class=chart]')[0]['data-t'].to_i,     # temperature
      point.css('span[class=icon_rain]')[0] ? true : false # rain status
    ]
  end
end
get_page_city_name(page) click to toggle source
# File lib/pyaru.rb, line 86
def get_page_city_name(page)
  page.css('footer div[class=bigweather] a')[0]['href'].gsub(%r{^.*/([^/]+)/$}, '\1')
end
help() click to toggle source
# File lib/pyaru.rb, line 46
  def help
    puts %q(Yandex weather (p.ya.ru) page crawler.

Delivers nice and clean weather info using p.ya.ru website right to your
console. Now a nice weather forecast is right under your fingertips.

Author: Kirill Kouznetsov <agon.smith@gmail.com>

This software is made just for fun so use it on your own risk.
    )

    exit 0
  end
open_weather_page(city) click to toggle source
# File lib/pyaru.rb, line 82
def open_weather_page(city)
  Nokogiri::HTML(open("http://p.ya.ru/#{URI::encode(city)}"))
end
parse_options() click to toggle source
# File lib/pyaru.rb, line 65
def parse_options
  if ARGV.size > 1
    STDERR.puts 'Wrong number of arguments.'
    STDERR.puts 'Please provide City name as in http://p.ya.ru/<CITY_NAME> url.'
    STDERR.puts 'Or omit them at all for default selection.'
    STDERR.puts
    exit 1
  elsif ARGV.size == 1
    help if ARGV[0] == '-h' || ARGV[0] == '--help'
    city = ARGV[0]
  else
    city = get_default_city_name
  end

  city.downcase
end
print_forecast_grid(forecast) click to toggle source
run() click to toggle source
# File lib/pyaru.rb, line 140
def run
  city = parse_options
  weather_page = open_weather_page(city)

  resolved_city_name = get_page_city_name(weather_page)
  puts "WARNING: City '#{city}' was not found. Falled back to: '#{resolved_city_name}'\n\n" if resolved_city_name.gsub('-', ' ') != city.gsub('-', ' ')
  city = resolved_city_name.split(' ').map(&:capitalize).join(' ')

  puts generate_header(city, get_current_temperature(weather_page))
  puts

  print_forecast_grid(get_forecast_data(weather_page))
  puts
end
temp_color_selector(temperature = 1) click to toggle source
# File lib/pyaru.rb, line 30
def temp_color_selector(temperature = 1)
  if temperature >= 25
    :red
  elsif temperature < 25 && temperature >= 18
    :light_red
  elsif temperature < 18 && temperature >= 10
    :light_yellow
  elsif temperature < 10 && temperature > 0
    :white
  elsif temperature <= 0 && temperature >= -5
    :light_cyan
  else
    :blue
  end
end