Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions steps/network-02/solution/meteoservice/data/cities.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Москва" : 37,
"Пермь" : 59,
"Санкт-Петербург" : 69,
"Новосибирск" : 99,
"Орел" : 31584,
"Чита" : 121,
"Братск" : 141,
"Краснодар" : 199
}
40 changes: 18 additions & 22 deletions steps/network-02/solution/meteoservice/meteoservice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,33 @@

require 'net/http'
require 'rexml/document'
require 'json'

require_relative 'lib/meteoservice_forecast'
require_relative 'data/cities.json'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it required?

please inline it for simplicity


# Словарик городов, собранных с сайта Метеосервиса. Можно написать код, который
# будет собирать все города и их названия с сайта амтоматически, но мы пока
# этого делать не будем. При необходимости добавляйте свои города руками.
CITIES = {
37 => 'Москва',
69 => 'Санкт-Петербург',
99 => 'Новосибирск',
59 => 'Пермь',
115 => 'Орел',
121 => 'Чита',
141 => 'Братск',
199 => 'Краснодар'
}.invert.freeze

# Сделаем массив из наваний городов, взяв ключи массива CITIES
city_names = CITIES.keys
# Прочитаем файл с городами и их индексами(https://www.meteoservice.ru/content/export.html)
# И запишем данные в cities
file = File.read("#{__dir__}/data/cities.json", encoding: 'utf-8')
cities = JSON.parse(file)

# Спрашиваем у пользователя, какой город по порядку ему нужен
puts 'Погоду для какого города Вы хотите узнать?'
city_names.each_with_index { |name, index| puts "#{index + 1}: #{name}" }
city_index = gets.to_i
unless city_index.between?(1, city_names.size)
city_index = gets.to_i
puts "Введите число от 1 до #{city_names.size}"
cities.each_with_index do |name,index|
puts "#{index+1}: #{name.first}"
end

city_index = gets.chomp.to_i
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No meed for chomp here

# Пока пользователь не введет число между 1 и 8 (в нашем случае)
# програма будет уведомлять об не правильном вводе и просить ввести число снова
until city_index.between?(1, cities.size)
puts "Неверный выбор!"
puts "Пожалуйста введите число от 1 до #{cities.size}"
city_index = gets.chomp.to_i
end

# Когда, наконец, получим нуный индекс, достаем city_id
city_id = CITIES[city_names[city_index - 1]]
city_id = cities[cities[city_index - 1]]

# Сформировали адрес запроса
url = "http://xml.meteoservice.ru/export/gismeteo/point/#{city_id}.xml"
Expand Down