Last active
November 25, 2018 19:19
-
-
Save inem/859c73486ae860b255dcbc0c7e1b84a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'uri' | |
require 'rexml/document' | |
require 'net/http' | |
require_relative 'cloth' | |
require_relative 'smart_wardrobe' | |
class MeteoService | |
CITIES = {'Москва' => '37', 'Рига' => '312', 'ГонКонг' => '256', 'Женева' => '374', 'Вашингтон' => '384', 'Каир' => '334'} | |
def initialize(url = "https://xml.meteoservice.ru/export/gismeteo/point") | |
@url = url | |
end | |
def get_xml(city_name) | |
code = CITIES[city_name] | |
uri = URI.parse("#{@url}/#{code}.xml") | |
response = Net::HTTP.get_response(uri) | |
response.body | |
end | |
end | |
class MeteoData | |
def self.from_xml(xml) | |
doc = REXML::Document.new(xml) | |
city_name = URI.unescape(doc.root.elements['REPORT/TOWN'].attributes['sname']) | |
element = doc.root.elements['REPORT/TOWN'].elements.to_a[0] | |
min_temp = element.elements['TEMPERATURE'].attributes['min'].to_i | |
new(city_name, min_temp) | |
end | |
attr_reader :city_name, :min_temp | |
def initialize(city_name, min_temp) | |
@city_name = city_name | |
@min_temp = min_temp | |
end | |
def to_s | |
"#{@city_name}: #{@min_temp}" | |
end | |
end | |
current_path = File.dirname(__FILE__) | |
files_list = Dir.glob("#{current_path}/../data/*.txt") | |
smart_wardrobe = SmartWardrobe.new(files_list) | |
xml = MeteoService.new.get_xml('Москва') | |
meteo_data = MeteoData.from_xml(xml) | |
puts meteo_data | |
advised_clothes = smart_wardrobe.advise(meteo_data.min_temp) | |
advised_clothes.each { |type, cloth| puts cloth.sample } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require 'refactoring' | |
describe 'Refactoring' do | |
describe 'MeteoData' do | |
it 'parses xml correctly' do | |
xml = IO.read("spec/fixtures/37.xml") | |
meteo_data = MeteoData.from_xml(xml) | |
expect(meteo_data.min_temp).to eq(-5) | |
expect(meteo_data.city_name).to eq('Москва') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment