use std::io::Read;
use std::io;
use rustc_serialize::json::DecoderError;
use rustc_serialize::json::Json;
use hyper::Url;
use hyper::Client;
use hyper::header::{Connection, ConnectionOption};
pub enum ServerList {
Zaif,
Coincheck,
BitBankTrade,
BitFlyer,
BtcE,
}
pub enum Currency {
FXBTC,
BTC,
LTC,
MONA,
PPC,
NMC,
NVC,
JPY,
USD,
RUR,
EUR,
}
pub type Pair = (Currency, Currency);
pub enum TickerError {
NotSupportPair,
DecoderError(DecoderError),
Other,
}
pub trait Ticker {
fn last(&self) -> f32;
fn bid(&self) -> f32;
fn ask(&self) -> f32;
}
pub trait HasHighLow : Ticker {
fn high(&self) -> f32;
fn low(&self) -> f32;
}
pub trait HasVolume : Ticker {
fn volume(&self) -> f32;
}
pub trait HasVwap : Ticker {
fn vwap(&self) -> f32;
}
fn get(url: Url) -> Result<String, io::Error> {
let client = Client::new();
let mut res = client.get(url)
.header(Connection(vec![ConnectionOption::Close]))
.send().unwrap();
let mut body = String::new();
match res.read_to_string(&mut body) {
Ok(_) => { Ok(body) },
Err(e) => { Err(e) },
}
}
pub fn get_json(url_string: &str) -> Json {
let url = Url::parse(&url_string).unwrap();
let result = get(url).unwrap();
Json::from_str(&result).ok().expect("Can not decode a json value from the string")
}