-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vdemedes
committed
Oct 1, 2016
1 parent
5b99019
commit 4f13d2e
Showing
6 changed files
with
82 additions
and
129 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.DS_Store | ||
node_modules | ||
.nyc_output | ||
coverage |
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,7 +1,5 @@ | ||
language: node_js | ||
node_js: | ||
- '5' | ||
- '6' | ||
- '4' | ||
- '0.12' | ||
- '0.10' | ||
after_success: npm run coveralls |
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,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; |
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