Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Pushing lib to repo.

  • Loading branch information...
commit c9b3093025172c1402dee9962e8304ee7518849f 1 parent 68ab5f0
@iced iced authored
Showing with 185 additions and 2 deletions.
  1. +0 −1  .gitignore
  2. +184 −0 lib/client.js
  3. +1 −1  package.json
View
1  .gitignore
@@ -1,3 +1,2 @@
-lib
node_modules
npm-debug.log
View
184 lib/client.js
@@ -0,0 +1,184 @@
+// Generated by CoffeeScript 1.3.3
+(function() {
+ var Client, fs, request, version, _;
+
+ require('pkginfo')(module);
+
+ version = this.version;
+
+ _ = require('underscore');
+
+ fs = require('fs');
+
+ request = require('request');
+
+ Client = (function() {
+ var MAX_RETRIES;
+
+ MAX_RETRIES = 5;
+
+ function Client(company, product, options, default_options, extra_options_list) {
+ var core_default_options;
+ if (options == null) {
+ options = {};
+ }
+ if (default_options == null) {
+ default_options = {};
+ }
+ if (extra_options_list == null) {
+ extra_options_list = [];
+ }
+ core_default_options = {
+ user_agent: this.version()
+ };
+ this.options_list = ['scheme', 'host', 'port', 'user_agent'].concat(extra_options_list);
+ this.options = {};
+ this.load_from_hash('params', options);
+ this.load_from_config(company, product, options.config_file);
+ this.load_from_env(company.toUpperCase() + '_' + product.toUpperCase());
+ this.load_from_env(company.toUpperCase());
+ this.load_from_config(company, product, "./." + company + ".json");
+ this.load_from_config(company, product, "./" + company + ".json");
+ this.load_from_config(company, product, "~/." + company + ".json");
+ this.load_from_hash('defaults', default_options);
+ this.load_from_hash('defaults', core_default_options);
+ }
+
+ Client.prototype.version = function() {
+ return "iron_core_node-" + version;
+ };
+
+ Client.prototype.set_option = function(source, name, value) {
+ if ((!(this.options[name] != null)) && (value != null)) {
+ console.log("Setting " + name + " to '" + value + "' from " + source);
+ return this.options[name] = value;
+ }
+ };
+
+ Client.prototype.load_from_hash = function(source, hash) {
+ var option, _i, _len, _ref, _results;
+ if (hash != null) {
+ _ref = this.options_list;
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ option = _ref[_i];
+ _results.push(this.set_option(source, option, hash[option]));
+ }
+ return _results;
+ }
+ };
+
+ Client.prototype.load_from_env = function(prefix) {
+ var option, _i, _len, _ref, _results;
+ _ref = this.options_list;
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ option = _ref[_i];
+ _results.push(this.set_option('environment_variable', option, process.env[prefix + '_' + option.toUpperCase()]));
+ }
+ return _results;
+ };
+
+ Client.prototype.load_from_config = function(company, product, config_file) {
+ var config, real_config_file;
+ if (config_file != null) {
+ try {
+ real_config_file = config_file.replace(/^~/, process.env.HOME);
+ config = JSON.parse(fs.readFileSync(real_config_file));
+ this.load_from_hash(config_file, config["" + company + "_" + product]);
+ this.load_from_hash(config_file, config[company]);
+ return this.load_from_hash(config_file, config);
+ } catch (_error) {}
+ }
+ };
+
+ Client.prototype.headers = function() {
+ return {
+ 'User-Agent': this.options.user_agent
+ };
+ };
+
+ Client.prototype.url = function() {
+ return "" + this.options.scheme + "://" + this.options.host + ":" + this.options.port + "/";
+ };
+
+ Client.prototype.request = function(request_info, cb, retry) {
+ var request_bind;
+ if (retry == null) {
+ retry = 0;
+ }
+ request_bind = _.bind(this.request, this);
+ return request(request_info, function(error, response, body) {
+ var delay;
+ if (response.statusCode === 200) {
+ return cb(error, response, body);
+ } else {
+ if (response.statusCode === 503 && retry < this.MAX_RETRIES) {
+ delay = Math.pow(4, retry) * 100 * Math.random();
+ return _.delay(request_bind, delay, request_info, cb, retry + 1);
+ } else {
+ return cb(error, response, body);
+ }
+ }
+ });
+ };
+
+ Client.prototype.get = function(method, params, cb) {
+ var request_info;
+ request_info = {
+ method: 'GET',
+ uri: this.url() + method,
+ headers: this.headers(),
+ qs: params
+ };
+ return this.request(request_info, cb);
+ };
+
+ Client.prototype.post = function(method, params, cb) {
+ var request_info;
+ request_info = {
+ method: 'POST',
+ uri: this.url() + method,
+ headers: this.headers(),
+ json: params
+ };
+ return this.request(request_info, cb);
+ };
+
+ Client.prototype.put = function(method, params, cb) {
+ var request_info;
+ request_info = {
+ method: 'PUT',
+ uri: this.url() + method,
+ headers: this.headers(),
+ json: params
+ };
+ return this.request(request_info, cb);
+ };
+
+ Client.prototype["delete"] = function(method, params, cb) {
+ var request_info;
+ request_info = {
+ method: 'DELETE',
+ uri: this.url() + method,
+ headers: this.headers(),
+ qs: params
+ };
+ return this.request(request_info, cb);
+ };
+
+ Client.prototype.parse_response = function(error, response, body, cb) {
+ if (response.statusCode === 200) {
+ return cb(null, body);
+ } else {
+ return cb(new Error(body), null);
+ }
+ };
+
+ return Client;
+
+ })();
+
+ module.exports.Client = Client;
+
+}).call(this);
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "iron_core",
- "version": "0.0.1",
+ "version": "0.0.2",
"description": "Core library for Iron products",
"homepage": "https://github.com/iron-io/iron_core_node",
"author": "Andrew Kirilenko & Iron.io, Inc",
Please sign in to comment.
Something went wrong with that request. Please try again.