From 41688effa91319121a7fe8a981c4a7ebb986339b Mon Sep 17 00:00:00 2001 From: Kieu Anh Tuan Date: Tue, 7 Jan 2014 17:03:03 +0100 Subject: Add support to lorempixel image provider --- index.js | 1 + lib/image.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/image.unit.js | 35 ++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 lib/image.js create mode 100644 test/image.unit.js diff --git a/index.js b/index.js index 4493fc9c..fdccb091 100644 --- a/index.js +++ b/index.js @@ -22,6 +22,7 @@ exports.Address = require('./lib/address'); exports.PhoneNumber = require('./lib/phone_number'); exports.Internet = require('./lib/internet'); exports.Company = require('./lib/company'); +exports.Image = require('./lib/image'); exports.Lorem = require('./lib/lorem'); exports.Helpers = require('./lib/helpers'); exports.random = require('./lib/random'); diff --git a/lib/image.js b/lib/image.js new file mode 100644 index 00000000..0456dd84 --- /dev/null +++ b/lib/image.js @@ -0,0 +1,56 @@ +var Faker = require('../index'); +var imageProviderUrl = 'http://lorempixel.com/'; +var image = { + imageUrl: function (width, height, category) { + var width = width || 640; + var height = height || 480; + + var url = imageProviderUrl + width + '/' + height; + if (typeof category !== 'undefined') { + url += '/' + category; + } + + return url; + }, + abstract: function (width, height) { + return image.imageUrl(width, height, 'abstract'); + }, + animals: function(width, height) { + return image.imageUrl(width, height, 'animals'); + }, + business: function(width, height) { + return image.imageUrl(width, height, 'business'); + }, + cats: function(width, height) { + return image.imageUrl(width, height, 'cats'); + }, + city: function(width, height) { + return image.imageUrl(width, height, 'city'); + }, + food: function(width, height) { + return image.imageUrl(width, height, 'food'); + }, + nightlife: function(width, height) { + return image.imageUrl(width, height, 'nightlife'); + }, + fashion: function(width, height) { + return image.imageUrl(width, height, 'fashion'); + }, + people: function(width, height) { + return image.imageUrl(width, height, 'people'); + }, + nature: function(width, height) { + return image.imageUrl(width, height, 'nature'); + }, + sports: function(width, height) { + return image.imageUrl(width, height, 'sports'); + }, + technics: function(width, height) { + return image.imageUrl(width, height, 'technics'); + }, + transport: function(width, height) { + return image.imageUrl(width, height, 'transport'); + } +}; + +module.exports = image; diff --git a/test/image.unit.js b/test/image.unit.js new file mode 100644 index 00000000..005f15f7 --- /dev/null +++ b/test/image.unit.js @@ -0,0 +1,35 @@ +if (typeof module !== 'undefined') { + var assert = require('assert'); + var sinon = require('sinon'); + var Faker = require('../index'); +} + +describe("image.js", function () { + describe("imageUrl()", function () { + it("returns a random image url from lorempixel", function () { + var imageUrl = Faker.Image.imageUrl(); + + assert.equal(imageUrl, 'http://lorempixel.com/640/480'); + }); + it("returns a random image url from lorempixel with width and height", function () { + var imageUrl = Faker.Image.imageUrl(100, 100); + + assert.equal(imageUrl, 'http://lorempixel.com/100/100'); + }); + it("returns a random image url in a abstract category", function () { + var imageUrl = Faker.Image.imageUrl(100, 100, 'abstract'); + + assert.equal(imageUrl, 'http://lorempixel.com/100/100/abstract'); + }); + it("returns a random image url in a category via proxy methods", function () { + var nightlife = Faker.Image.nightlife(); + assert.equal(nightlife, 'http://lorempixel.com/640/480/nightlife'); + + var city = Faker.Image.city(); + assert.equal(city, 'http://lorempixel.com/640/480/city'); + + var fashion = Faker.Image.fashion(); + assert.equal(fashion, 'http://lorempixel.com/640/480/fashion'); + }); + }); +}); -- cgit v1.2.3