From 4554b2a4533181886446985c9858f1e49deb4670 Mon Sep 17 00:00:00 2001 From: David Li Date: Sat, 31 Oct 2015 17:58:55 -0400 Subject: Restructure modules --- src/pushbullet/mod.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/pushbullet/mod.rs (limited to 'src/pushbullet') diff --git a/src/pushbullet/mod.rs b/src/pushbullet/mod.rs new file mode 100644 index 0000000..1f2254e --- /dev/null +++ b/src/pushbullet/mod.rs @@ -0,0 +1,54 @@ +extern crate websocket; +extern crate rustc_serialize; + +use message::Message; + +use self::websocket::Sender; +use self::websocket::Receiver; +use self::websocket::WebSocketStream; +use self::websocket::DataFrame; +use self::websocket::Client; +use self::websocket::client::request::Url; + +pub struct PBClient { + client: websocket::client::Client, + websocket::client::receiver::Receiver> +} + +impl PBClient { + pub fn new(token: &str) -> PBClient { + + let wss_url = format!("wss://stream.pushbullet.com/websocket/{}", token); + let url = Url::parse(wss_url.as_ref()).unwrap(); + let request = Client::connect(url).unwrap(); + let response = request.send().unwrap(); + response.validate().unwrap(); + + PBClient { + client: response.begin() + } + } + + fn process_message(message: Result) -> Option { + let message = match message { + Ok(m) => m, + Err(e) => { + println!("Error: {:?}", e); + return None; + } + }; + + if let websocket::Message::Text(message) = message { + Message::parse(message.as_ref()) + } + else { + None + } + } + + pub fn messages<'a>(&'a mut self) -> Box + 'a> { + let mut receiver = self.client.get_mut_reciever(); // there is a typo in the API + Box::new(receiver.incoming_messages().filter_map(PBClient::process_message)) + } +} -- cgit v1.2.3