Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-olczyk committed Jul 13, 2015
0 parents commit 8c18607
Show file tree
Hide file tree
Showing 58 changed files with 3,006 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
@@ -0,0 +1,11 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
tab_width = 4
charset = UTF-8
trim_trailing_whitespace = true
insert_final_newline = true
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
.idea
node_modules
test/config.dist.js
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,4 @@
# SMSAPI JavaScript Client

## 1.0.0 - XXXX-XX-XX
* initial commit
13 changes: 13 additions & 0 deletions LICENSE
@@ -0,0 +1,13 @@
Copyright 2015 SMSAPI

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
106 changes: 106 additions & 0 deletions README.md
@@ -0,0 +1,106 @@
# SMSAPI JavaScript Client

Klient JavaScript pozwalający na wysyłanie wiadomości SMS, MMS, VMS oraz zarządzanie kontem w serwisie SMSAPI.pl

## Instalacja (node.js)

```bash

$ npm install smsapi --save

```

## Przykład użycia

```javascript

var SMSAPI = require('smsapi'),
smsapi = new SMSAPI();

smsapi.authentication
.login('username', 'password')
.then(sendEcoMessage)
.then(displayResult)
.catch(displayError);

function sendEcoMessage(){
return smsapi.message
.sms()
.eco()
.to('605xxxxxx')
.test()
.message('My first message!')
.execute(); // return Promise
}

function displayResult(result){
console.log(result);
}

function displayError(err){
console.error(err);
}

```

# Dokumentacja

Dokumentacja interfejsu REST API znajduje się pod adresem [http://www.smsapi.pl/rest](http://www.smsapi.pl/rest).

Wszystkie odwołania do API zwracają obiekt `Promise` zgodny ze standardem [Promises/A+](https://promisesaplus.com).

Wykorzystywana implementacja: https://github.com/tildeio/rsvp.js

## Dostępne operacje

* message
* sms
* mms
* vms
* phonebook
* contact
* add
* get
* update
* list
* delete
* group
* get
* add
* update
* list
* delete
* points
* get
* sender
* add
* delete
* status
* default
* list
* hlr
* check
* user
* add
* delete
* update
* get
* list

## Przykłady

Dodatkowe przykłady użycia dostępnych operacji można znaleźć w testach (./test).

## Testy

```bash

$ npm install mocha -g
$ npm install .
$ npm test

```

# Licencja

[Apache 2.0 License](LICENSE)
4 changes: 4 additions & 0 deletions index.js
@@ -0,0 +1,4 @@

var SMSAPI = require('./lib/smsapi.js');

module.exports = SMSAPI;
79 changes: 79 additions & 0 deletions lib/action-abstract.js
@@ -0,0 +1,79 @@

var _ = require('underscore')._;

/**
* @param {Object} options
* @param {ProxyAbstract} options.proxy
* @param {Object} [params]
*/
function ActionAbstract(options, params){
this._options = options || {};
this._params = _.extend({}, this.defaultParams, params);
this._proxy = options.proxy;
}

/**
* action's default params
* @type {Object}
*/
ActionAbstract.prototype.defaultParams = {};

/**
* @return {ProxyAbstract}
*/
ActionAbstract.prototype.request = function(){
if (_.isUndefined(this._proxy))
throw new Error('Proxy has been not defined');

return this._proxy.request();
};

/**
* set custom param for the request
* @param {String} name
* @param {String} value
* @returns {ActionAbstract} this
*/
ActionAbstract.prototype.param = function(name, value){
if (_.isUndefined(value))
return this._params[name];
else
this._params[name] = value;

return this;
};

/**
* set multiple params at once
* will extend (not overwrite!) current params
* @param {Object} params
* @returns {ActionAbstract} this
*/
ActionAbstract.prototype.params = function(params){
if (_.isUndefined(params))
return this._params;
else
this._params = _.extend(this._params, params);

return this;
};

/**
* clear all request params
* @returns {ActionAbstract}
*/
ActionAbstract.prototype.clear = function(){
this._params = _.clone(this.defaultParams);

return this;
};

/**
* should execute action and return Promise
* @returns {Promise}
*/
ActionAbstract.prototype.execute = function(){
throw new Error('Function .execute() not implemented');
};

module.exports = ActionAbstract;
20 changes: 20 additions & 0 deletions lib/action-factory-abstract.js
@@ -0,0 +1,20 @@
var _ = require('underscore')._;

/**
* @param {Object} options
*/
function ActionFactoryAbstract(options){
this._options = options || {};
}

/**
* create and return action with options saved in the constructor
* @param {Object} Action
* @param {Object} params
* @return {ActionAbstract}
*/
ActionFactoryAbstract.prototype.createAction = function(Action, params){
return new Action(this._options, params);
};

module.exports = ActionFactoryAbstract;
60 changes: 60 additions & 0 deletions lib/authentication-abstract.js
@@ -0,0 +1,60 @@
var _ = require('underscore')._;

/**
* @param {Object} [options]
*/
function AuthenticationAbstract(options){
options = options || {};
this._proxy = options.proxy;
}

/**
* [proxy description]
* @param {[type]} proxy [description]
* @return {[type]} [description]
*/
AuthenticationAbstract.prototype.proxy = function(proxy){
if (_.isUndefined(proxy))
return this._proxy;

this._proxy = proxy;
return this;
};

/**
* should return object with required
* POST params for the request
* @return {Object}
*/
AuthenticationAbstract.prototype.getPOSTParams = function(){
return {};
};

/**
* should return object with required
* GET params for the request
* @return {Object}
*/
AuthenticationAbstract.prototype.getGETParams = function(){
return {};
};

/**
* should login the user
* @param {String} login
* @param {String} password
* @return {Promise}
*/
AuthenticationAbstract.prototype.login = function(login, password){
throw new Error('Not implemented');
};

/**
* should logout the user
* @return {AuthenticationAbstract} this
*/
AuthenticationAbstract.prototype.logout = function(){
throw new Error('Not implemented');
};

module.exports = AuthenticationAbstract;
66 changes: 66 additions & 0 deletions lib/authentication-simple.js
@@ -0,0 +1,66 @@

var md5 = require('MD5'),
_ = require('underscore')._,
Promise = require('rsvp').Promise,
AuthenticationAbstract = require('./authentication-abstract.js');

/**
* simple, using login and password, athentication
* when used every request to the api will be altered
* and login and password params will be added respectively
* @param {Object} options
*/
function AuthenticationSimple(options){
AuthenticationAbstract.call(this, options);
this._username = null;
this._password = null;
}

AuthenticationSimple.prototype = Object.create(AuthenticationAbstract.prototype);

/**
* perform login
* WARNING:
* credentials are not validated and user is not
* authenticated here (due to technical reasons)
* it means that login action can end with success, but user
* will not be able to retrieve data from api
* @param {String} username
* @param {String} password
* @return {Promise} will resolve instantly (only deferred)
*/
AuthenticationSimple.prototype.login = function(username, password){
this._username = username;
this._password = md5(password);

return new Promise(function(resolve, reject){
// defer
if (username && password)
setTimeout(resolve, 0);
else
setTimeout(reject, 0);
});
};

AuthenticationSimple.prototype.getGETParams = function(){
return {
username: this._username,
password: this._password
};
};

AuthenticationSimple.prototype.isAuthorized = function(){
return this._username && this._password;
};

/**
* @return {AuthenticationSimple}
*/
AuthenticationSimple.prototype.logout = function(){
this._username = null;
this._password = null;
return this;
};

module.exports = AuthenticationSimple;

0 comments on commit 8c18607

Please sign in to comment.