-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor method for printing a movie
- Loading branch information
Showing
6 changed files
with
88 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var chalk = require('chalk'); | ||
var expect = require('chai').expect; | ||
|
||
var printMovie = require('./print_movie'); | ||
|
||
describe('.printMovie', function() { | ||
|
||
var stat = { | ||
movie: { | ||
imdbId: '0121766', | ||
title: 'Star Wars', | ||
|
||
rating: '7.7', | ||
plot: 'As the Clone Wars near an end, the Sith Lord Darth Sidious steps out of the shadows...', | ||
year: '2005', | ||
duration: '120 min', | ||
genres: ['Fantasy', 'Sci-Fi'], | ||
directors: ['George Lucas'], | ||
cast: ['Ewan McGregor', 'Natalie Portman'], | ||
country: ['USA'], | ||
language: ['English'] | ||
}, | ||
file: { | ||
path: '/Downloads/Star Wars.avi' | ||
} | ||
}; | ||
|
||
it('prints a movie', function() { | ||
var result = printMovie(stat); | ||
|
||
expect(result).to.include('Star Wars'); | ||
expect(result).to.include('Year: ' + chalk.cyan('2005')); | ||
expect(result).to.include('Duration: ' + chalk.cyan('120 min')); | ||
expect(result).to.include('Genres: ' + chalk.cyan('Fantasy') + ', ' + chalk.cyan('Sci-Fi')); | ||
expect(result).to.include('Country: ' + chalk.cyan('USA')); | ||
expect(result).to.include(chalk.cyan('http://www.imdb.com/title/tt00121766')); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,34 @@ | ||
require('colors'); | ||
var _ = require('lodash'); | ||
var fs = require('fs'); | ||
var chalk = require('chalk'); | ||
var path = require('path'); | ||
|
||
module.exports = function(stat) { | ||
console.log('\n----------------------------\n'); | ||
|
||
console.log(stat.movie.title.bold.blue); | ||
|
||
if (stat.movie.plot) { | ||
console.log(stat.movie.plot.italic.grey); | ||
} | ||
console.log(); | ||
|
||
if (stat.movie.rating) { | ||
console.log('Rating:'.yellow, stat.movie.rating.cyan); | ||
} | ||
var tpl = fs.readFileSync(path.resolve(__dirname, 'print_movie.tpl.txt')); | ||
var compiled = _.template(tpl.toString().trim()); | ||
|
||
if (stat.movie.year) { | ||
console.log('Year:'.yellow, stat.movie.year.cyan); | ||
} | ||
function formatCollection(collection) { | ||
return collection.map(function(item) { | ||
return chalk.cyan(item.trim()); | ||
}).join(', '); | ||
} | ||
|
||
if (stat.movie.duration) { | ||
console.log('Duration:'.yellow, stat.movie.duration.cyan); | ||
} | ||
|
||
if (stat.movie.genres) { | ||
console.log('Genre:'.yellow, _.map(stat.movie.genres, function(genre) { return genre.trim().blue; }).join(', ')); | ||
} | ||
|
||
if (stat.movie.directors) { | ||
console.log('Directors:'.yellow, _.values(stat.movie.directors).join(', ')); | ||
} | ||
module.exports = function(stat) { | ||
return compiled({ | ||
title: chalk.bold.blue(_.get(stat, 'movie.title', '')), | ||
plot: chalk.italic.grey(_.get(stat, 'movie.plot', '')), | ||
|
||
if (stat.movie.cast) { | ||
console.log('Cast:'.yellow, _.values(stat.movie.cast).slice(0, 7).join(', ')); | ||
} | ||
rating: chalk.cyan(_.get(stat, 'movie.rating', '')), | ||
year: chalk.cyan(_.get(stat, 'movie.year', '')), | ||
duration: chalk.cyan(_.get(stat, 'movie.duration', '')), | ||
genres: formatCollection(_.get(stat, 'movie.genres', [])), | ||
|
||
if (stat.movie.country && stat.movie.language) { | ||
console.log( | ||
'Country:'.yellow, stat.movie.country.join(', '), | ||
'Language:'.yellow, stat.movie.language.join(', ') | ||
); | ||
} | ||
directors: formatCollection(_.get(stat, 'movie.directors', [])), | ||
cast: formatCollection(_.get(stat, 'movie.cast', []).slice(0, 7)), | ||
|
||
if (stat.movie.imdbId) { | ||
console.log('Url:'.yellow, 'http://www.imdb.com/title/tt0' + stat.movie.imdbId); | ||
} | ||
country: formatCollection(_.get(stat, 'movie.country', [])), | ||
language: formatCollection(_.get(stat, 'movie.language', [])), | ||
|
||
console.log('File:'.yellow, stat.file.path); | ||
imdbUrl: chalk.cyan('http://www.imdb.com/title/tt0' + _.get(stat, 'movie.imdbId', '')), | ||
filePath: chalk.cyan(_.get(stat, 'file.path')) | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<%= title %> | ||
<%= plot %> | ||
|
||
Rating: <%= rating %> | ||
Year: <%= year %> | ||
Duration: <%= duration %> | ||
Genres: <%= genres %> | ||
Directors: <%= directors %> | ||
Cast: <%= cast %> | ||
Country: <%= country %> | ||
Language: <%= language %> | ||
Imdb: <%= imdbUrl %> | ||
File: <%= filePath %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters