1 2 /** 3 * a library to interact with the Instapaper API 4 * 5 * @param {object} opts options 6 * @param {string} [opts.username] the username 7 * @param {string} [opts.password] the password 8 * @constructor 9 **/ 10 var SpazInstapaper = function(opts) { 11 this.opts = sch.defaults({ 12 'username':null, 13 'password':null 14 }, opts); 15 }; 16 17 18 19 SpazInstapaper.prototype.statuses = { 20 '200' : 'authenticated', 21 '201' : 'saved', 22 '400' : 'bad request - missing parameter?', 23 '403' : 'Invalid username or password', 24 '500' : 'The service encountered an error. Please try again later' 25 }; 26 27 28 29 SpazInstapaper.prototype.urls = { 30 'authenticate' : 'https://www.instapaper.com/api/authenticate', 31 'add' : 'https://www.instapaper.com/api/add' 32 }; 33 34 35 /** 36 * Authenticate against the service (not ACTUALLY required -- see http://blog.instapaper.com/post/73123968/read-later-api) 37 * 38 * @param {object} data data hash we'll pass to authenticate 39 * @param {string} [data.username] the username -- required if not set in constructor 40 * @param {string} [data.password] the password -- required if not set in constructor 41 * @param {function} onSuccess callback to fire on success. takes no parameters 42 * @param {function} onFailure callback to fire on failure. takes parameters errmsg, status_code, xhr 43 **/ 44 SpazInstapaper.prototype.authenticate = function(data, onSuccess, onFailure) { 45 data = sch.defaults({ 46 'username':this.opts.username, 47 'password':this.opts.password 48 }, data); 49 50 if (data.title) { 51 data['auto-title'] = 0; 52 } 53 54 var errmsg; 55 var xhr = jQuery.ajax({ 56 'url' : this.urls['authenticate'], 57 'type' : 'POST', 58 'data' : data, 59 'complete': function(xhr, status) { 60 if (xhr.status == '200') { 61 onSuccess(); 62 } else { 63 var status_code = xhr.status; 64 if ( (errmsg = this.statuses[status_code]) ) { 65 onFailure(errmsg, status_code, xhr); 66 } else { 67 onFailure('Unknown Error', status_code, xhr); 68 } 69 } 70 }, 71 'dataType': 'text' 72 }); 73 }; 74 75 76 /** 77 * save a URL 78 * 79 * @param {object} data data hash we'll pass to authenticate 80 * @param {string} data.url the url to save 81 * @param {string} [data.username] the username -- required if not set in constructor 82 * @param {string} [data.password] the password -- required if not set in constructor 83 * @param {string} [data.title] the title. If set, will not auto-retrieve title 84 * @param {string} [data.selection] text to associate with the URL 85 * @param {number} [data.auto-title] Whether to try to retrieve the document's title from the URL automatically. 1 or 0 86 * @param {function} onSuccess callback to fire on success. takes one param, a hash with 'url' and 'title' 87 * @param {function} onFailure callback to fire on failure. takes parameters errmsg, status_code, xhr 88 **/ 89 SpazInstapaper.prototype.add = function(data) { 90 opts = sch.defaults({ 91 'username':this.opts.username, 92 'password':this.opts.password, 93 'url':null, 94 'title':null, 95 'selection':null, 96 'auto-title':1 97 }, opts); 98 99 if (opts.title) { 100 opts['auto-title']=0; 101 } 102 103 104 var xhr = jQuery.ajax({ 105 'url' : this.urls['add'], 106 'type' : 'POST', 107 'data' : data, 108 'complete': function(xhr, status) { 109 var errmsg; 110 111 if (xhr.status === 201) { 112 data = { 113 'url' : xhr.getResponseHeader('Content-Location'), 114 'title' : xhr.getResponseHeader('X-Instapaper-Title') 115 }; 116 onSuccess(data); 117 } else { 118 var status_code = xhr.status; 119 if ( (errmsg = this.statuses[status_code]) ) { 120 onFailure(errmsg, status, xhr); 121 } else { 122 onFailure('Unknown Error', status_code, xhr); 123 } 124 } 125 }, 126 'dataType': 'text' 127 }); 128 };