Permalink
Browse files

0.1.16 - rewrite + coffeescript

  • Loading branch information...
1 parent 551426a commit df7d568883be3d4ba971332652d54ac15f152851 @contra contra committed Oct 25, 2011
Showing with 316 additions and 0 deletions.
  1. +7 −0 Cakefile
  2. +15 −0 LICENSE
  3. +1 −0 README.md
  4. +202 −0 lib/nodester.js
  5. +91 −0 src/nodester.coffee
View
@@ -0,0 +1,7 @@
+{exec} = require 'child_process'
+task 'build', 'Build project from src to lib', ->
+ exec 'coffee --compile --output lib/ src/', (err, stdout, stderr) ->
+ throw err if err
+ console.log stdout if stdout
+ console.log stderr if stderr
+ console.log 'Build completed!'
View
15 LICENSE
@@ -0,0 +1,15 @@
+Copyright (C) 2011 Chris Matthieu (contact info: chris@nodester.com)
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as
+published by the Free Software Foundation, Version 3, 19 November 2007.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with this program. If not, see http://www.gnu.org/licenses/agpl-3.0.html.
+
+http://www.gnu.org/licenses/why-affero-gpl.html
View
@@ -0,0 +1 @@
+nodester-api is a library to interact with Nodester (or 3rd party Nodester services) through NodeJS
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,91 @@
+request = require 'request'
+querystring = require 'querystring'
+fs = require 'fs'
+
+class Nodester
+ constructor: (@username, @password, @basehost, @secure) ->
+ @basehost ?= 'api.nodester.com'
+ @protocol = if @secure then 'https://' else 'http://'
+ @baseurl = "#{ @protocol }#{ username }:#{ @password }@#{ basehost }/"
+
+ request: (method, path, body, cb) ->
+ req =
+ uri: @baseurl + path
+ method: method
+ body: querystring.stringify(body)
+ headers:
+ 'Content-Type': 'application/x-www-form-urlencoded'
+ proxy: process.env.http_proxy
+
+ request req, handleResponse cb
+
+ get: (path, cb) -> @request "GET", path, null, cb
+ post: (path, body, cb) -> @request "POST", path, body, cb
+ put: (path, body, cb) -> @request "PUT", path, body, cb
+ del: (path, body, cb) -> @request "DELETE", path, body, cb
+
+ status: (cb) -> @get "status", cb
+ coupon_request: (email, cb) -> @post "coupon", {email: email}, cb
+
+ user_delete = (user, cb) -> @del "user", user: user, cb
+ user_create: (user, pass, email, rsakey, coupon, cb) ->
+ rsadata = fs.readFileSync rsadata
+ return cb message: "No RSA key found in #{ rsakey }" unless rsadata
+ return cb message: "Invalid SSH key file." unless rsadata.length > 40
+ postData =
+ user: user
+ password: pass
+ email: email
+ coupon: coupon
+ rsakey: rsadata
+ @post "user", postData, cb
+
+ user_sendtoken: (some_user, cb) -> @post "reset_password", {user: some_user}, cb
+ user_setpass: (token, a_password, cb) -> @put "reset_password/#{ token }", {password: a_password}, cb
+ user_setkey: (rsakey, cb) -> @put "user", rsakey: rsakey, cb
+ apps_list: (cb) -> @get "apps", cb
+
+ app_create: (name, start, cb) -> @post "app", {appname: name, start: start}, cb
+
+ app_running: (name, running, cb) -> @put "app", {appname: name, running: running}, cb
+ app_start: (name, cb) -> @app_running name, "true", cb
+ app_restart: (name, cb) -> @app_running name, "restart", cb
+ app_stop: (name, cb) -> @app_running name, "false", cb
+
+ app_delete: (name, cb) -> @del "app", appname: name, cb
+ app_gitreset: (name, cb) -> @del "gitreset", appname: name, cb
+ app_info: (name, cb) -> @get "app/#{ name }", cb
+ app_logs: (name, cb) -> @get "applogs#{ name }", cb
+
+ appnpm_handler: (name, package, action, cb) -> @post "appnpm", {appname: name, package: package, action: action}, cb
+ appnpm_install: (name, package, cb) -> @appnpm_handler name, package, "install", cb
+ appnpm_update: (name, package, cb) -> @appnpm_handler name, package, "update", cb
+ appnpm_uninstall: (name, package, cb) -> @appnpm_handler name, package, "uninstall", cb
+
+ appdomain_add: (name, domain, cb) -> @post "appdomains", {appname: name, domain: domain}, cb
+ appdomain_delete: (name, domain, cb) -> @del "appdomains", {appname: name, domain: domain}, cb
+
+ appdomains: (cb) -> @get "appdomains", cb
+
+handleResponse = (cb) ->
+ return (err, res, body) =>
+ errCode = res.statusCode unless res.statusCode < 400
+ if body?
+ try
+ success = JSON.parse body
+ catch e
+ errMessage = body
+ errCause = "JSON Parse error!"
+
+ if errCode then errCause = "HTTP Error #{ errCode } returned."
+ if success?.message and not /^success/.exec success?.status then errCause = success.message
+ unless body then errCause ?= "No response received."
+
+ if errCause
+ error = {}
+ error.code = errCode
+ error.message = "Fatal Error! API Response: #{ body }\nReason: #{ errCause }"
+
+ cb error, success, {response: body, errorCode: errCode}
+
+module.exports.nodester = Nodester

0 comments on commit df7d568

Please sign in to comment.