/* * GET specs. */ exports.fetch = function(req, res){ var _ = require('underscore'), jsdom = require('jsdom'), request = require('request'); var model = req.params.model, isMinified = req.query.minified == 'true' ? true : false; modifier = req.query.modifier; console.log('Model requested: ', model, req.params, req.query, isMinified); request( { uri: 'http://www.mazda.com.au/vehicles/'+ model +'/specifications' }, function (error, response, body) { // swift error checking if (error && response.statusCode !== 200) { console.log('Request error.'); // TODO: render error page here... make sure error check includes case when unknown model selected } // console.log(response); // data for specs var specs = []; var tickUrl = 'http://www.mazda.com.au/brochures/base-framework/img/specs/specs_tick.gif', falseString = '-'; jsdom.env( { html: body, scripts: ['http://code.jquery.com/jquery.min.js'] }, function (error, window) { var $ = window.jQuery, $body = $(window.document.body), $styles = $('.spec-body'); // For each Body Style (Sedan, Wagon, etc.) $styles.each(_parseBodyStyleHtml); // console.log(specs); // console.log('FIRST BODY STYLE CATEGORIES YO: ', _.pluck(specs[0].categories, 'name')); // res.send('There are ' + $styles.length + ' body style(s) for the ' + model); var carName = $body.find('h1').text().replace(' Specifications',''); res.render( 'specs', { title: carName, styles: specs, modifier: modifier, pretty: !isMinified } ); function _parseBodyStyleHtml(i,bodyStyle) { var style = { name: '', image: '', slug: '', grades: [], categories: [] }; var $bodyStyle = $(bodyStyle), $cats = $bodyStyle.find('.spec-cat'); // Create array of grade names (e.g. 'Sport', 'Touring', etc.) $cats.first().find('table thead th').each(function (i,grade) { // the first