Skip to content

Commit

Permalink
Revert "Updated library"
Browse files Browse the repository at this point in the history
This reverts commit 4c9e001.
  • Loading branch information
ZapZapGo committed May 21, 2013
1 parent 4c9e001 commit 2b0f60f
Show file tree
Hide file tree
Showing 15 changed files with 793 additions and 6 deletions.
25 changes: 20 additions & 5 deletions LICENSE.md
@@ -1,6 +1,21 @@
MIT License
The MIT License

Copyright (C) 2013 Robin Orheden
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Copyright (c) Robin Orheden, 2013 <http://amail.io/>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
2 changes: 1 addition & 1 deletion README.md
@@ -1,4 +1,4 @@
Comfirm AlphaMail Node.js Client
AlphaMail Node.js Client
================================
Use this library to connect your Node.js application to the AlphaMail system.
Start sending transactional e-mail today without the hassle of SMTP and large blocks of unmaintainable HTML.
Expand Down
86 changes: 86 additions & 0 deletions examples/advanced.js
@@ -0,0 +1,86 @@
/*
The MIT License
Copyright (c) Robin Orheden, 2013 <http://amail.io/>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// In doubt or experiencing problems?
// Please email our support at 'support@comfirm.se'

var alphamail = require('alphamail');

// Step #1: Let's start by entering the web service URL and the API-token you've been provided
// If you haven't gotten your API-token yet. Log into AlphaMail or contact support at 'support@comfirm.se'.
var emailService = new alphamail.EmailService()
.setServiceUrl("http://api.amail.io/v2/")
.setApiToken("YOUR-ACCOUNT-API-TOKEN-HERE");

// Step #2: Let's create a message with the data we want to use in our template
var message = {
// Represents the <# payload.message #> in our template
message: "Hello world like a boss!",
// Represents the <# payload.some_other_message #> in our template
some_other_message: "And to the rest of the world! Chíkmàa! مرحبا! नमस्ते! Dumelang!"
};

// Step #3: Let's set up everything that is specific for delivering this email
var payload = new alphamail.EmailMessagePayload()
.setProjectId(12345) // The id of the project your want to send with
.setSender(new alphamail.EmailContact("Sender Company Name", "your-sender-email@your-sender-domain.com"))
.setReceiver(new alphamail.EmailContact("Joe E. Receiver", "email-of-receiver@comfirm.se"))
.setBodyObject(message);

// Step #4: Haven't we waited long enough. Let's send this!
emailService.queue(payload, {
// Success!
onSuccess: function(queueId){
// Step #5: Pop the champagné! We got here which mean that the request was sent successfully and the email is on it's way!
console.log("Successfully queued message with id '" + queueId + "' (you can use this ID to get more details about the delivery)");
},
// Oh heck. Something went wrong. But don't stop here.
// If you haven't solved it yourself. Just contact our brilliant support and they will help you.
// Validation error
onValidationError: function(errorCode, message){
// Example: Handle request specific error code here
if (errorCode == 3){
// Example: Print a nice message to the user.
}else{
// Something in the input was wrong. Probably good to double double-check!
console.log("Validation error: " + message + " (" + errorCode + ")");
}
},
// Authentication error
onAuthenticationError: function(errorCode, message){
// Ooops! You've probably just entered the wrong API-token.
console.log("Authentication error: " + message + " (" + errorCode + ")");
},
// Service error
onInternalError: function(errorCode, message){
// Not that it is going to happen.. Right :-)
console.log("Internal error: " + message + " (" + errorCode + ")");
},
// Connection error
onConnectionError: function(message){
// Most likely your internet connection that is down. We are covered for most things except "multi-data-center-angry-server-bashing-monkeys" (remember who coined it) or.. nuclear bombs.
// If one blew. Well.. It's just likely that our servers are down.
console.log("An error (probably related to connection) occurred: " + message);
}
});
53 changes: 53 additions & 0 deletions examples/simple.js
@@ -0,0 +1,53 @@
/*
The MIT License
Copyright (c) Robin Orheden, 2013 <http://amail.io/>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// In doubt or experiencing problems?
// Please email our support at 'support@comfirm.se'

// If missing, run # npm install alphamail
var alphamail = require('alphamail');
var emailService = new alphamail.EmailService("YOUR-ACCOUNT-API-TOKEN-HERE");

var message = {
user: {
id: 12345,
username: "jdoe75",
firstName: "John",
lastName: "Doe"
}
};

var payload = new alphamail.EmailMessagePayload()
.setProjectId(12345) // ID of your project
.setSender(new alphamail.EmailContact("My Company", "your@domain.com"))
.setReceiver(new alphamail.EmailContact("Some dude", "receiver@some55-domain.com"))
.setBodyObject(message);

emailService.queue(payload, function(error, result){
if(error){
console.log(error);
}else{
console.log("Mail successfully sent! ID = " + result);
}
});
1 change: 1 addition & 0 deletions src/index.js
@@ -0,0 +1 @@
exports = module.exports = require('./lib/alphamail');
75 changes: 75 additions & 0 deletions src/lib/alphamail/alphamailrestclient.js
@@ -0,0 +1,75 @@
var common = require('./common').Common,
restclient = require('./restclient');

var AlphaMailRestClient = exports.AlphaMailRestClient = function(token){
this.options = {};
this.client = new restclient.RestClient();
this.setServiceUrl("http://api.amail.io/v2/");
this.setApiToken(token);
};

AlphaMailRestClient.prototype.setServiceUrl = function(base_url){
this.client.setBaseUrl(base_url);
return this;
};

AlphaMailRestClient.prototype.setApiToken = function(token){
this.client.setBasicAuthentication(null, token);
return this;
};

AlphaMailRestClient.prototype.get = function(path, events){
var outerScope = this;
events = common.resolveEventObject(events);

this.client.get(path, function(response, data){
common.handleResponse(response.statusCode, data, events.onSuccess, events.onValidationError,
events.onAuthenticationError || outerScope.options.on_authentication_error,
events.onServiceError || outerScope.options.on_service_error);
}, events.onConnectionError);

return this;
};

AlphaMailRestClient.prototype.put = function(path, bodyObject, events){
var outerScope = this;

bodyObject = bodyObject || {};
events = common.resolveEventObject(events);

this.client.put(path, bodyObject, function(response, data){
common.handleResponse(response.statusCode, data, events.onSuccess, events.onValidationError,
events.onAuthenticationError || outerScope.options.on_authentication_error,
events.onServiceError || outerScope.options.on_service_error);
}, events.onConnectionError);

return this;
};

AlphaMailRestClient.prototype.post = function(path, bodyObject, events){
var outerScope = this;

bodyObject = bodyObject || {};
events = common.resolveEventObject(events);

this.client.post(path, bodyObject, function(response, data){
common.handleResponse(response.statusCode, data, events.onSuccess, events.onValidationError,
events.onAuthenticationError || outerScope.options.on_authentication_error,
events.onServiceError || outerScope.options.on_service_error);
}, events.onConnectionError);

return this;
};

AlphaMailRestClient.prototype.delete = function(path, events){
var outerScope = this;
events = common.resolveEventObject(events);

this.client.delete(path, function(response, data){
common.handleResponse(response.statusCode, data, events.onSuccess, events.onValidationError,
events.onAuthenticationError || outerScope.options.on_authentication_error,
events.onServiceError || outerScope.options.on_service_error);
}, events.onConnectionError);

return this;
};
70 changes: 70 additions & 0 deletions src/lib/alphamail/common.js
@@ -0,0 +1,70 @@
var Common = exports.Common = {};

Common.resolveEventObject = function(value){
var result = null;

if(value == null){
result = {};
}else if (typeof(value) == 'function') {
var errorCallback = function(errorCode, message){
var error = new Error(message);
error.code = errorCode; // Attach code
value(error);
};

result = {
onSuccess: function(result, message){
value(false, result);
},
onValidationError: errorCallback,
onAuthenticationError: errorCallback,
onServiceError: errorCallback,
onConnectionError: errorCallback
};
}else{
result = value;
}

return result;
};

Common.handleResponse = function(statusCode, data, onSuccess, onValidationError, onAuthenticationError, onServiceError){
switch(statusCode){
case 200: case 201: case 202:
// Success
if(data.error_code == 0){
if(onSuccess){
onSuccess(data.result, data.message);
}
}else{
if(onServiceError){
onServiceError(data.error_code, data.message);
}
}
break;
case 401: case 403:
// Unauthorized
if(onAuthenticationError){
onAuthenticationError(data.error_code, data.message);
}
break;
case 400: case 405:
// Bad request (due to validation)
if(onValidationError){
onValidationError(data.error_code, data.message);
}
break;
case 500:
// AlphaMailInternalException
if(onServiceError){
onServiceError(data);
}
break;
default:
// AlphaMailServiceException
if(onServiceError){
onServiceError(data);
}
break;
}
};

0 comments on commit 2b0f60f

Please sign in to comment.