Skip to content

Commit 2840df9

Browse files
committed
fetch items by category
1 parent 2d7d4c7 commit 2840df9

5 files changed

Lines changed: 60 additions & 18 deletions

File tree

demo/fetchItemsByCategory.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const Ebay = require('../src/index');
2+
3+
let ebay = new Ebay({
4+
clientID: "Ajaykuma-nodeapi-PRD-bf1a91299-ed4deb45",
5+
limit: 6
6+
});
7+
ebay.findItemsByCategory().then((data) => {
8+
console.log(data);
9+
}, (error) => {
10+
console.log(error);
11+
});
12+
13+
14+
15+

src/buildURL.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ const buildURL = {
3232
* @return {String} build url
3333
* @private
3434
*/
35-
buildShoppingUrl() {
35+
buildShoppingUrl(options) {
3636
let base_url = "http://open.api.ebay.com/Shopping?";
37-
base_url += "SECURITY-APPNAME=" + this.options.clientID;
38-
base_url += "&OPERATION-NAME=" + configData["findItemsByKeywords"]["OPERATION-NAME"];
37+
base_url += "SECURITY-APPNAME=" + options.clientID;
38+
base_url += "&OPERATION-NAME=" + options.operationName;
3939
base_url += "&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD&";
40-
base_url += param + "=" + data;
41-
base_url += "&paginationInput.entriesPerPage=" + this.options.limit;
42-
base_url += "&GLOBAL-ID=" + this.options.globalID;
40+
base_url += options.param + "=" + options.name;
41+
base_url += "&paginationInput.entriesPerPage=" + options.limit;
42+
//base_url += "&GLOBAL-ID=" + oglobalID;
4343
return base_url;
4444
}
4545
};

src/index.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Ebay.prototype = {
1818
this.options.name = keyword;
1919
this.options.operationName = "findItemsByKeywords";
2020
this.options.param = "keywords";
21-
let url = urlObject.buildSearchUrl(this.options, keyword);
21+
let url = urlObject.buildSearchUrl(this.options);
2222
console.log(url);
2323
return makeRequest(url).then((data) => {
2424
let result = JSON.parse(data);
@@ -30,9 +30,31 @@ Ebay.prototype = {
3030

3131
},
3232

33+
findItemsByCategory: function (categoryID) {
34+
if (!categoryID) throw new Error("Category ID is null or invalid");
35+
this.options.name = categoryID;
36+
this.options.operationName = "findItemsByCategory";
37+
this.options.param = "categoryId";
38+
let url = urlObject.buildSearchUrl(this.options);
39+
console.log(url);
40+
return makeRequest(url).then((data) => {
41+
let result = JSON.parse(data);
42+
console.log(result);
43+
return result["findItemsByKeywordsResponse"];
44+
45+
}, (error) => {
46+
console.log(error);
47+
})
48+
49+
},
50+
3351
getAllCategories: function () {
3452
//console.log(url);
35-
return makeRequest("http://open.api.ebay.com/Shopping?callname=GetCategoryInfo&appid=" + this.options.clientID + "&version=967&siteid=0&CategoryID=-1&responseencoding=JSON&IncludeSelector=ChildCategories").then((data) => {
53+
this.options.name = keyword;
54+
this.options.operationName = "findItemsByKeywords";
55+
this.options.param = "keywords";
56+
let url = urlObject.buildShoppingUrl(this.options);
57+
return makeRequest(url).then((data) => {
3658
let result = JSON.parse(data);
3759
return result;
3860
}, (error) => {

test/test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
let expect = require("chai").expect;
22
let should = require('chai').should();
33
let eBay = require('../src/index');
4-
let configData = require('../src/config');
54

65
describe("check all the options provided is valid or not - Ebay Constructor ", () => {
76
it("check input is provided or not", () => {

test/testBuildURL.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
let expect = require("chai").expect;
22
let should = require('chai').should();
33
let eBay = require('../src/index');
4+
let buildURL = require('../src/buildURL');
45

5-
describe("check build url method", () => {
6-
let ebay = new eBay({
7-
clientID: "testID",
8-
limit: 6
9-
});
10-
let expected_url = "http://svcs.ebay.com/services/search/FindingService/v1?SECURITY-APPNAME=testID&OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD&keywords=iphone&paginationInput.entriesPerPage=6&GLOBAL-ID=EBAY-US"
11-
it("should build correct url", () => {
12-
console.log(ebay.buildAPIUrl("iphone"));
13-
// expect(ebay.buildAPIUrl("iphone")).to.be.equal(expected_url);
6+
describe("test building url methods", () => {
7+
8+
let expected_search_url = "http://svcs.ebay.com/services/search/FindingService/v1?SECURITY-APPNAME=testID&OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD&keywords=iphone&paginationInput.entriesPerPage=6&GLOBAL-ID=EBAY-US"
9+
it("test search url", () => {
10+
11+
let options = {
12+
name: "iphone",
13+
operationName: "findItemsByKeywords",
14+
param: "keywords",
15+
clientID: "testID",
16+
limit: 6,
17+
globalID: "EBAY-US"
18+
}
19+
expect(buildURL.buildSearchUrl(options)).to.be.equal(expected_search_url);
1420
});
1521
});

0 commit comments

Comments
 (0)