Skip to content

Commit

Permalink
code review - refactor check for email and userId.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtruong committed Oct 13, 2016
1 parent 3fe68f7 commit 3a395dd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 95 deletions.
58 changes: 0 additions & 58 deletions .idea/misc.xml

This file was deleted.

64 changes: 27 additions & 37 deletions iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,7 @@ public void track(String eventName, String campaignId, String templateId) {
public void track(String eventName, String campaignId, String templateId, JSONObject dataFields) {
JSONObject requestJSON = new JSONObject();
try {
if (_email != null) {
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
} else {
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
}
addEmailOrUserIdToJson(requestJSON);
requestJSON.put(IterableConstants.KEY_EVENTNAME, eventName);

requestJSON.put(IterableConstants.KEY_CAMPAIGNID, campaignId);
Expand Down Expand Up @@ -342,19 +338,19 @@ public void sendPush(String email, int campaignId, Date sendAt, JSONObject dataF
}

public void updateEmail(String newEmail) {
JSONObject requestJSON = new JSONObject();
if (_email != null) {
JSONObject requestJSON = new JSONObject();

try {
requestJSON.put(IterableConstants.KEY_CURRENT_EMAIL, _email);
requestJSON.put(IterableConstants.KEY_NEW_EMAIL, newEmail);
}
catch (JSONException e) {
e.printStackTrace();
}
try {
requestJSON.put(IterableConstants.KEY_CURRENT_EMAIL, _email);
requestJSON.put(IterableConstants.KEY_NEW_EMAIL, newEmail);
}
catch (JSONException e) {
e.printStackTrace();
}

sendRequest(IterableConstants.ENDPOINT_UPDATEEMAIL, requestJSON);
sendRequest(IterableConstants.ENDPOINT_UPDATEEMAIL, requestJSON);

if (_email != null) {
_email = newEmail;
} else {
IterableLogger.w(TAG, "updateEmail should not be called with a userId. " +
Expand All @@ -366,12 +362,7 @@ public void updateUser(JSONObject dataFields) {
JSONObject requestJSON = new JSONObject();

try {
if (_email != null) {
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
} else {
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
}

addEmailOrUserIdToJson(requestJSON);
requestJSON.put(IterableConstants.KEY_DATAFIELDS, dataFields);
}
catch (JSONException e) {
Expand Down Expand Up @@ -425,11 +416,7 @@ protected void trackPushOpen(int campaignId, int templateId, String messageId) {
JSONObject requestJSON = new JSONObject();

try {
if (_email != null) {
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
} else {
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
}
addEmailOrUserIdToJson(requestJSON);
requestJSON.put(IterableConstants.KEY_CAMPAIGNID, campaignId);
requestJSON.put(IterableConstants.KEY_TEMPLATE_ID, templateId);
requestJSON.put(IterableConstants.KEY_MESSAGE_ID, messageId);
Expand All @@ -448,12 +435,7 @@ protected void trackPushOpen(int campaignId, int templateId, String messageId) {
protected void disablePush(String token) {
JSONObject requestJSON = new JSONObject();
try {
requestJSON.put(IterableConstants.KEY_TOKEN, token);
if (_email != null) {
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
} else {
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
}
requestJSON.put(IterableConstants.KEY_TOKEN, token);addEmailOrUserIdToJson(requestJSON);
}
catch (JSONException e) {
e.printStackTrace();
Expand Down Expand Up @@ -495,11 +477,7 @@ private void registerDeviceToken(String applicationName, String token, JSONObjec

JSONObject requestJSON = new JSONObject();
try {
if (_email != null) {
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
} else {
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
}
addEmailOrUserIdToJson(requestJSON);

if (dataFields == null) {
dataFields = new JSONObject();
Expand Down Expand Up @@ -529,6 +507,18 @@ private void sendRequest(String resourcePath, JSONObject json) {
new IterableRequest().execute(request);
}

private void addEmailOrUserIdToJson(JSONObject requestJSON) {
try {
if (_email != null) {
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
} else {
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
}
} catch (JSONException e) {
e.printStackTrace();
}
}

//---------------------------------------------------------------------------------------
//endregion

Expand Down

0 comments on commit 3a395dd

Please sign in to comment.