1
+ /**
2
+ * The eBay Shopping/Finding API JavaScript Client
3
+ * The client works by performing JSONP requests. The client also supports AJAX if JQuery is installled.
4
+ * @author : Rajat Mittal
5
+ * @version : 1.0
6
+ * @param params[Object] - parameter object
7
+ * {
8
+ * appId (required): application id
9
+ * mode (optional) : request mode. One of the following two types ('AJAX', 'JSONP') [ Default : 'JSONP' ]
10
+ * url (optional) : base url . Only required when mode is AJAX. It substitutes the constant
11
+ * SHOPPING_API/FINDING_API with the provided url.
12
+ * }
13
+ */
14
+ function ebay ( params ) {
15
+ if ( params . appId ) {
16
+ this . appId = params . appId ;
17
+ this . _responseFormat = 'JSON' ;
18
+ this . callback = params . callback ;
19
+ this . mode = params . mode || 'JSONP' ;
20
+ if ( this . mode === 'AJAX' && params . url && params . url !== '' ) {
21
+ ebay . SHOPPING_API = params . url + '?callname=' ;
22
+ ebay . FINDING_API = params . url + '?&SERVICE-VERSION=1.0.0&REST-PAYLOAD&OPERATION-NAME=' ;
23
+ }
24
+ }
25
+ }
26
+
27
+ ebay . SHOPPING_API = 'http://open.api.ebay.com/shopping?callname=' ;
28
+ ebay . FINDING_API = 'http://svcs.ebay.com/services/search/FindingService/v1?&SERVICE-VERSION=1.0.0&REST-PAYLOAD&OPERATION-NAME=' ;
29
+
30
+ /**
31
+ * Utility function to generate linear unique numbers to supplement unique callbacknames, unique script sources
32
+ * @return [number] - counter
33
+ */
34
+ ebay . _getCallId = ( function ( ) {
35
+ var callId = 0 ;
36
+ return function ( ) {
37
+ return callId ++ ;
38
+ }
39
+ } ) ( ) ;
40
+
41
+ /**
42
+ * Utility function to merge two objects
43
+ * @param source[Object]- Object to merge from
44
+ * @param target[Object]- Object to merge into
45
+ * @return [Object] - final object composed after merging source object in the target object
46
+ */
47
+ ebay . _merge = function ( source , target ) {
48
+ for ( attrs in source ) {
49
+ if ( source . hasOwnProperty ( attrs ) ) { //dont navigate the prototype chain.
50
+ target [ attrs ] = source [ attrs ] ;
51
+ }
52
+ }
53
+ return target ;
54
+ } ;
55
+
56
+ /**
57
+ * Remove unneccesary fields from the response.
58
+ * @param response[Object] - response object to cleanup
59
+ */
60
+ ebay . _cleanResponse = function ( response ) {
61
+ delete response . Ack ;
62
+ delete response . Build ;
63
+ delete response . Version ;
64
+ delete response . Timestamp ;
65
+
66
+ return response ;
67
+ } ;
68
+
69
+ /**
70
+ * Executes a particular call
71
+ * @param contextObj - Object defining the call and its primary parameter
72
+ * @param props[Object] - object holding additional request parameters
73
+ * @param callback[Function] - Reference to callback fn to report the response of this call to. If not specified,
74
+ * the object's callback fn is called.
75
+ */
76
+ ebay . _execute = function ( contextObj , props , callback ) {
77
+ var queryObj = ebay . _merge ( props , contextObj ) ;
78
+ var query = this . _buildQuery ( queryObj ) ;
79
+ this . _performQuery ( query , callback ) ;
80
+ } ;
81
+
82
+ /**
83
+ * A modified curry function to allow partial functions inside the ebay client
84
+ */
85
+ ebay . _curry = function ( ) {
86
+ var args = Array . prototype . slice . call ( arguments ) ;
87
+ var fn = args . shift ( ) ;
88
+ return function ( ) {
89
+ return fn . apply ( this , args . concat (
90
+ Array . prototype . slice . call ( arguments ) ) ) ;
91
+ } ;
92
+ } ;
93
+
94
+ /**
95
+ * Creates a JSONP call
96
+ * @param url- url to do the request at. The url should be ending with the query string param for JSONP
97
+ * as defined by the provider of the JSONP service.
98
+ * @param callback[Function] - Reference to callback fn to report the response of this call to.
99
+ */
100
+ ebay . _getJSON = function ( url , callback ) {
101
+ var script = document . createElement ( 'SCRIPT' ) ;
102
+ var tempCallbackName = 'JSONP' + ebay . _getCallId ( ) ;
103
+ script . src = url + tempCallbackName ;
104
+ window [ tempCallbackName ] = function ( response ) {
105
+ callback ( response ) ;
106
+ delete window [ tempCallbackName ] ;
107
+ script . parentNode . removeChild ( script ) ;
108
+ } ;
109
+ document . getElementsByTagName ( 'HEAD' ) [ 0 ] . appendChild ( script ) ;
110
+ } ;
111
+
112
+ /**
113
+ * Constructs a complete URL for making a GET request
114
+ * props[Object] - property object
115
+ * {
116
+ * callName - name of the actual ebay api call to make [REQUIRED]
117
+ * All call specific parameters
118
+ * }
119
+ */
120
+ ebay . prototype . _buildQuery = function ( props ) {
121
+ function adder ( ) {
122
+ delete props . callName ;
123
+ var urlSuffix = '' ;
124
+ for ( var key in props ) {
125
+ if ( props . hasOwnProperty ( key ) ) {
126
+ urlSuffix += '&' + key + '=' + props [ key ] ;
127
+ }
128
+ }
129
+ return urlSuffix ;
130
+ }
131
+ if ( props . callName ) {
132
+ if ( props . type === 'FINDING' ) {
133
+ var url = ebay . FINDING_API + props . callName + '&SECURITY-APPNAME=' + this . appId + '&RESPONSE-DATA-FORMAT=' + this . _responseFormat ;
134
+ url = url + adder ( ) + '&callback=' ;
135
+ }
136
+ else {
137
+ var url = ebay . SHOPPING_API + props . callName + '&appid=' + this . appId + '&version=525' + '&responseencoding=' + this . _responseFormat ;
138
+ url = url + adder ( ) + '&callbackname=' ;
139
+ }
140
+ return url ;
141
+ }
142
+ return false ;
143
+ } ;
144
+
145
+ /**
146
+ * Do the actual query
147
+ * @param query - URL
148
+ * @callback - call specific callback fn. If not provided, than the callback function attached to the ebay object is used
149
+ */
150
+ ebay . prototype . _performQuery = function ( query , callback ) {
151
+ var self = this ;
152
+ if ( self . mode === 'AJAX' ) {
153
+ jQuery . ajax ( {
154
+ url : query ,
155
+ success : function ( response ) {
156
+ callback = callback || self . callback ;
157
+ callback ( ebay . _cleanResponse ( response ) ) ;
158
+ }
159
+ } ) ;
160
+ }
161
+ else {
162
+ ebay . _getJSON ( query , function ( response ) {
163
+ if ( callback ) {
164
+ callback ( ebay . _cleanResponse ( response ) ) ;
165
+ }
166
+ else if ( self . callback ) {
167
+ self . callback ( ebay . _cleanResponse ( response ) ) ;
168
+ }
169
+ } ) ;
170
+ }
171
+ } ;
172
+
173
+ /**
174
+ * Get User Profiles
175
+ * @param userIds[Array] - Array of ids of the users
176
+ * @param props[Object] - object holding additional request parameters ( so called IncludeSelectors )
177
+ * @param callback[Function] - Reference to callback fn to report this response to. If not specified,
178
+ * the object's callback fn is called.
179
+ */
180
+ ebay . prototype . getUsers = function ( userIds , props , callback ) {
181
+ var noOfUsers = userIds . length ;
182
+ if ( noOfUsers > 1 ) {
183
+ var finalResponse = [ ] ;
184
+ var callbackCount = 0 ;
185
+ var self = this ;
186
+ var callbackIdentifier = '_tempCallback' + ebay . _getCallId ( ) ;
187
+ this [ callbackIdentifier ] = function ( insertAt , response ) {
188
+ finalResponse [ insertAt ] = response ;
189
+ callbackCount ++ ;
190
+ if ( callbackCount === noOfUsers ) {
191
+ var callback = callback || self . callback ;
192
+ delete self [ callbackIdentifier ] ;
193
+ callback ( finalResponse ) ;
194
+ }
195
+ } ;
196
+
197
+ var position = 0 ;
198
+ while ( userIds . length !== 0 ) {
199
+ this . getUsers ( [ userIds [ 0 ] ] , props , ebay . _curry ( this [ callbackIdentifier ] , position ) ) ;
200
+ userIds . shift ( ) ;
201
+ position ++ ;
202
+ }
203
+ }
204
+ else {
205
+ ebay . _execute . call ( this , {
206
+ callName : 'GetUserProfile' ,
207
+ 'UserID' : userIds [ 0 ]
208
+ } , props , callback ) ;
209
+ }
210
+ } ;
211
+
212
+ /**
213
+ * Get Categories
214
+ * @param categoryIds[Array] - Array of ids of the categories
215
+ * @param props[Object] - object holding additional request parameters ( so called IncludeSelectors )
216
+ * @param callback[Function] - Reference to callback fn to report this response to. If not specified,
217
+ * the object's callback fn is called.
218
+ */
219
+ ebay . prototype . getCategory = function ( categoryIds , props , callback ) {
220
+ var noOfCategories = categoryIds . length ;
221
+ if ( noOfCategories > 1 ) {
222
+ var finalResponse = [ ] ;
223
+ var callbackCount = 0 ;
224
+ var self = this ;
225
+ var callbackIdentifier = '_tempCallback' + ebay . _getCallId ( ) ;
226
+ this [ callbackIdentifier ] = function ( insertAt , response ) {
227
+ finalResponse [ insertAt ] = response ;
228
+ callbackCount ++ ;
229
+ if ( callbackCount === noOfCategories ) {
230
+ var callback = callback || self . callback ;
231
+ delete self [ callbackIdentifier ] ;
232
+ callback ( finalResponse ) ;
233
+ }
234
+ } ;
235
+
236
+ var position = 0 ;
237
+ while ( categoryIds . length !== 0 ) {
238
+ this . getCategory ( [ categoryIds [ 0 ] ] , props , ebay . _curry ( this [ callbackIdentifier ] , position ) ) ;
239
+ categoryIds . shift ( ) ;
240
+ position ++ ;
241
+ }
242
+ }
243
+ else {
244
+ ebay . _execute . call ( this , {
245
+ callName : 'GetCategoryInfo' ,
246
+ CategoryID : categoryIds [ 0 ]
247
+ } , props , callback ) ;
248
+ }
249
+ } ;
250
+
251
+ /**
252
+ * Get Items
253
+ * @param items[Array] - Array of item ids
254
+ * @param props[Object] - object holding additional request parameters ( so called IncludeSelectors )
255
+ * @param callback[Function] - Reference to callback fn to report this response to. If not specified,
256
+ * the object's callback fn is called.
257
+ */
258
+ ebay . prototype . getItems = function ( items , props , callback ) {
259
+ var multipleItems = ( items . length === 1 ) ? false : true ;
260
+ if ( multipleItems ) {
261
+ //make getMultipleItems call
262
+ var contextObj = {
263
+ callName : 'GetMultipleItems' ,
264
+ 'ItemID' : items . join ( )
265
+ } ;
266
+ }
267
+ else {
268
+ //make getSingleItem call
269
+ var contextObj = {
270
+ callName : 'GetSingleItem' ,
271
+ 'ItemID' : items [ 0 ]
272
+ } ;
273
+ }
274
+ ebay . _execute . call ( this , contextObj , props , callback ) ;
275
+ } ;
276
+
277
+ /**
278
+ * Get Popular Items
279
+ * @param keywords[String] - list of keywords separated by space
280
+ * @param props[Object] - object holding additional request parameters
281
+ * @param callback[Function] - Reference to callback fn to report this response to. If not specified,
282
+ * the object's callback fn is called.
283
+ */
284
+ ebay . prototype . getPopularItems = function ( keywords , props , callback ) {
285
+ ebay . _execute . call ( this , {
286
+ callName : 'FindPopularItems' ,
287
+ 'QueryKeywords' : keywords
288
+ } , props , callback ) ;
289
+ } ;
290
+
291
+ /**
292
+ * Get Search Items from the FINDING API based upon Keywords
293
+ * @param keywords[String] - list of keywords separated by space
294
+ * @param props[Object] - object holding additional request parameters
295
+ * @param callback[Function] - Reference to callback fn to report this response to. If not specified,
296
+ * the object's callback fn is called.
297
+ */
298
+ ebay . prototype . searchByKeywords = function ( keywords , props , callback ) {
299
+ ebay . _execute . call ( this , {
300
+ callName : 'findItemsByKeywords' ,
301
+ 'keywords' : keywords ,
302
+ type : 'FINDING'
303
+ } , props , callback ) ;
304
+ } ;
305
+
306
+ /**
307
+ * Get Items Listings in a particular category from the FINDING API
308
+ * @param categoryId[String] - categoryId
309
+ * @param props[Object] - object holding additional request parameters
310
+ * @param callback[Function] - Reference to callback fn to report this response to. If not specified,
311
+ * the object's callback fn is called.
312
+ */
313
+ ebay . prototype . getItemsInCategory = function ( categoryId , props , callback ) {
314
+ ebay . _execute . call ( this , {
315
+ callName : 'findItemsByCategory' ,
316
+ 'categoryId' : categoryId ,
317
+ type : 'FINDING'
318
+ } , props , callback ) ;
319
+ } ;
320
+
321
+ /**
322
+ * Get Product info
323
+ * @param keywords[String] - keywords for the item
324
+ * @param props[Object] - object holding additional request parameters
325
+ * @param callback[Function] - Reference to callback fn to report this response to. If not specified,
326
+ * the object's callback fn is called.
327
+ */
328
+ ebay . prototype . getProductInfo = function ( keywords , props , callback ) {
329
+ ebay . _execute . call ( this , {
330
+ callName : 'FindProducts' ,
331
+ 'QueryKeywords' : keywords
332
+ } , props , callback ) ;
333
+ } ;
0 commit comments