Skip to content

Commit 1dd2fb9

Browse files
committed
- Add support for user_agent in Purchase
- Add support for images in PurchaseItem and centralize the logic of creating a new image entry that exists in Content - Minor cosmetic fix to add generic type on tags field
1 parent e81dbc6 commit 1dd2fb9

5 files changed

Lines changed: 86 additions & 16 deletions

File tree

src/main/com/sailthru/client/SailthruUtil.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,23 @@ public static Gson createGson() {
5454
.registerTypeHierarchyAdapter(Map.class, new NullSerializingMapTypeAdapter())
5555
.create();
5656
}
57+
58+
/**
59+
* Add a new image entry to the images map.
60+
*
61+
* @param images map containing the references of images. Can be null, in that case the returned map will be a new instance with the entry
62+
* @param key key for the map, either "full" or "thumb"
63+
* @param url url for the image to use
64+
* @return a new map instance of the images parameter was null otherwise the updated map.
65+
*/
66+
public static Map<String, Map<String, String>> putImage(Map<String, Map<String, String>> images, String key, String url) {
67+
if (images == null) {
68+
images = new HashMap<String, Map<String, String>>();
69+
}
70+
Map<String, String> urlMap = new HashMap<String, String>();
71+
urlMap.put("url", url);
72+
images.put(key, urlMap);
73+
return images;
74+
}
75+
5776
}

src/main/com/sailthru/client/params/Content.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import com.google.gson.reflect.TypeToken;
44
import com.sailthru.client.ApiAction;
5+
import com.sailthru.client.SailthruUtil;
6+
57
import java.lang.reflect.Type;
68
import java.util.Date;
7-
import java.util.HashMap;
89
import java.util.Map;
910
import java.util.List;
1011
import java.util.ArrayList;
@@ -21,7 +22,7 @@ public class Content extends AbstractApiParams implements ApiParams {
2122
protected String title;
2223
protected String date;
2324
protected String expire_date;
24-
protected List tags;
25+
protected List<String> tags;
2526
protected Map<String, Object> vars;
2627
protected Map<String, Map<String, String>> images;
2728
protected List<Double> location;
@@ -112,22 +113,12 @@ public Content setImages(Map<String, Map<String, String>> images) {
112113
}
113114

114115
public Content setFullImage(String url) {
115-
if (images == null) {
116-
images = new HashMap<String, Map<String, String>>();
117-
}
118-
Map<String, String> urlMap = new HashMap<String, String>();
119-
urlMap.put("url", url);
120-
images.put("full", urlMap);
116+
this.images = SailthruUtil.putImage(this.images, "full", url);
121117
return this;
122118
}
123119

124120
public Content setThumbImage(String url) {
125-
if (images == null) {
126-
images = new HashMap<String, Map<String, String>>();
127-
}
128-
Map<String, String> urlMap = new HashMap<String, String>();
129-
urlMap.put("url", url);
130-
images.put("thumb", urlMap);
121+
this.images = SailthruUtil.putImage(this.images, "thumb", url);
131122
return this;
132123
}
133124

src/main/com/sailthru/client/params/Purchase.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public enum Channel {app, offline, online}
4848
@SerializedName("device_id")
4949
protected String deviceId;
5050

51+
@SerializedName("user_agent")
52+
protected String userAgent;
53+
5154
public Purchase setEmail(String email) {
5255
this.email = email;
5356
return this;
@@ -153,6 +156,15 @@ public Purchase setDeviceId(String deviceId) {
153156
return this;
154157
}
155158

159+
public String getUserAgent() {
160+
return userAgent;
161+
}
162+
163+
public Purchase setUserAgent(String userAgent) {
164+
this.userAgent = userAgent;
165+
return this;
166+
}
167+
156168
@Override
157169
public ApiAction getApiCall() {
158170
return ApiAction.purchase;

src/main/com/sailthru/client/params/PurchaseItem.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ public class PurchaseItem {
1717
protected String price;
1818
protected String id;
1919
protected String url;
20-
protected List tags;
20+
protected List<String> tags;
2121
protected Map<String, Object> vars;
22+
protected Map<String, Map<String, String>> images;
2223

2324
public PurchaseItem(Integer qty, String title, Integer price, String id, String url) {
2425
this.qty = qty.toString();
@@ -38,6 +39,34 @@ public PurchaseItem setVars(Map<String, Object> vars) {
3839
return this;
3940
}
4041

42+
public Map<String, Map<String, String>> getImages() {
43+
return images;
44+
}
45+
46+
/*
47+
* A map of image names to { “url” : <url> } image maps.
48+
* Use the name “full” to denote the full-sized image, and “thumb” to denote
49+
* the thumbnail-sized image. Other image names are not reserved.
50+
*
51+
* @see #setFullImage(String)
52+
* @see #setThumbImage(String)
53+
*/
54+
public PurchaseItem setImages(Map<String, Map<String, String>> images) {
55+
this.images = images;
56+
return this;
57+
}
58+
59+
public PurchaseItem setFullImage(String url) {
60+
this.images = SailthruUtil.putImage(this.images, "full", url);
61+
return this;
62+
}
63+
64+
public PurchaseItem setThumbImage(String url) {
65+
this.images = SailthruUtil.putImage(this.images, "thumb", url);
66+
return this;
67+
}
68+
69+
4170
public Map<String, Object> toHashMap() {
4271
Gson gson = SailthruUtil.createGson();
4372
String json = gson.toJson(this);

src/test/com/sailthru/client/SailthruUtilTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,31 @@ public void testGson() {
7777
@Test
7878
public void testGsonNull() {
7979
gson = SailthruUtil.createGson();
80-
Map map = new HashMap();
80+
Map<String, Object> map = new HashMap<String, Object>();
8181
map.put("baz", null);
8282

8383
String expected = "{\"baz\":null}";
8484
String result = gson.toJson(map);
8585

8686
assertEquals(expected, result);
8787
}
88+
89+
@Test
90+
public void imagesMapIsUpdated() {
91+
Map<String, Map<String, String>> map = SailthruUtil.putImage(null, "full", "https://something/full.jpg");
92+
assertEquals(1, map.size());
93+
assertEquals("https://something/full.jpg", map.get("full").get("url"));
94+
95+
map = SailthruUtil.putImage(map, "thumb", "https://something/thumb.jpg");
96+
assertEquals(2, map.size());
97+
assertEquals("https://something/thumb.jpg", map.get("thumb").get("url"));
98+
99+
map = SailthruUtil.putImage(map, "custom", "https://something/custom.jpg");
100+
assertEquals(3, map.size());
101+
assertEquals("https://something/custom.jpg", map.get("custom").get("url"));
102+
103+
map = SailthruUtil.putImage(map, "thumb", "https://something/anotherthumb.jpg");
104+
assertEquals(3, map.size());
105+
assertEquals("https://something/anotherthumb.jpg", map.get("thumb").get("url"));
106+
}
88107
}

0 commit comments

Comments
 (0)