Skip to content

Commit 7fd8391

Browse files
committed
update commerceitem to allow optional fields
1 parent 95334be commit 7fd8391

File tree

4 files changed

+119
-34
lines changed

4 files changed

+119
-34
lines changed

swift-sdk/CommerceItem.swift

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,100 @@
44

55
import UIKit
66

7-
/**
8-
`CommerceItem` represents a product. These are used by the commerce API; see [IterableAPI trackPurchase:items:dataFields:]
9-
*/
7+
/// `CommerceItem` represents a product
8+
/// - SeeAlso: IterableAPI.track(purchase withTotal: items:)
109
@objcMembers public class CommerceItem: NSObject {
11-
/** id of this product */
10+
/// id of this product
1211
public var id: String
1312

14-
/** name of this product */
13+
/// name of this product
1514
public var name: String
1615

17-
/** price of this product */
16+
/// price of this product
1817
public var price: NSNumber
1918

20-
/** quantity of this product */
19+
/// quantity of this product
2120
public var quantity: UInt
2221

23-
/**
24-
Creates a `CommerceItem` with the specified properties
25-
26-
- parameters:
27-
- id: id of the product
28-
- name: name of the product
29-
- price: price of the product
30-
- quantity: quantity of the product
31-
32-
- returns: an instance of `CommerceItem` with the specified properties
33-
*/
34-
public init(id: String, name: String, price: NSNumber, quantity: UInt) {
22+
/// SKU of this product
23+
public var sku: String?
24+
25+
/// description of the product
26+
public var itemDescription: String?
27+
28+
/// URL of the product
29+
public var url: String?
30+
31+
/// URL of the product's image
32+
public var imageUrl: String?
33+
34+
/// categories this product belongs to
35+
/// each category is a breadcrumb in list form
36+
public var categories: [String]?
37+
38+
/// Creates a `CommerceItem` with the specified properties
39+
///
40+
/// - Parameters:
41+
/// - id: id of the product
42+
/// - name: name of the product
43+
/// - price: price of the product
44+
/// - quantity: quantity of the product
45+
/// - sku: SKU of the eproduct
46+
/// - description: description of the product
47+
/// - url: URL of the product
48+
/// - imageUrl: URL of the product's image
49+
/// - categories: categories this product belongs to
50+
///
51+
/// - returns: an instance of `CommerceItem` with the specified properties
52+
public init(id: String,
53+
name: String,
54+
price: NSNumber,
55+
quantity: UInt,
56+
sku: String? = nil,
57+
itemDescription: String? = nil,
58+
url: String? = nil,
59+
imageUrl: String? = nil,
60+
categories: [String]? = nil) {
3561
self.id = id
3662
self.name = name
3763
self.price = price
3864
self.quantity = quantity
65+
self.sku = sku
66+
self.itemDescription = itemDescription
67+
self.url = url
68+
self.imageUrl = imageUrl
69+
self.categories = categories
3970
}
4071

41-
/**
42-
A Dictionary respresentation of this item
43-
44-
- returns: An NSDictionary representing this item
45-
*/
72+
/// A `Dictionary` representation of this item
73+
///
74+
/// - returns: An `Dictionary` representing this item
4675
public func toDictionary() -> [AnyHashable: Any] {
47-
["id": id,
48-
"name": name,
49-
"price": price,
50-
"quantity": quantity]
76+
var dictionary: [AnyHashable: Any] = [JsonKey.CommerceItem.id: id,
77+
JsonKey.CommerceItem.name: name,
78+
JsonKey.CommerceItem.price: price,
79+
JsonKey.CommerceItem.quantity: quantity]
80+
81+
if let sku = sku {
82+
dictionary[JsonKey.CommerceItem.sku] = sku
83+
}
84+
85+
if let description = itemDescription {
86+
dictionary[JsonKey.CommerceItem.description] = description
87+
}
88+
89+
if let url = url {
90+
dictionary[JsonKey.CommerceItem.url] = url
91+
}
92+
93+
if let imageUrl = imageUrl {
94+
dictionary[JsonKey.CommerceItem.imageUrl] = imageUrl
95+
}
96+
97+
if let categories = categories {
98+
dictionary[JsonKey.CommerceItem.categories] = categories
99+
}
100+
101+
return dictionary
51102
}
52103
}

swift-sdk/Constants.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,18 @@ enum JsonKey {
159159
static let user = "user"
160160
}
161161

162+
enum CommerceItem {
163+
static let id = "id"
164+
static let name = "name"
165+
static let price = "price"
166+
static let quantity = "quantity"
167+
static let sku = "sku"
168+
static let description = "description"
169+
static let imageUrl = "imageUrl"
170+
static let url = "url"
171+
static let categories = "categories"
172+
}
173+
162174
enum Device {
163175
static let localizedModel = "localizedModel"
164176
static let vendorId = "identifierForVendor"

swift-sdk/Internal/RequestCreator.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,8 @@ struct RequestCreator {
8989
ITBError("Both email and userId are nil")
9090
return .failure(IterableError.general(description: "Both email and userId are nil"))
9191
}
92-
93-
var itemsToSerialize = [[AnyHashable: Any]]()
9492

95-
for item in items {
96-
itemsToSerialize.append(item.toDictionary())
97-
}
93+
let itemsToSerialize = items.map { $0.toDictionary() }
9894

9995
var apiUserDict = [AnyHashable: Any]()
10096

tests/unit-tests/CommerceItemTests.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import XCTest
77
@testable import IterableSDK
88

99
class CommerceItemTests: XCTestCase {
10-
func testToDictionary() {
10+
func testToDictionaryWithRequiredFields() {
1111
let commerceItemDictionary = CommerceItem(id: "commerceItemId", name: "commerceItemName", price: 6, quantity: 9000).toDictionary()
1212

1313
let expected: [AnyHashable: Any] = ["id": "commerceItemId",
@@ -17,4 +17,30 @@ class CommerceItemTests: XCTestCase {
1717

1818
XCTAssertEqual(NSDictionary(dictionary: commerceItemDictionary), NSDictionary(dictionary: expected))
1919
}
20+
21+
func testToDictionaryWithAllFields() {
22+
let itemDictionary = CommerceItem(id: "THINKTANK",
23+
name: "Tachikoma",
24+
price: 7.62,
25+
quantity: 9,
26+
sku: "kusanagi",
27+
itemDescription: "spider type multi leg/multi ped combat vehicle equipped with AI",
28+
url: "stand-alone-complex",
29+
imageUrl: "laughing-man",
30+
categories: ["section 9",
31+
"personnel transport"]).toDictionary()
32+
33+
let expected: [AnyHashable: Any] = ["id": "THINKTANK",
34+
"name": "Tachikoma",
35+
"price": 7.62,
36+
"quantity": 9,
37+
"sku": "kusanagi",
38+
"description": "spider type multi leg/multi ped combat vehicle equipped with AI",
39+
"url": "stand-alone-complex",
40+
"imageUrl": "laughing-man",
41+
"categories": ["section 9",
42+
"personnel transport"]]
43+
44+
XCTAssertEqual(NSDictionary(dictionary: itemDictionary), NSDictionary(dictionary: expected))
45+
}
2046
}

0 commit comments

Comments
 (0)