Skip to content

Commit

Permalink
Changed callback functions to promises
Browse files Browse the repository at this point in the history
  • Loading branch information
iamphill committed Feb 24, 2015
1 parent df89cdb commit 51605de
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 112 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ new Tweetsie({
<div class="body"> \
<p>{{body}}</p> \
</div> \
',
callback: function (tweets) {
'
}).then(function (tweets) {

}).catch(function (error) {

}
});
```

Expand All @@ -49,8 +50,8 @@ The above creates a new instance of Tweetsie with all available options being us
| widgetid | ID of the widget created on [Twitter](https://twitter.com/settings/widgets) |
| count | Number of Tweets to parse. Defaults to all which is 20 |
| template | String template used to display on the page |
| callback | This is called when Tweetsie has completed parsing all the tweets |
| error | Function that is called when any errors occur |

Tweetsie uses ES6 Promises to be super awesome and hip! For older browsers, you will need to use a [Polyfill](https://github.com/jakearchibald/es6-promise).

## Styling

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Tweetsie",
"version": "0.1.0",
"version": "0.2.0",
"homepage": "https://github.com/iamphill/Tweetsie",
"description": "Twitter plugin built with JS! 'cause I mean why the hello not?!",
"main": "dist/Tweetsie.js",
Expand Down
51 changes: 28 additions & 23 deletions dist/Tweetsie.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,31 @@ var Tweetsie = (function () {
**/

function Tweetsie(opts) {
var _this = this;

_classCallCheck(this, Tweetsie);

this.opts = opts;

// No opts error message
if (this.opts === undefined) {
this.error("No object was passed to Tweetsie");
return;
}

// Not widgetid error message
if (this.opts.widgetid === undefined) {
this.error("Must pass in a `widgetid`");
return;
}

// Initialize request
this.initRequest();
return new Promise(function (resolve, reject) {
_this.resolve = resolve;
_this.reject = reject;

// No opts error message
if (_this.opts === undefined) {
_this.error("No object was passed to Tweetsie");
return;
}

// Not widgetid error message
if (_this.opts.widgetid === undefined) {
_this.error("Must pass in a `widgetid`");
return;
}

// Initialize request
_this.initRequest();
});
}

_prototypeProperties(Tweetsie, null, {
Expand All @@ -50,11 +57,9 @@ var Tweetsie = (function () {
**/

value: function error(message) {
console.log("Tweetsie: " + message);

// Return error message to error callback
if (this.opts.error !== undefined) {
this.opts.error(message);
if (this.reject !== undefined) {
this.reject(message);
}
},
writable: true,
Expand Down Expand Up @@ -158,15 +163,15 @@ var Tweetsie = (function () {
// Loop all the tweets
this.loopTweets();

// Do the callback!
if (this.opts.callback !== undefined) {
this.opts.callback(this.tweets);
}

// Should Tweetsie parse the template and then create elements based on that?
if (this.opts.template !== undefined) {
this.parseTemplate();
}

// Do the callback!
if (this.resolve !== undefined) {
this.resolve(this.tweets);
}
},
writable: true,
configurable: true
Expand Down
7 changes: 4 additions & 3 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
<div class="body"> \
<p>{{body}}</p> \
</div> \
',
callback: function (tweets) {
'
}).then(function (tweets) {

}
}).catch(function (error) {

});
</script>
</body>
45 changes: 24 additions & 21 deletions lib/Tweetsie.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,35 @@ class Tweetsie {
constructor(opts) {
this.opts = opts;

// No opts error message
if (this.opts === undefined) {
this.error('No object was passed to Tweetsie');
return;
}
return new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;

// No opts error message
if (this.opts === undefined) {
this.error('No object was passed to Tweetsie');
return;
}

// Not widgetid error message
if (this.opts.widgetid === undefined) {
this.error('Must pass in a `widgetid`');
return;
}
// Not widgetid error message
if (this.opts.widgetid === undefined) {
this.error('Must pass in a `widgetid`');
return;
}

// Initialize request
this.initRequest();
// Initialize request
this.initRequest();
});
}

/**
Outputs an error message
@param String message to output
**/
error(message) {
console.log(`Tweetsie: ${message}`);

// Return error message to error callback
if (this.opts.error !== undefined) {
this.opts.error(message);
if (this.reject !== undefined) {
this.reject(message);
}
}

Expand Down Expand Up @@ -115,15 +118,15 @@ class Tweetsie {
// Loop all the tweets
this.loopTweets();

// Do the callback!
if (this.opts.callback !== undefined) {
this.opts.callback(this.tweets);
}

// Should Tweetsie parse the template and then create elements based on that?
if (this.opts.template !== undefined) {
this.parseTemplate();
}

// Do the callback!
if (this.resolve !== undefined) {
this.resolve(this.tweets);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Tweetsie",
"version": "0.1.0",
"version": "0.2.0",
"description": "",
"scripts": {
"test": "gulp test"
Expand Down
103 changes: 45 additions & 58 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

// Create Tweetsie widget
new Tweetsie({
widgetid: widgetid,
callback: function (tweets) {
assert.equal(tweets.length, 20, 'Tweets not found!');
done();
}
widgetid: widgetid
}).then(function (tweets) {
assert.equal(tweets.length, 20, 'Tweets not found!');
done();
});
});

Expand All @@ -24,21 +23,20 @@

new Tweetsie({
widgetid: '1',
error: function (msg) {
assert.equal(msg, NOT_FOUND_MSG, 'No error getting Tweets. Huh?');
done();
}
}).catch(function (msg) {
assert.equal(msg, NOT_FOUND_MSG, 'No error getting Tweets. Huh?');
done();
});
});

test('Error no widgetid', function (assert) {
var done = assert.async();

new Tweetsie({
error: function (msg) {
assert.equal(msg, WIDGETID_ERROR_MSG, 'No widget ID');
done();
}
count: 1
}).catch(function (msg) {
assert.equal(msg, WIDGETID_ERROR_MSG, 'No widget ID');
done();
});
});

Expand All @@ -47,11 +45,10 @@

new Tweetsie({
widgetid: widgetid,
count: 1,
callback: function (tweets) {
assert.equal(tweets.length, 1, 'Tweets not found!');
done();
}
count: 1
}).then(function (tweets) {
assert.equal(tweets.length, 1, 'Tweets not found!');
done();
});
});

Expand All @@ -61,24 +58,23 @@

new Tweetsie({
widgetid: widgetid,
count: 1,
callback: function (tweets) {
var tweet = tweets[0];

assert.notEqual(tweet.id, undefined, 'Tweet ID not undefined');
assert.notEqual(tweet.tweet_url, undefined, 'Tweet URL not undefined');
assert.notEqual(tweet.date, undefined, 'Tweet date not undefined');
assert.notEqual(tweet.author.avatar, undefined, 'Tweet avatar not undefined');
assert.notEqual(tweet.author.name, undefined, 'Tweet name not undefined');
assert.notEqual(tweet.author.profile_url, undefined, 'Tweet profile URL not undefined');
assert.notEqual(tweet.author.username, undefined, 'Tweet username not undefined');
assert.notEqual(tweet.actions.favorite, undefined, 'Tweet favorite URL not undefined');
assert.notEqual(tweet.actions.reply, undefined, 'Tweet reply URL not undefined');
assert.notEqual(tweet.actions.retweet, undefined, 'Tweet retweet URL not undefined');

// Complete!
done();
}
count: 1
}).then(function (tweets) {
var tweet = tweets[0];

assert.notEqual(tweet.id, undefined, 'Tweet ID not undefined');
assert.notEqual(tweet.tweet_url, undefined, 'Tweet URL not undefined');
assert.notEqual(tweet.date, undefined, 'Tweet date not undefined');
assert.notEqual(tweet.author.avatar, undefined, 'Tweet avatar not undefined');
assert.notEqual(tweet.author.name, undefined, 'Tweet name not undefined');
assert.notEqual(tweet.author.profile_url, undefined, 'Tweet profile URL not undefined');
assert.notEqual(tweet.author.username, undefined, 'Tweet username not undefined');
assert.notEqual(tweet.actions.favorite, undefined, 'Tweet favorite URL not undefined');
assert.notEqual(tweet.actions.reply, undefined, 'Tweet reply URL not undefined');
assert.notEqual(tweet.actions.retweet, undefined, 'Tweet retweet URL not undefined');

// Complete!
done();
});
});

Expand All @@ -88,11 +84,10 @@
new Tweetsie({
widgetid: widgetid,
count: 1,
template: '',
error: function (msg) {
assert.equal(msg, TEMPLATE_EMPTY_MATCHES, 'Template matches found?!');
done();
}
template: ''
}).catch(function (msg) {
assert.equal(msg, TEMPLATE_EMPTY_MATCHES, 'Template matches found?!');
done();
});
});

Expand All @@ -102,33 +97,25 @@
new Tweetsie({
widgetid: widgetid,
count: 1,
template: '{{ body }}',
error: function (msg) {
assert.equal(msg, NO_CONTAINER, 'Somehow found a container?');
done();
}
template: '{{ body }}'
}).catch(function (msg) {
assert.equal(msg, NO_CONTAINER, 'Somehow found a container?');
done();
});
});

test('Tweet template with container', function (assert) {
var done = assert.async();
var i = 0;

new Tweetsie({
container: 'tweetsie-container',
widgetid: widgetid,
count: 1,
template: '<p>{{ body }}</p>',
callback: function (tweets) {
if (i === 0) {
setTimeout(function () {
var el = document.getElementById(CONTAINER_ID);
assert.equal(el.getElementsByTagName('p').length, 1, 'Template didn\'t render right!');
done();
}, 200);
}
i++;
}
template: '<p>{{ body }}</p>'
}).then(function (tweets) {
var el = document.getElementById(CONTAINER_ID);
assert.equal(el.getElementsByTagName('p').length, 1, 'Template didn\'t render right!');
done();
});
});
})();

0 comments on commit 51605de

Please sign in to comment.