aboutsummaryrefslogtreecommitdiff
path: root/src/pushbullet
diff options
context:
space:
mode:
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))
+ }
+}