1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use Error;
use Week;
use Weekday;
use Building;

use reqwest;

#[derive(Debug)]
pub struct Room {
    pub name: String,
}

// The API spits out a list of strings on this request, so we can't deserialize
// the room values directly, but have to convert afterwards.
impl From<String> for Room {
    fn from(string: String) -> Self {
        Room { name: string }
    }
}

const BASE_URL: &'static str = "https://www2.htw-dresden.de/~app/API/GetFreeRooms.php";

impl Room {
    /// Returns a list of free `Rooms`s for a list of given parameters.
    ///
    /// # Arguments
    ///
    /// * `week` - Even or odd week
    /// * `Weekday` - A specified weekday
    /// * `start_time` - Beginning of the search timeframe
    /// * `end_time` - Ending of the search timeframe
    /// * `building` - Which building to search in
    ///
    /// # Example
    ///
    /// ```
    /// use htwdresden::{Week, Weekday, Room, Building};
    ///
    /// let rooms = Room::get_free(Week::Even, Weekday::Monday, "9:30", "10:30", Building::Z);
    /// ```
    pub fn get_free(week: Week,
                    day: Weekday,
                    start_time: &str,
                    end_time: &str,
                    building: Building)
                    -> Result<Vec<Room>, Error> {
        let url = format!("{}?week={}&day={}&startTime={}&endTime={}&building={:?}",
                          BASE_URL,
                          week as u8,
                          day as u8,
                          start_time,
                          end_time,
                          building);
        let rooms = reqwest::get(&url)
            ?
            .json()
            .map(|response: Vec<String>| response)?
            .iter()
            .map(|room| room.to_string().into())
            .collect();

        Ok(rooms)
    }
}