nodejsでherokuのAPIを叩く(domain編)

nodejsからherokuのAPIを叩く。

サポートしてるのはdomainの追加と削除のみ。
気が向いたらnpmにする。

```javascript
var CONFIG = {
 heroku: {
     key: "hogehoge",
     name: "fugafugaapp"
  }
};
var request = require("request");
var path = require("path");

/************************************************************/
// httpArg
//   convert a dictionary to http arguments like:
//   hoge=fuga&piyo=piyopiyo
//   -- params := Dictionary
/************************************************************/
function httpArg(params) {
    var ret = "";
    var initial = true;
    for (var key in params) {
        if (initial) {
            initial = false;
            ret = key + "=" + params[key];
        }
        else {
            ret += "&" + key + "=" + params[key];
        }
    }
    return ret;
};


/************************************************************/
// herokuPOST
//   call a heroku API using POST method.
//   -- api := String, end point of the api.
//             the url will be https://api.heroku.com/apps/:appname/:api
//   -- params := Dictionary, parametetrs for the API
//   -- cb := function(Error, body)
/************************************************************/
exports.herokuPOST = function(api, params, cb) {
    var base64_user = new Buffer(":" + CONFIG.heroku.key).toString("base64");
    request.post({
        url: "https://api.heroku.com/apps/" + CONFIG.heroku.name + "/" + api,
        headers: {
            accept: "application/json",
            authorization: "Basic " + base64_user
        },
        body: httpArg(params)
    }, function(err, response, body) {
        cb(err, body);
    });
};

/************************************************************/
// herokuGET
//   call a heroku API using GET method.
//   -- api := String, end point of the api.
//             the url will be https://api.heroku.com/apps/:appname/:api
//   -- cb := function(Error, body)
/************************************************************/
exports.herokuGET = function(api, cb) {
    var base64_user = new Buffer(":" + CONFIG.heroku.key).toString("base64");
    request({
        url: "https://api.heroku.com/apps/" + CONFIG.heroku.name + "/" + api,
        headers: {
            accept: "application/json",
            authorization: "Basic " + base64_user
        }
    }, function(err, response, body) {
        cb(err, body);
    });
};

/************************************************************/
// herokuDEL
//   call a heroku API using DELETE method.
//   -- api := String, end point of the api.
//             the url will be https://api.heroku.com/apps/:appname/:api
//   -- cb := function(Error, body)
/************************************************************/
exports.herokuDEL = function(api, cb) {
    var base64_user = new Buffer(":" + CONFIG.heroku.key).toString("base64");
    request.del({
        url: "https://api.heroku.com/apps/" + CONFIG.heroku.name + "/" + api,
        headers: {
            accept: "application/json",
            authorization: "Basic " + base64_user
        }
    }, function(err, response, body) {
        cb(err, body);
    });
};



/************************************************************/
// registerDomain
//     domain := String
//     cb := function(Error)
/************************************************************/
exports.registerDomain = function(domain, cb) {
    exports.herokuPOST("domains", {
        "domain_name[domain]": domain
    }, function(err, body) {
        cb(err);
    });
};

/************************************************************/
// removeDomain
//     domain := String
//     cb := function(Error)
/************************************************************/
exports.removeDomain = function(domain, cb) {
    exports.herokuDEL("domains/" + domain, function(err, body) {
        cb(err);
    });
};


// sample code 
// exports.registerDomain("hogehoge.smakul.com", function(err) {
//     console.log(err);
//     exports.removeDomain("hogehoge.smakul.com", function(err) {
//         console.log(err);
//     });
// });


```