Skip to content

Commit

Permalink
update project
Browse files Browse the repository at this point in the history
  • Loading branch information
vdemedes committed Oct 1, 2016
1 parent 5b99019 commit 4f13d2e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 129 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
node_modules
.nyc_output
coverage
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: node_js
node_js:
- '5'
- '6'
- '4'
- '0.12'
- '0.10'
after_success: npm run coveralls
114 changes: 48 additions & 66 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,64 @@
'use strict';

/**
* Dependencies
*/

var got = require('got');


/**
* Google Images Client
*/

function Client (id, apiKey) {
if (!(this instanceof Client)) {
return new Client(id, apiKey);
const qs = require('querystring');
const got = require('got');

class Client {
constructor(id, apiKey) {
this.endpoint = 'https://www.googleapis.com';
this.apiKey = apiKey;
this.id = id;
}

this.endpoint = 'https://www.googleapis.com';
this.apiKey = apiKey;
this.id = id;
}
search(query, options) {
if (!query) {
throw new TypeError('Expected a query');
}

Client.prototype.search = function (query, options) {
if (!query) {
throw new TypeError('Expected a query');
return got(this.endpoint + '/customsearch/v1?' + this._buildOptions(query, options), {
json: true
}).then(this._buildResponse);
}

query = query.replace(/[- ]/g, '');
_buildOptions(query, options) {
if (!options) {
options = {};
}

return got(this.endpoint + '/customsearch/v1', {
query: this._buildOptions(query, options),
json: true
}).then(this._buildResponse);
};
var result = {
q: query.replace(/\s/g, '+'),
searchType: 'image',
cx: this.id,
key: this.apiKey
};

Client.prototype._buildOptions = function (query, options) {
if (!options) {
options = {};
}
if (options.page) {
result.start = options.page;
}

var result = {
q: query.replace(/\s/g, '+'),
searchType: 'image',
cx: this.id,
key: this.apiKey
};
if (options.size) {
result.imgSize = options.size;
}

if (options.page) {
result.start = options.page;
return qs.stringify(result);
}

if (options.size) {
result.imgSize = options.size;
_buildResponse(res) {
return (res.body.items || []).map(function (item) {
return {
type: item.mime,
width: item.image.width,
height: item.image.height,
size: item.image.byteSize,
url: item.link,
thumbnail: {
url: item.image.thumbnailLink,
width: item.image.thumbnailWidth,
height: item.image.thumbnailHeight
}
};
});
}

return result;
};

Client.prototype._buildResponse = function (res) {
return res.body.items.map(function (item) {
return {
type: item.mime,
width: item.image.width,
height: item.image.height,
size: item.image.byteSize,
url: item.link,
thumbnail: {
url: item.image.thumbnailLink,
width: item.image.thumbnailWidth,
height: item.image.thumbnailHeight
}
};
});
};


/**
* Expose client
*/
}

module.exports = Client;
15 changes: 5 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,17 @@
"files": [
"index.js"
],
"main": "index.js",
"dependencies": {
"got": "^5.3.0"
"got": "^6.5.0"
},
"devDependencies": {
"ava": "^0.9.1",
"coveralls": "^2.11.6",
"eslint-config-vdemedes": "^1.0.1",
"nyc": "^5.3.0",
"xo": "^0.12.1"
"ava": "^0.16.0",
"coveralls": "^2.11.14",
"nyc": "^8.3.0",
"xo": "^0.16.0"
},
"scripts": {
"test": "xo && nyc ava --serial --verbose",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"xo": {
"extends": "vdemedes"
}
}
16 changes: 3 additions & 13 deletions Readme.md → readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# google-images

[![Build Status](https://travis-ci.org/vdemedes/google-images.svg?branch=master)](https://travis-ci.org/vdemedes/google-images)
[![Coverage Status](https://coveralls.io/repos/vdemedes/google-images/badge.svg?branch=master&service=github)](https://coveralls.io/github/vdemedes/google-images?branch=master)
# google-images [![Build Status](https://travis-ci.org/vdemedes/google-images.svg?branch=master)](https://travis-ci.org/vdemedes/google-images) [![Coverage Status](https://coveralls.io/repos/vdemedes/google-images/badge.svg?branch=master&service=github)](https://coveralls.io/github/vdemedes/google-images?branch=master)

This module for Node.js helps searching images using Google Images.
It provides just one method, *search*, simplicity FTW.
Expand All @@ -19,9 +16,9 @@ $ npm install google-images --save
**Note**: You'll need to [set up your own Google Custom Search Engine](#set-up-google-custom-search-engine) to search for images.

```js
const googleImages = require('google-images');
const ImagesClient = require('google-images');

let client = googleImages('CSE ID', 'API KEY');
let client = new ImagesClient('CSE ID', 'API KEY');

client.search('Steve Angello')
.then(function (images) {
Expand Down Expand Up @@ -77,13 +74,6 @@ Register a new app and enable Google Custom Search Engine API here: [Google Deve
Make a note of the API key.


## Tests

```
$ npm test
```


## License

MIT © [Vadym Demedes](http://vadimdemedes.com)
60 changes: 23 additions & 37 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
'use strict';

/**
* Dependencies
*/

const test = require('ava');
const http = require('http');
const qs = require('querystring');
const test = require('ava');

const googleImages = require('./');


/**
* Tests
*/
const ImagesClient = require('./');

test('query', async t => {
let client = testClient('id', 'api');
let server = testServer();
const client = testClient('id', 'api');
const server = testServer();

let query = qs.stringify({
const query = qs.stringify({
q: 'steve+angello',
searchType: 'image',
cx: 'id',
Expand All @@ -31,15 +22,15 @@ test('query', async t => {
server.close();
});

let images = await client.search('steve angello');
t.same(images, fakeImages());
const images = await client.search('steve angello');
t.deepEqual(images, fakeImages());
});

test('page option', async t => {
let client = testClient('id', 'api');
let server = testServer();
const client = testClient('id', 'api');
const server = testServer();

let query = qs.stringify({
const query = qs.stringify({
q: 'steve+angello',
searchType: 'image',
cx: 'id',
Expand All @@ -52,18 +43,18 @@ test('page option', async t => {
server.close();
});

let images = await client.search('steve angello', {
const images = await client.search('steve angello', {
page: 1
});

t.same(images, fakeImages());
t.deepEqual(images, fakeImages());
});

test('size option', async t => {
let client = testClient('id', 'api');
let server = testServer();
const client = testClient('id', 'api');
const server = testServer();

let query = qs.stringify({
const query = qs.stringify({
q: 'steve+angello',
searchType: 'image',
cx: 'id',
Expand All @@ -76,27 +67,22 @@ test('size option', async t => {
server.close();
});

let images = await client.search('steve angello', {
const images = await client.search('steve angello', {
size: 'large'
});

t.same(images, fakeImages());
t.deepEqual(images, fakeImages());
});


/**
* Helpers
*/

function testClient (id, apiKey) {
let client = googleImages(id, apiKey);
function testClient(id, apiKey) {
const client = new ImagesClient(id, apiKey);
client.endpoint = 'http://localhost:9999';

return client;
}

function testServer () {
let server = http.createServer(function (req, res) {
function testServer() {
const server = http.createServer(function (req, res) {
server.emit(req.url, req, res);
});

Expand All @@ -105,7 +91,7 @@ function testServer () {
return server;
}

function fakeResponse () {
function fakeResponse() {
return JSON.stringify({
items: [{
link: 'http://steveangello.com/boss.jpg',
Expand All @@ -122,7 +108,7 @@ function fakeResponse () {
});
}

function fakeImages () {
function fakeImages() {
return [{
url: 'http://steveangello.com/boss.jpg',
type: 'image/jpeg',
Expand Down

0 comments on commit 4f13d2e

Please sign in to comment.