Skip to content

Commit

Permalink
refactor, simplify api
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Jul 10, 2014
1 parent 836135c commit cf2260f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 36 deletions.
19 changes: 7 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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)
```

Expand Down Expand Up @@ -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
{
Expand Down
43 changes: 28 additions & 15 deletions bin/review.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
})

7 changes: 6 additions & 1 deletion example/cookie/review.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
12 changes: 5 additions & 7 deletions lib/review.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -108,4 +106,4 @@ module.exports = function review () {
})

return app
}
}
4 changes: 3 additions & 1 deletion script/rasterize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {}
Expand Down

0 comments on commit cf2260f

Please sign in to comment.