dinzai-datni 0.1.1

Cryptocurrency ticker library for Rust.
use rustc_serialize::json;
use rustc_serialize::Decodable;
use core;
use core::{Currency, Pair, TickerError};

#[derive(RustcEncodable, RustcDecodable, Debug)]
pub struct Ticker {
    pub last: f32,
    pub bid: f32,
    pub ask: f32,
    pub high: f32,
    pub low: f32,
    pub volume: f32,
    pub timestamp: u64,
}

impl core::Ticker for Ticker {
    fn last(&self) -> f32 {
        self.last
    }
    fn bid(&self) -> f32 {
        self.bid
    }
    fn ask(&self) -> f32 {
        self.ask
    }
}

pub fn ticker(pair: Pair) -> Result<Ticker, TickerError> {
    let _ = match pair {
        (Currency::BTC, Currency::JPY) => "btc_jpy",
        _ => return Err(TickerError::NotSupportPair),
    };
    let base_url = "https://coincheck.jp/api";
    let url_string = format!("{}/ticker", base_url);
    let json = core::get_json(&url_string);
    let mut decoder = json::Decoder::new(json);
    let result = Ticker::decode(&mut decoder);
    match  result {
        Ok(x) => Ok(x),
        Err(e) => Err(TickerError::DecoderError(e)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use core::Currency;

    #[test]
    fn test_ticker_okay() {
        let pair = (Currency::BTC, Currency::JPY);
        let ticker = ticker(pair);
        assert!(ticker.is_ok());
        println!("{:?}", ticker.ok().unwrap());
    }
    
    #[test]
    fn test_ticker_invalid_pair() {
        let pair = (Currency::BTC, Currency::USD);
        let ticker = ticker(pair);
        assert!(ticker.is_err());
    }
}