Skip to content

Commit

Permalink
Added newsletter subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
benlowry committed May 21, 2013
1 parent b63438b commit 3d677af
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 96 deletions.
217 changes: 135 additions & 82 deletions Playtomic/.idea/workspace.xml

Large diffs are not rendered by default.

Binary file modified Playtomic/out/production/Playtomic/Playtomic.apk
Binary file not shown.
Binary file modified Playtomic/out/production/Playtomic/Playtomic.unaligned.apk
Binary file not shown.
Binary file not shown.
29 changes: 29 additions & 0 deletions Playtomic/src/com/playtomic/android/Newsletter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.playtomic.android;

import org.json.JSONObject;

public class Newsletter {

private static String SECTION = "newsletter";
private static String SUBSCRIBE = "subscribe";

/**
* Subscribes a person to your newsletter
* @param options JSONObject with email and other fields you configure
* @param callback NewsletterSubscribeHandler for receiving the response
*/
public static void subscribe(JSONObject options, final NewsletterSubscribeHandler callback) {
PRequest.load(SECTION, SUBSCRIBE, options, new PResponseHandler() {

@Override
public void onResponse(PResponse response, JSONObject data) {
if(response.getSuccess()) {
callback.onSuccess(response);
} else {
callback.onFailure(response);
}
}

});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.playtomic.android;

public interface NewsletterSubscribeHandler {
void onSuccess(PResponse response);
void onFailure(PResponse response);
}
38 changes: 38 additions & 0 deletions Playtomic/src/com/playtomic/android/NewsletterSubscription.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.playtomic.android;
import org.json.JSONObject;
import org.json.JSONException;

public class NewsletterSubscription extends JSONObject {

public void set(String name, Object value) {
try {
put(name, value);
} catch(JSONException err) {

}
}

public void setEmail(String email) {
set("email", email);
}

public void setFirstName(String firstname) {
set("firstname", firstname);
}

public void setLastName(String lastname) {
set("lastname", lastname);
}

public void setField(String name, Object value) {
if(!has("fields")) {
set("fields", new JSONObject());
}

try {
optJSONObject("fields").put(name, value);
} catch(JSONException err) {

}
}
}
36 changes: 22 additions & 14 deletions Playtomic/src/com/playtomic/android/PResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,32 @@ public String getErrorMessage() {
switch(errorcode)
{
case 1:
return "General error, this typically means the player is unable to connect to the server.";
return "General error, this typically means the player is unable to connect to the server";
case 2:
return "Invalid game credentials. Make sure you use the right public and private keys.";
return "Invalid game credentials. Make sure you use the right public and private keys";
case 3:
return "Request timed out.";
return "Request timed out";
case 4:
return "Invalid request.";
return "Invalid request";

case 100:
return "GeoIP API has been disabled for this game.";
return "GeoIP API has been disabled for this game";

case 200:
return "Leaderboard API has been disabled for this game.";
return "Leaderboard API has been disabled for this game";
case 201:
return "The player's name wasn't provided.";
return "The player's name wasn't provided";
case 203:
return "Player is banned from submitting scores in this game.";
return "Player is banned from submitting scores in this game";
case 204:
return "Score was not saved because it was not the player's best. You can allow players to have " +
"more than one score by specifying allowduplicates=true in your save options.";
return "Score was not saved because it was not the player's best, you can allow players to have " +
"more than one score by specifying allowduplicates=true in your save options";

case 300:
return "GameVars API has been disabled for this game.";
return "GameVars API has been disabled for this game";

case 400:
return "Level sharing API has been disabled for this game.";
return "Level sharing API has been disabled for this game";
case 401:
return "Invalid rating (must be 1 - 10)";
case 402:
Expand All @@ -79,7 +79,7 @@ public String getErrorMessage() {
return "Level already exists";

case 500:
return "Achievements API has been disabled for this game.";
return "Achievements API has been disabled for this game";
case 501:
return "Missing playerid";
case 502:
Expand All @@ -89,9 +89,17 @@ public String getErrorMessage() {
case 504:
return "Invalid achievementid or achievement key";
case 505:
return "Player already had the achievement. You can overwrite old achievements with overwrite=true or save each time the player is awarded with allowduplicates=true";
return "Player already had the achievement, you can overwrite old achievements with overwrite=true or " +
"save each time the player is awarded with allowduplicates=true";
case 506:
return "Player already had the achievement and it was overwritten or a duplicate was saved successfully";

case 600:
return "Newsletter API has been disabled for this game";
case 601:
return "MailChimp API Key has not been configured";
case 602:
return "MailChimp API returned an error";
}

return "Unknown error...";
Expand Down
50 changes: 50 additions & 0 deletions Playtomic/src/com/playtomic/tests/PTestNewsletter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.playtomic.tests;

import com.playtomic.android.Newsletter;
import com.playtomic.android.NewsletterSubscribeHandler;
import com.playtomic.android.NewsletterSubscription;
import com.playtomic.android.PResponse;

public class PTestNewsletter extends PTest {

public static void subscribe(final TestHandler callback) {

final String section = "TestNewsletter.subscribe";

final NewsletterSubscription options = new NewsletterSubscription();
options.setEmail("invalid @email.com");

Newsletter.subscribe(options, new NewsletterSubscribeHandler() {

@Override
public void onFailure(PResponse r) {
assertFalse(section, "Request failed", r.getSuccess());
assertEquals(section, "No errorcode", r.getErrorCode(), 602);

options.setEmail("valid@testuri.com");
options.setField("STRINGFIELD", "a value");

Newsletter.subscribe(options, new NewsletterSubscribeHandler() {

@Override
public void onSuccess(PResponse r2) {
assertTrue(section + "#2", "Request succeeded", r2.getSuccess());
assertEquals(section + "#2", "No errorcode", r2.getErrorCode(), 0);
callback.done();
}
@Override
public void onFailure(PResponse response) {
fail(section, "Request failed with errorcode " + response.getErrorCode());
callback.done();
}
});
}

@Override
public void onSuccess(PResponse response) {
fail(section, "Request failed with errorcode " + response.getErrorCode());
callback.done();
}
});
}
}

0 comments on commit 3d677af

Please sign in to comment.