Skip to content

Commit

Permalink
Use the new web functions from new Matlab version
Browse files Browse the repository at this point in the history
Using webwrite instead of urlread etc. Matlab is also able to handle
json data now. webwrite was introduced in Matlab 2015a.
  • Loading branch information
jensb89 committed Apr 28, 2016
1 parent dedd915 commit f3888aa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 211 deletions.
50 changes: 28 additions & 22 deletions Pushbullet.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
% p.pushFile(device_iden, file_name, file_type, file_url) to push a
% file which has already been uploaded
%
% Copyright 2014, Jens Brauer, https://github.com/jensb89
% Copyright 2016, Jens Brauer, https://github.com/jensb89

properties
HOST = 'https://api.pushbullet.com/v2'
Expand All @@ -29,17 +29,23 @@

function self = Pushbullet(apikey)
self.ApiKey = apikey;
if verLessThan('MATLAB','8.5')
warning(['You are using a Matlab version prior to 2015a',...
'Matlab-Pushbullet might not work with this version.',...
'Update Matlab or use an older version of Matlab-Pushbullet',...
'from here: https://github.com/jensb89/Matlab-Pushbullet/releases/']);
end
end

function load_devices(self)
% Get a list of devices
output = urlread([self.HOST,self.DEVICES_URL],...
'Authentication','Basic',...
'Username',self.ApiKey,'Get',{});
output_converted = json_parser(output);
self.Devices = output_converted{1}.devices;
options = weboptions('KeyName','Access-Token','KeyValue',self.ApiKey);
output = webread([self.HOST,self.DEVICES_URL],options);
self.Devices = output.devices;
for i=1:length(self.Devices)
sprintf('%s : %s', self.Devices{1}.nickname, self.Devices{1}.iden)
if self.Devices{i}.active && isfield(self.Devices{i},'nickname')
sprintf('%s : %s', self.Devices{i}.nickname, self.Devices{i}.iden)
end
end
end

Expand All @@ -52,13 +58,13 @@ function load_devices(self)
% title -- a title for the note
% body -- the body of the note

data = {'type', 'note',...
'device_iden',device_iden,...
'title', title,...
'body', message};
data = struct('body',message,...
'device_iden',device_iden,...
'title',title,...
'type','note');

if isempty(device_iden)
data(3:4) = []; %delete device_iden in data -> push to all connected devices
data = rmfield(data,'device_iden');
end

output = push(self, data);
Expand All @@ -75,14 +81,14 @@ function load_devices(self)
% 'image/png')
% file_url -- the url of the file

data = {'type','file',...
data = struct('type','file',...
'device_iden',device_iden,...
'file_name',file_name,...
'file_type',file_type,...
'file_url',file_url};
'file_url',file_url);

if isempty(device_iden)
data(3:4) = []; %delete device_iden in data -> push to all connected devices
data = rmfield(data,'device_iden'); %delete device_iden in data -> push to all connected devices
end

output = push(self, data);
Expand All @@ -96,25 +102,25 @@ function load_devices(self)
% title -- a title for the note
% body -- the body of the note

data = {'type', 'note',...
data = struct('type', 'note',...
'device_iden',device_iden,...
'title', title,...
'body', message,...
'url', url};
'url', url);

if isempty(device_iden)
data(3:4) = []; %delete device_iden in data -> push to all connected devices
data = rmfield(data,'device_iden'); %delete device_iden in data -> push to all connected devices
end

output = push(self, data);
end

function output = push(self, data)
% Perform the POST Request
output = urlread([self.HOST,self.PUSH_URL],...
'Authentication','Basic',...
'Username',self.ApiKey,...
'Post',data);
options = weboptions('KeyName','Access-Token',...
'KeyValue',self.ApiKey,...
'MediaType','application/json');
output = webwrite([self.HOST,self.PUSH_URL],data,options)
end

% HELPER FUNCTIONS
Expand Down
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ In order to use the API you need an API key that can be obtained

![Matlab Pushbullet Notification on iPhone](matlab-pushbullet-ios.png)

Important: The Matlab class now uses the new webwrite and webread functions from Matlab. The webwrite was introduced in Matlab R2015a. If you are using an older matlab version, use the older release of Matlab-Pushbullet [here](https://github.com/jensb89/Matlab-Pushbullet/releases/).

#Usage

Expand Down Expand Up @@ -59,8 +60,4 @@ Furthermore `device_iden` can be an e-mail adress.
License
-------

MIT license. See LICENSE for full text.

Acknowledgement
-------
[JSON-Parser](http://www.mathworks.com/matlabcentral/fileexchange/20565-json-parser) is used to parse the json output.
MIT license. See LICENSE for full text.
184 changes: 0 additions & 184 deletions json_parser.m

This file was deleted.

0 comments on commit f3888aa

Please sign in to comment.