Clean up lots of duplication on the weather code.

This commit is contained in:
Darren VanBuren 2016-08-18 17:16:58 -07:00
parent 781c2806b7
commit a356b7631d

View file

@ -123,40 +123,21 @@ fn handle_privmsg(target: &String, message_body: &String, message_obj: &Message,
}
fn get_weather(api_key: &String, location_city: &String, location_area: &String) -> String {
let client = Client::new();
let data_type = "conditions";
let url = &format!("{}{}/{}/q/{}/{}.json", WEATHER_API_BASE, api_key, data_type, location_area, location_city);
println!("Attempting to fetch {}", url);
let mut response = match client.get(url).send() {
Ok(response) => response,
Err(_) => panic!("Error fetching JSON!"),
};
let mut response_body = String::new();
match response.read_to_string(&mut response_body) {
Ok(_) => (),
Err(_) => panic!("Error reading to buffer"),
};
let mut reply_message = String::new();
if let Ok(parsed_response) = json::Json::from_str(&response_body) {
if let Some(ref current_observation) = parsed_response.find("current_observation") {
let ref display_location = current_observation.find("display_location").unwrap();
let display_location_string = display_location.find("full").unwrap().as_string().unwrap();
let temperature_string = current_observation.find("temperature_string").unwrap().as_string().unwrap();
let weather = current_observation.find("weather").unwrap().as_string().unwrap();
let wind_string = current_observation.find("wind_string").unwrap().as_string().unwrap();
let humidity = current_observation.find("relative_humidity").unwrap().as_string().unwrap();
let observation_time = current_observation.find("observation_time").unwrap().as_string().unwrap();
reply_message = format!("It is {} and {} in {}. Wind: {}, Humidity: {}, {}",
weather, temperature_string, display_location_string, wind_string, humidity, observation_time);
}
}
reply_message
get_weather_inner(&url)
}
fn get_weather_locid(api_key: &String, locid_type: &String, locid: &String) -> String {
let client = Client::new();
let data_type = "conditions";
let url = &format!("{}{}/{}/q/{}:{}.json", WEATHER_API_BASE, api_key, data_type, locid_type, locid);
get_weather_inner(&url)
}
fn get_weather_inner(url: &String) -> String {
let client = Client::new();
println!("Attempting to fetch {}", url);
let mut response = match client.get(url).send() {
Ok(response) => response,