/*
 * Avocore : twitter.js
 * copyright (c) 2015 Susisu
 */

"use strict";

function end_module() {
    module.exports = Object.freeze({
        "TwitterKeys"   : TwitterKeys,
        "TwitterURLBase": TwitterURLBase,
        "rest"          : rest
    });
}

var request = require("request");

var actions = require("./actions.js");

function TwitterKeys(consumerKey, consumerSecret, token, tokenSecret) {
    this.consumerKey    = consumerKey;
    this.consumerSecret = consumerSecret;
    this.token          = token;
    this.tokenSecret    = tokenSecret;
}

var TwitterURLBase = Object.freeze({
    "rest"      : "https://api.twitter.com/1.1/",
    "stream"    : "https://stream.twitter.com/1.1/",
    "userStream": "https://userstream.twitter.com/1.1/",
    "siteStream": "https://sitestream.twitter.com/1.1/",
    "media"     : "https://upload.twitter.com/1.1/"
});

function rest(keys, method, apiName, params) {
    method = method.toUpperCase();
    var options = {
        "oauth": {
            "consumer_key"   : keys.consumerKey,
            "consumer_secret": keys.consumerSecret,
            "token"          : keys.token,
            "token_secret"   : keys.tokenSecret
        },
        "method": method,
        "url"   : TwitterURLBase.rest + apiName + ".json"
    };
    switch (method) {
        case "GET":
            options["qs"] = params;
            break;
        case "POST":
            options["form"] = params;
            break;
        default:
            throw new Error("unknown request method: " + method);
    }
    return new actions.Action(function (emit) {
        request(options, function (error, response, data) {
            if (!error) {
                emit(data);
            }
            else {
                console.log(error);
            }
        });
    });
}

end_module();

@@ -143,36 +143,24 @@
    %22
-TwitterKeys%22   : TwitterKeys
+KeySet%22 : KeySet
 ,%0A  
@@ -170,35 +170,21 @@
    %22
-Twitter
 UR
-L
+I
 Base%22: 
-Twitter
 UR
-L
+I
 Base
@@ -199,23 +199,16 @@
   %22rest%22
-       
    : res
@@ -308,19 +308,14 @@
 ion 
-TwitterKeys
+KeySet
 (con
@@ -522,26 +522,19 @@
 %0A%7D%0A%0Avar 
-Twitter
 UR
-L
+I
 Base = O
@@ -1194,18 +1194,11 @@
   : 
-Twitter
 UR
-L
+I
 Base

Rename objects