Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Feb 17, 2013
1 parent 67f9308 commit c7c2527
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 201 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
example/cache/
example/cached/cache
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

_Get screenshots of all your running sites in different resolutions!_

Updating large and possibly responsively designed sites can be a hassle. You never know whether your change breakes
anything on the other end of your sitemap, or in a certain resolution, except if have a look at every individual
Updating large and possibly responsively designed sites can be a hassle. You
never know whether your change breakes anything on the other end of your
sitemap, or in a certain resolution, except if have a look at every individual
page...in every resolution you care about.

The idea of `review` is not to test everything (and visual testing is hard) but rather use the human mind's excellent
ability to quickly scan information and filter out what you need, i.e. what is broken.
The idea of `review` is not to test everything (and visual testing is hard) but
rather use the human mind's excellent ability to quickly scan information and
filter out what you need, i.e. what is broken.

![preview](http://f.cl.ly/items/3O1w3Y0X2i0s1F1M273x/Screen%20Shot%202013-01-24%20at%2012.50.38%20PM.png)

This screenshot is from [review-host](https://github.com/juliangruber/review-host), which hosts multiple reviews in one server.
This screenshot is from
[review-host](https://github.com/juliangruber/review-host), which hosts multiple
reviews in one server.

## Usage

Expand Down Expand Up @@ -74,7 +78,9 @@ Display `title` in the review. Defaults to `Review`.

### review#sites(sites | fn)

Either pass an object with titles as keys and urls as values, or a `Function` that fetches the sites to be displayed. This way, on every page load that list is refreshed and you can e.g. display all sites present in your sitemap.
Either pass an object with titles as keys and urls as values, or a `Function`
that fetches the sites to be displayed. This way, on every page load that list
is refreshed and you can e.g. display all sites present in your sitemap.

```js
review.sites(function (cb) {
Expand All @@ -85,15 +91,17 @@ review.sites(function (cb) {
})
```

See [example/sites.js](https://github.com/juliangruber/review/blob/master/example/sites.js)
See [example/sites](https://github.com/juliangruber/review/blob/master/example/sites/sites.js)

### review#resolutions(["WxH", "WxH", ...])

Configure the resolutions to use for screenshots. Defaults to `["1200x800"]`

### review#wait(x)

PhantomJS will wait for `x` milliseconds after loading the page before it takes the screenshot, so you can make sure your page is completely loaded. Defaults to `0`.
PhantomJS will wait for `x` milliseconds after loading the page before it takes
the screenshot, so you can make sure your page is completely loaded.
Defaults to `0`.

### review#cache({ dir : 'directory', expires : 60 })

Expand All @@ -119,15 +127,17 @@ The cookie format is:

### review#cut(bool)

If `bool` is `true` then screenshots will be cut to the exact dimensions that you specified. Without this pages can be longer than your specified height.
If `bool` is `true` then screenshots will be cut to the exact dimensions that
you specified. Without this pages can be longer than your specified height.

### review#listen(port)

Start the review server on port `port`.

## Installation

You need to have phantomjs (>=1.7 if you want to use cookies) installed, get it via
You need to have phantomjs (>=1.7 if you want to use cookies) installed, get it
via

```bash
$ brew install phantomjs
Expand Down
2 changes: 1 addition & 1 deletion example/cache.js → example/cached/cache.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var review = require('..')
var review = require('../..')

review()
.title('Super Review')
Expand Down
16 changes: 0 additions & 16 deletions example/sites.js

This file was deleted.

21 changes: 21 additions & 0 deletions example/sites/sites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var review = require('../..')

review()
.title('My Review')
.sites(function (cb) {
cb(null, Math.random() > 0.5
? {
microsoft : 'http://www.microsoft.com/',
bootstrap : 'http://twitter.github.com/bootstrap/'
}
: {
words : 'http://www.newnet-soft.com/beta/',
alistapart : 'http://alistapart.com/'
}
)
})
.resolutions(['1900x1600', '1280x1024', '800x600'])
.cut(true)
.listen(5000, function () {
console.log('-> Review on port 5000')
})
110 changes: 45 additions & 65 deletions lib/review.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
/**
* module dependencies
*/

var express = require('express')
var span = require('span')
var snapshot = require('./snapshot')

module.exports = review
/**
* review app
*/

function review () {
var title = 'Review'
var sites = {}
var resolutions = ['1200x800']
var wait = 0
var cache = false

module.exports = function review () {
var app = express()
var snapshot = require('./snapshot')

app.set('title', 'Review')
app.set('sites', {})
app.set('resolutions', ['1200x800'])
app.set('wait', 0)
app.set('cache', false)
app.set('cookie', {})
app.set('cut', false)

app.set('view engine', 'jade')
app.set('views', __dirname + '/../views')
Expand All @@ -20,62 +28,39 @@ function review () {
/**
* setters
*/

app.title = function (_title) {
title = _title
return app
}

app.sites = function (_sites) {
sites = _sites
return app
}

app.resolutions = function (_resolutions) {
resolutions = _resolutions
return app
}

app.wait = function (_wait) {
wait = _wait
return app
}

app.cache = function (_cache) {
cache = _cache
snapshot.cache(_cache)
return app
}

app.cookie = function (cookie) {
snapshot.cookie(cookie)
return app
}

app.cut = function (cut) {
snapshot.cut(cut)
return app
}

var setters = [
'title', 'sites', 'resolutions', 'wait', 'cache', 'cookie', 'cut'
]
setters.forEach(function (key) {
app[key] = function (value) {
return app.set(key, value)
}
})

/**
* routes
* snapshot route
*/

app.get('/snapshot/:url/:resolution/:wait', snapshot(app))

/**
* index
*/

app.get('/', function (req, res, next) {
if (!sites) return res.status(500).end('sites not set')
app.get('/', function (req, res, next) {
if (typeof app.get('sites') == 'function') app.get('sites')(onSites)
else onSites(null, app.get('sites'))

if (typeof sites == 'function') sites(onSites)
else onSites(null, sites)

function onSites (err, sites) {
function onSites (err, _sites) {
if (err) return next(err)

/**
* calculate maximum screenshot width, used for scaling
*/

var maxWidth
resolutions.forEach(function (resolution) {
app.get('resolutions').forEach(function (resolution) {
var width = parseInt(resolution.split('x')[0], 10)
if (!maxWidth || width > maxWidth) maxWidth = width
})
Expand All @@ -84,7 +69,7 @@ function review () {
* add css information to resolutions
*/

var _resolutions = resolutions.map(function (resolution) {
var resolutions = app.get('resolutions').map(function (resolution) {
var width = parseInt(resolution.split('x')[0], 10)

return {
Expand All @@ -98,27 +83,22 @@ function review () {
* encode sites' URLs to be URL-safe
*/

var _sites = {}
Object.keys(sites).forEach(function (title) {
_sites[title] = encodeURIComponent(sites[title])
var sites = {}
Object.keys(_sites).forEach(function (title) {
sites[title] = encodeURIComponent(_sites[title])
})

/**
* render
*/

res.render('index', {
title : title,
sites : _sites,
resolutions : _resolutions,
wait : wait,
cache : cache,
expires : cache? span(cache.expires * 1000) : null
sites : sites,
resolutions : resolutions,
expires : app.get('cache')? span(app.get('cache').expires * 1000) : null
})
}
})

app.use('/snapshot', snapshot)

return app
}
Loading

0 comments on commit c7c2527

Please sign in to comment.