Class: App42::Shopping::CatalogueService

Inherits:
Object
  • Object
show all
Defined in:
lib/shopping/CatalogueService.rb

Overview

This Service provides a complete cloud based catalogue management. An app can keep all its items based on category on the Cloud. This service provides several utility methods to manage catalogue on the cloud.

One can add items with its related information in a particular category. And there can be several categories in a catalogue. The App developer can create several catalogues if needed.

The Cart service can be used along with Catalogue service to create an end to end Shopping feature for a Mobile and Web App.

See Also:

  • ItemData

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret_key, base_url) ⇒ CatalogueService

this is a constructor that takes

Parameters:

  • apiKey
  • secretKey
  • baseURL


38
39
40
41
42
43
44
45
# File 'lib/shopping/CatalogueService.rb', line 38

def initialize(api_key, secret_key, base_url)
  puts "Shopping ->initialize"
  @api_key = api_key
  @secret_key = secret_key
  @base_url = base_url
  @resource = "catalogue"
  @version = "1.0"
end

Instance Method Details

#add_item(catalogueName, categoryName, itemData) ⇒ Object

Creates a Item in a Category for a particular Catelogue

Parameters:

  • catalogueName
    • Name of the Catalogue to which item has to be added

  • categoryName
    • Name of the Category to which item has to be added

  • itemData
    • Item Information that has to be added

Returns:

  • Catalogue object containing added item.

Raises:

  • App42Exception

See Also:



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/shopping/CatalogueService.rb', line 168

def add_item(catalogueName, categoryName, itemData)
  puts "Add Item Called "
  puts "Base url #{@base_url}"
  response = nil;
  catalogueObj = nil;
  catalogueObj = Catalogue.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(catalogueName, "CatalogueName");
  util.throwExceptionIfNullOrBlank(categoryName, "CategoryName");
  util.throwExceptionIfNullOrBlank(itemData, "ItemData");
  file = File.new(itemData.image())
  if (FileTest.exists?(file) == false)
    raise App42Exception.new("The file with the name #{file} not found")
  end
  begin
    puts "file is #{file} and #{itemData.itemId}"
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    post_params=Hash.new
    query_params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
      'catalogueName' => catalogueName,
      'categoryName' => categoryName
    }
    params = query_params.clone
    post_params.store("itemId",itemData.itemId)
    post_params.store("name",itemData.name)
    post_params.store("description",itemData.description)
    post_params.store("price",(itemData.price.to_i).to_s)
    params = params.merge(post_params)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{catalogueName}/#{categoryName}/item"
    response = connection.shopMultipart(signature, resource_url, query_params, params, file)
    catalogue = CatalogueResponseBuilder.new()
    catalogueObj = catalogue.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return catalogueObj
end

#create_catalogue(catalogueName, catalogueDescription) ⇒ Object

Creates a Catalogue for a particular App. Categories can be added to the Catalogue

Parameters:

  • catalogueName
    • Name of the Catalogue to be created

  • catalogueDescription
    • Description of the catalogue to be createds

Returns:

  • Catalogue object

Raises:

  • App42Exception



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/shopping/CatalogueService.rb', line 60

def create_catalogue(catalogueName, catalogueDescription)
  puts "Create Catalogue Called "
  puts "Base url #{catalogueDescription}"
  response = nil;
  catalogueObj = nil;
  catalogueObj = Catalogue.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName");
  util.throwExceptionIfNullOrBlank(catalogueDescription, "catalogueDescription");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    body = {'app42' => {"catalogue"=> {
      "name" => catalogueName,
      "description" => catalogueDescription
      }}}.to_json
    puts "Body #{body}"
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("body", body)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}"
    response = connection.post(signature, resource_url, query_params, body)
    catalogue = CatalogueResponseBuilder.new()
    catalogueObj = catalogue.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return catalogueObj
end

#create_category(catalogueName, categoryName, categoryDescription) ⇒ Object

Creates a Category for a particular Catalogue e.g. Books, Music etc.

Parameters:

  • catalogueName
    • Name of the Catalogue for which Category has to be created

  • categoryName
    • Name of the Category that has to be created

  • categoryDescription
    • Description of the category to be created

Returns:

  • Catalogue object containing created category information

Raises:

  • App42Exception



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/shopping/CatalogueService.rb', line 112

def create_category(catalogueName, categoryName, categoryDescription)
  puts "Create Category Called "
  puts "Base url #{@base_url}"
  response = nil;
  catalogueObj = nil;
  catalogueObj = Catalogue.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName");
  util.throwExceptionIfNullOrBlank(categoryName, "categoryName");
  util.throwExceptionIfNullOrBlank(categoryDescription, "categoryDescription");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    body = {'app42' => {"catalogue"=> {"categories"=> {"category" =>
      { "name" => categoryName,
      "description" => categoryDescription,
      }}}}}.to_json
    puts "Body #{body}"
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("body", body)
    params.store("catalogueName", catalogueName)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{catalogueName}/category"
    response = connection.post(signature, resource_url, query_params, body)
    catalogue = CatalogueResponseBuilder.new()
    catalogueObj = catalogue.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return catalogueObj
end

#delete_category(catalogueName, categoryName) ⇒ Object

Deletes category

Parameters:

  • catalogueName
    • Name of the Catalogue from which count of item has to be fetched

  • categoryName
    • Name of the Category from which count of item has to be fetched

Returns:

  • App42Response object

Raises:

  • App42Exception



614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/shopping/CatalogueService.rb', line 614

def delete_category(catalogueName, categoryName)
  puts "deleteCategory Called "
  puts "Base url #{@base_url}"
  response = nil;
  responseObj = App42Response.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(catalogueName, "CatalogueName");
  util.throwExceptionIfNullOrBlank(categoryName, "CategoryName");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("catalogueName", catalogueName);
    params.store("categoryName", categoryName);
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{catalogueName}/category/#{categoryName}"
    response = connection.delete(signature, resource_url, query_params)
    responseObj.strResponse = response
    responseObj.isResponseSuccess = true
  rescue  App42Exception => e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return responseObj
end

#get_item_by_id(catalogueName, categoryName, itemId) ⇒ Object

Fetches Item by id for a Catalogue and Category

Parameters:

  • catalogueName
    • Name of the Catalogue from which item has to be fetched

  • categoryName
    • Name of the Category from which item has to be fetched

  • itemId
    • Item id for which information has to be fetched.

Returns:

  • Catalogue object

Raises:

  • App42Exception



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/shopping/CatalogueService.rb', line 319

def get_item_by_id(catalogueName, categoryName, itemId)
  puts "Get Items By ID Called "
  puts "Base url #{@base_url}"
  response = nil;
  catalogueObj = nil;
  catalogueObj = Catalogue.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName");
  util.throwExceptionIfNullOrBlank(categoryName, "categoryName");
  util.throwExceptionIfNullOrBlank(itemId, "itemId");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    puts params
    params.store("catalogueName", catalogueName)
    params.store("categoryName", categoryName)
    params.store("itemId", itemId)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{catalogueName}/#{categoryName}/#{itemId}"
    response = connection.get(signature, resource_url, query_params)
    catalogue = CatalogueResponseBuilder.new
    catalogueObj = catalogue.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return catalogueObj
end

#get_items(catalogueName) ⇒ Object

Fetches all items for a Catalogue

Parameters:

  • catalogueName
    • Name of the Catalogue from which item has to be fetched

Returns:

  • Catalogue object containing all Items

Raises:

  • App42Exception



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/shopping/CatalogueService.rb', line 224

def get_items(catalogueName)
  puts "Get Items Called "
  puts "Base url #{@base_url}"
  response = nil;
  catalogueObj = nil;
  catalogueObj = Catalogue.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    puts params
    params.store("catalogueName", catalogueName)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{catalogueName}"
    response = connection.get(signature, resource_url, query_params)
    catalogue = CatalogueResponseBuilder.new
    catalogueObj = catalogue.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return catalogueObj
end

#get_items_by_category(catalogueName, categoryName) ⇒ Object

Fetches all items for a Catalogue and Category

Parameters:

  • catalogueName
    • Name of the Catalogue from which item has to be fetched

  • categoryName
    • Name of the Category from which item has to be fetched

Returns:

  • Catalogue object

Raises:

  • App42Exception



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/shopping/CatalogueService.rb', line 270

def get_items_by_category(catalogueName, categoryName)
  puts "Get Items By Category Called "
  puts "Base url #{@base_url}"
  response = nil;
  catalogueObj = nil;
  catalogueObj = Catalogue.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName");
  util.throwExceptionIfNullOrBlank(categoryName, "categoryName");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("catalogueName", catalogueName)
    params.store("categoryName", categoryName)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{catalogueName}/#{categoryName}"
    response = connection.get(signature, resource_url, query_params)
    catalogue = CatalogueResponseBuilder.new
    catalogueObj = catalogue.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return catalogueObj
end

#get_items_by_category_by_paging(catalogueName, categoryName, max, offset) ⇒ Object

Fetches all items for a Catalogue and Category by paging.

Parameters:

  • catalogueName
    • Name of the Catalogue from which item has to be fetched

  • categoryName
    • Name of the Category from which item has to be fetched

  • max
    • Maximum number of records to be fetched

  • offset
    • From where the records are to be fetched

Returns:

  • Catalogue object

Raises:

  • App42Exception



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/shopping/CatalogueService.rb', line 514

def get_items_by_category_by_paging(catalogueName, categoryName, max, offset)
  puts "Get Items By Category Called "
  puts "Base url #{@base_url}"
  response = nil;
  catalogueObj = nil;
  catalogueObj = Catalogue.new
  util = Util.new
  util.validateMax(max);
  util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName");
  util.throwExceptionIfNullOrBlank(categoryName, "categoryName");
  util.throwExceptionIfNullOrBlank(max, "Max");
  util.throwExceptionIfNullOrBlank(offset, "Offset");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("catalogueName", catalogueName)
    params.store("categoryName", categoryName)
    params.store("max", "" + (max.to_i).to_s);
    params.store("offset", "" + (offset.to_i).to_s);
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/paging/#{catalogueName}/#{categoryName}/#{(max.to_i).to_s}/#{(offset.to_i).to_s}"
    response = connection.get(signature, resource_url, query_params)
    catalogue = CatalogueResponseBuilder.new
    catalogueObj = catalogue.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return catalogueObj
end

#get_items_count_by_category(catalogueName, categoryName) ⇒ Object

Fetches count of all items for a Catalogue and Category

Parameters:

  • catalogueName
    • Name of the Catalogue from which count of item has to be fetched

  • categoryName
    • Name of the Category from which count of item has to be fetched

Returns:

  • App42Response object

Raises:

  • App42Exception



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# File 'lib/shopping/CatalogueService.rb', line 566

def get_items_count_by_category(catalogueName, categoryName)
  puts "get_items_count_by_category Called "
  puts "Base url #{@base_url}"
  response = nil;
  responseObj = App42Response.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(catalogueName, "CatalogueName");
  util.throwExceptionIfNullOrBlank(categoryName, "CategoryName");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("catalogueName", catalogueName);
    params.store("categoryName", categoryName);
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{catalogueName}/#{categoryName}/count"
    response = connection.get(signature, resource_url, query_params)
    responseObj.strResponse = response
    responseObj.isResponseSuccess = true
    responseObj = CatalogueResponseBuilder.new()
    responseObj.getTotalRecords(response);
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return responseObj
end

#remove_all_items(catalogueName) ⇒ Object

Removes all Items in a Catalogue

Parameters:

  • catalogueName
    • Name of the Catalogue from which item has to be removed

Returns:

  • Catalogue object containing removed items

Raises:

  • App42Exception



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/shopping/CatalogueService.rb', line 367

def remove_all_items(catalogueName)
  puts "Remove All Items Called "
  puts "Base url #{@base_url}"
  response = nil;
  responseObj = App42Response.new();
  util = Util.new
  util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    puts params
    params.store("catalogueName", catalogueName)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{catalogueName}"
    response = connection.delete(signature, resource_url, query_params)
    responseObj.strResponse=(response)
    responseObj.isResponseSuccess=(true)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return responseObj
end

#remove_item_by_id(catalogueName, categoryName, itemId) ⇒ Object

Removes Item by Id

Parameters:

  • catalogueName
    • Name of the Catalogue from which item has to be removed

  • categoryName
    • Name of the Category from which item has to be removed

  • itemId
    • Item id which has to be removed

Returns:

  • Catalogue object containing removed item

Raises:

  • App42Exception



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/shopping/CatalogueService.rb', line 461

def remove_item_by_id(catalogueName, categoryName, itemId)
  puts "Remove Items By ID Called "
  puts "Base url #{@base_url}"
  response = nil;
  responseObj = App42Response.new();
  util = Util.new
  util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName");
  util.throwExceptionIfNullOrBlank(categoryName, "categoryName");
  util.throwExceptionIfNullOrBlank(itemId, "itemId");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    puts params
    params.store("catalogueName", catalogueName)
    params.store("categoryName", categoryName)
    params.store("itemId", itemId)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{catalogueName}/#{categoryName}/#{itemId}"
    response = connection.delete(signature, resource_url, query_params)
    responseObj.strResponse=(response)
    responseObj.isResponseSuccess=(true)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return responseObj
end

#remove_items_by_category(catalogueName, categoryName) ⇒ Object

Removes all Items from a Catalogue and Category

Parameters:

  • catalogueName
    • Name of the Catalogue from which item has to be removed

  • categoryName
    • Name of the Category from which item has to be removed

Returns:

  • Catalogue object containing removed item

Raises:

  • App42Exception



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/shopping/CatalogueService.rb', line 412

def remove_items_by_category(catalogueName, categoryName)
  puts "Remove Items By Category Called "
  puts "Base url #{@base_url}"
  response = nil;
  responseObj = App42Response.new();
  util = Util.new
  util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName");
  util.throwExceptionIfNullOrBlank(categoryName, "categoryName");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    puts params
    params.store("catalogueName", catalogueName)
    params.store("categoryName", categoryName)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{catalogueName}/#{categoryName}"
    response = connection.delete(signature, resource_url, query_params)
    responseObj.strResponse=(response)
    responseObj.isResponseSuccess=(true)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return responseObj
end