aboutsummaryrefslogtreecommitdiff
path: root/src/pushbullet
diff options
context:
space:
mode:
authorDavid Li <li.davidm96@gmail.com>2015-10-31 17:58:55 -0400
committerDavid Li <li.davidm96@gmail.com>2015-10-31 17:58:55 -0400
commit4554b2a4533181886446985c9858f1e49deb4670 (patch)
tree5679d42dd4902919219ff6bcb0790e220108f0b5 /src/pushbullet
parent49e7f56a66cb9e77989e67e4d7fecba7c5e2af89 (diff)
Restructure modules
Diffstat (limited to 'src/pushbullet')
-rw-r--r--src/pushbullet/mod.rs54
1 files changed, 54 insertions, 0 deletions
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::dataframe::DataFrame,
+ websocket::client::sender::Sender<websocket::stream::WebSocketStream>,
+ websocket::client::receiver::Receiver<websocket::stream::WebSocketStream>>
+}
+
+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<websocket::Message, websocket::result::WebSocketError>) -> Option<Message> {
+ 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<Iterator<Item=Message> + '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))
+ }
+}