Permalink
Browse files

refactor, simplify api

  • Loading branch information...
juliangruber committed Jul 10, 2014
1 parent 836135c commit cf2260f481df1add4b7c111329693be93a5c1326
Showing with 49 additions and 36 deletions.
  1. +7 −12 README.md
  2. +28 −15 bin/review.js
  3. +6 −1 example/cookie/review.js
  4. +5 −7 lib/review.js
  5. +3 −1 script/rasterize.js
View
@@ -23,8 +23,7 @@ reviews in one server.
$ npm install -g review
$ review --sites='{"google":"http://google.com","facebook":"http://facebook.com"}' \
--resolutions='["1280x1024", "1900x1600", "800x600"]' \
--cookie='{"name":"cookie_monster","value":"i_eat_them","domain":"google.com"}'
--resolutions='["1280x1024", "1900x1600", "800x600"]'
$ open http://localhost:4000/
$ # and check
@@ -41,7 +40,7 @@ Options:
--resolutions, -r Resolutions as JSON Array of strings [default: "[\"1200x800\"]"]
--wait, -w Time to give the page to finish loading, in milliseconds [default: 0]
--cache, -c Cache snapshots for x milliseconds [default: false]
--cookie Adds a cookie to PhatomJS. Can be called multiple times [default: "{}"]
--cookie Add a cookie to PhatomJS
--cut Cut snapshots to exact screen size [default: false]
--help, -h Print usage instructions
@@ -60,15 +59,11 @@ review()
dir : __dirname + '/cache/',
expires : 60
})
.cookies([{
.cookie({
name : 'cookie monster',
value : 'i eat them!',
domain : 'google.com'
},{
name : 'universe',
value : '42',
domain : 'google.com'
}])
})
.listen(4000)
```
@@ -121,11 +116,11 @@ Defaults to `0`.
Cache rendered snapshots for `expires` seconds in `dir`.
### review#cookies([cookie, cookie, ...])
### review#cookie(cookie)
An array of cookies that PhatomJS will use when requesting all pages.
Add a cookie for PhantomJS to use. Can be called multiple times, to set multiple cookies.
The individual cookie format is:
The cookie format is:
```js
{
View
@@ -32,8 +32,7 @@ var argv = optimist
.default('cache', false)
.alias('c', 'cache')
.describe('cookie', 'Adds a cookie to PhatomJS. Can be called multiple times')
.default('cookie', '{}')
.describe('cookie', 'Add a cookie to PhatomJS')
.describe('cut', 'Cut snapshots to exact screen size')
.default('cut', false)
@@ -44,16 +43,30 @@ var argv = optimist
if (argv.help || !argv.sites) return optimist.showHelp()
var cookies = "[" + argv.cookie + "]" // wrap in square brackets, not a valid JSON otherwise
review()
.title(argv.title)
.sites(JSON.parse(argv.sites))
.resolutions(JSON.parse(argv.resolutions))
.wait(argv.wait)
.cookies(JSON.parse(cookies))
.cut(argv.cut)
.cache(argv.cache? { dir : __dirname + '/cache', expires : argv.cache } : false)
.listen(argv.port, function () {
console.log('-> Review on port ' + argv.port)
})
var server = review()
server.title(argv.title)
server.sites(JSON.parse(argv.sites))
server.resolutions(JSON.parse(argv.resolutions))
server.wait(argv.wait)
server.cut(argv.cut)
if (argv.cache) {
server.cache({
dir : __dirname + '/cache',
expires : argv.cache
})
} else {
server.cache(false)
}
var cookies = argv.cookies
if (!Array.isArray(cookies)) cookies = [cookies]
cookies.forEach(function(cookie){
server.cookie(JSON.parse(cookie))
});
server.listen(argv.port, function () {
console.log('-> Review on port ' + argv.port)
})
View
@@ -19,6 +19,11 @@ review()
value : 'this is what I do',
domain : 'localhost'
})
.cookie({
name : 'eating this eating that',
value : 'cookies never make me fat',
domain : 'localhost'
})
.listen(5000, function () {
console.log('-> Review on port 5000')
})
})
View
@@ -30,19 +30,17 @@ module.exports = function review () {
*/
var setters = [
'title', 'sites', 'resolutions', 'wait', 'cache', 'cookies', 'cut'
'title', 'sites', 'resolutions', 'wait', 'cache', 'cut'
]
setters.forEach(function (key) {
app[key] = function (value) {
return app.set(key, value)
}
})
// for backward compatibility
app['cookie'] = function(cookie) {
var cookiesJar = app.get('cookies')
cookiesJar.push(cookie)
return app.set('cookies', cookiesJar)
app.cookie = function(cookie){
app.get('cookies').push(cookie)
return app
}
/**
@@ -108,4 +106,4 @@ module.exports = function review () {
})
return app
}
}
View
@@ -22,7 +22,9 @@ if (cut) page.clipRect = {
}
cookies = JSON.parse(cookies)
cookies.forEach(function(cookie) { phantom.addCookie(cookie) })
cookies.forEach(function(cookie) {
phantom.addCookie(cookie)
})
// silence phantomjs
page.onConsoleMessage = function () {}

0 comments on commit cf2260f

Please sign in to comment.