Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Syncing to ruby core logic.

  • Loading branch information...
commit 7b90199f269365fea40008d8689b879bf1b3813c 1 parent b724edf
Andrew Kirilenko authored July 22, 2012
70  lib/client.js
@@ -18,7 +18,7 @@
18 18
     MAX_RETRIES = 5;
19 19
 
20 20
     function Client(company, product, options, defaultOptions, extraOptionsList) {
21  
-      var coreDefaultOptions;
  21
+      var config_base, coreDefaultOptions, ext, suffix, suffixes, _i, _j, _k, _len, _len1, _len2, _ref, _ref1;
22 22
       if (options == null) {
23 23
         options = {};
24 24
       }
@@ -31,6 +31,8 @@
31 31
       coreDefaultOptions = {
32 32
         user_agent: this.version()
33 33
       };
  34
+      this.env = options.env;
  35
+      this.env || (this.env = process.env[company.toUpperCase() + '_' + product.toUpperCase() + '_ENV'] || process.env[product.toUpperCase() + '_ENV']);
34 36
       this.optionsList = ['scheme', 'host', 'port', 'user_agent'].concat(extraOptionsList);
35 37
       this.options = {};
36 38
       this.loadFromHash('params', options);
@@ -39,9 +41,27 @@
39 41
       this.loadFromConfig(company, product, process.env[company.toUpperCase() + '_CONFIG']);
40 42
       this.loadFromEnv(company.toUpperCase() + '_' + product.toUpperCase());
41 43
       this.loadFromEnv(company.toUpperCase());
42  
-      this.loadFromConfig(company, product, "./." + company + ".json");
43  
-      this.loadFromConfig(company, product, "./" + company + ".json");
44  
-      this.loadFromConfig(company, product, "~/." + company + ".json");
  44
+      suffixes = [];
  45
+      if (this.env != null) {
  46
+        suffixes.push("-" + this.env);
  47
+        suffixes.push("_" + this.env);
  48
+      }
  49
+      suffixes.push('');
  50
+      for (_i = 0, _len = suffixes.length; _i < _len; _i++) {
  51
+        suffix = suffixes[_i];
  52
+        _ref = ['.json'];
  53
+        for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
  54
+          ext = _ref[_j];
  55
+          _ref1 = ["" + company + "-" + product, "" + company + "_" + product, company];
  56
+          for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
  57
+            config_base = _ref1[_k];
  58
+            this.loadFromConfig(company, product, "" + config_base + suffix + ext);
  59
+            this.loadFromConfig(company, product, "." + config_base + suffix + ext);
  60
+            this.loadFromConfig(company, product, "~/" + config_base + suffix + ext);
  61
+            this.loadFromConfig(company, product, "~/." + config_base + suffix + ext);
  62
+          }
  63
+        }
  64
+      }
45 65
       this.loadFromHash('defaults', defaultOptions);
46 66
       this.loadFromHash('defaults', coreDefaultOptions);
47 67
     }
@@ -80,16 +100,46 @@
80 100
       return _results;
81 101
     };
82 102
 
  103
+    Client.prototype.getSubHash = function(hash, subs) {
  104
+      var sub, _i, _len;
  105
+      if (hash == null) {
  106
+        return null;
  107
+      }
  108
+      for (_i = 0, _len = subs.length; _i < _len; _i++) {
  109
+        sub = subs[_i];
  110
+        if (hash[sub] == null) {
  111
+          return null;
  112
+        }
  113
+        hash = hash[sub];
  114
+      }
  115
+      return hash;
  116
+    };
  117
+
83 118
     Client.prototype.loadFromConfig = function(company, product, configFile) {
84  
-      var config, realConfigFile;
  119
+      var config, configData, realConfigFile;
85 120
       if (configFile != null) {
  121
+        realConfigFile = configFile.replace(/^~/, process.env.HOME);
  122
+        configData = '{}';
86 123
         try {
87  
-          realConfigFile = configFile.replace(/^~/, process.env.HOME);
88  
-          config = JSON.parse(fs.readFileSync(realConfigFile));
89  
-          this.loadFromHash(configFile, config["" + company + "_" + product]);
90  
-          this.loadFromHash(configFile, config[company]);
91  
-          return this.loadFromHash(configFile, config);
  124
+          configData = fs.readFileSync(realConfigFile);
92 125
         } catch (_error) {}
  126
+        config = JSON.parse(configData);
  127
+        if (this.env != null) {
  128
+          this.loadFromHash(configFile, this.getSubHash(config, [this.env, "" + company + "_" + product]));
  129
+          this.loadFromHash(configFile, this.getSubHash(config, [this.env, company, product]));
  130
+          this.loadFromHash(configFile, this.getSubHash(config, [this.env, product]));
  131
+          this.loadFromHash(configFile, this.getSubHash(config, [this.env, company]));
  132
+          this.loadFromHash(configFile, this.getSubHash(config, ["" + company + "_" + product, this.env]));
  133
+          this.loadFromHash(configFile, this.getSubHash(config, [company, product, this.env]));
  134
+          this.loadFromHash(configFile, this.getSubHash(config, [product, this.env]));
  135
+          this.loadFromHash(configFile, this.getSubHash(config, [company, this.env]));
  136
+          this.loadFromHash(configFile, this.getSubHash(config, [this.env]));
  137
+        }
  138
+        this.loadFromHash(configFile, this.getSubHash(config, ["" + company + "_" + product]));
  139
+        this.loadFromHash(configFile, this.getSubHash(config, [company, product]));
  140
+        this.loadFromHash(configFile, this.getSubHash(config, [product]));
  141
+        this.loadFromHash(configFile, this.getSubHash(config, [company]));
  142
+        return this.loadFromHash(configFile, this.getSubHash(config, []));
93 143
       }
94 144
     };
95 145
 
2  package.json
... ...
@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "iron_core",
3  
-  "version": "0.1.0",
  3
+  "version": "0.2.0",
4 4
   "description": "Core library for Iron products",
5 5
   "homepage": "https://github.com/iron-io/iron_core_node",
6 6
   "author": "Andrew Kirilenko & Iron.io, Inc",
65  src/client.coffee
@@ -12,19 +12,39 @@ class Client
12 12
     coreDefaultOptions =
13 13
       user_agent: @version()
14 14
 
  15
+    @env = options.env
  16
+    @env ||= process.env[company.toUpperCase() + '_' + product.toUpperCase() + '_ENV'] || process.env[product.toUpperCase() + '_ENV']
  17
+
15 18
     @optionsList = ['scheme', 'host', 'port', 'user_agent'].concat(extraOptionsList)
16 19
 
17 20
     @options = {}
18 21
 
19 22
     @loadFromHash('params', options)
  23
+
20 24
     @loadFromConfig(company, product, options.config)
  25
+
21 26
     @loadFromConfig(company, product, process.env[company.toUpperCase() + '_' + product.toUpperCase() + '_CONFIG'])
22 27
     @loadFromConfig(company, product, process.env[company.toUpperCase() + '_CONFIG'])
  28
+
23 29
     @loadFromEnv(company.toUpperCase() + '_' + product.toUpperCase())
24 30
     @loadFromEnv(company.toUpperCase())
25  
-    @loadFromConfig(company, product, "./.#{company}.json")
26  
-    @loadFromConfig(company, product, "./#{company}.json")
27  
-    @loadFromConfig(company, product, "~/.#{company}.json")
  31
+
  32
+    suffixes = []
  33
+
  34
+    if @env?
  35
+      suffixes.push("-#{@env}")
  36
+      suffixes.push("_#{@env}")
  37
+
  38
+    suffixes.push('')
  39
+
  40
+    for suffix in suffixes
  41
+      for ext in ['.json']
  42
+        for config_base in ["#{company}-#{product}", "#{company}_#{product}", company]
  43
+          @loadFromConfig(company, product, "#{config_base}#{suffix}#{ext}")
  44
+          @loadFromConfig(company, product, ".#{config_base}#{suffix}#{ext}")
  45
+          @loadFromConfig(company, product, "~/#{config_base}#{suffix}#{ext}")
  46
+          @loadFromConfig(company, product, "~/.#{config_base}#{suffix}#{ext}")
  47
+
28 48
     @loadFromHash('defaults', defaultOptions)
29 49
     @loadFromHash('defaults', coreDefaultOptions)
30 50
 
@@ -42,16 +62,45 @@ class Client
42 62
   loadFromEnv: (prefix) ->
43 63
     @setOption('environment variable', option, process.env[prefix + '_' + option.toUpperCase()]) for option in @optionsList
44 64
 
  65
+  getSubHash: (hash, subs) ->
  66
+    return null unless hash?
  67
+
  68
+    for sub in subs
  69
+      return null unless hash[sub]?
  70
+
  71
+      hash = hash[sub]
  72
+
  73
+    hash
  74
+
45 75
   loadFromConfig: (company, product, configFile) ->
46 76
     if configFile?
  77
+      realConfigFile = configFile.replace(/^~/, process.env.HOME)
  78
+
  79
+      configData = '{}'
  80
+
47 81
       try
48  
-        realConfigFile = configFile.replace(/^~/, process.env.HOME)
  82
+        configData = fs.readFileSync(realConfigFile)
  83
+
  84
+      config = JSON.parse(configData)
  85
+
  86
+      if @env?
  87
+        @loadFromHash(configFile, @getSubHash(config, [@env, "#{company}_#{product}"]))
  88
+        @loadFromHash(configFile, @getSubHash(config, [@env, company, product]))
  89
+        @loadFromHash(configFile, @getSubHash(config, [@env, product]))
  90
+        @loadFromHash(configFile, @getSubHash(config, [@env, company]))
  91
+
  92
+        @loadFromHash(configFile, @getSubHash(config, ["#{company}_#{product}", @env]))
  93
+        @loadFromHash(configFile, @getSubHash(config, [company, product, @env]))
  94
+        @loadFromHash(configFile, @getSubHash(config, [product, @env]))
  95
+        @loadFromHash(configFile, @getSubHash(config, [company, @env]))
49 96
 
50  
-        config = JSON.parse(fs.readFileSync(realConfigFile))
  97
+        @loadFromHash(configFile, @getSubHash(config, [@env]))
51 98
 
52  
-        @loadFromHash(configFile, config["#{company}_#{product}"])
53  
-        @loadFromHash(configFile, config[company])
54  
-        @loadFromHash(configFile, config)
  99
+      @loadFromHash(configFile, @getSubHash(config, ["#{company}_#{product}"]))
  100
+      @loadFromHash(configFile, @getSubHash(config, [company, product]))
  101
+      @loadFromHash(configFile, @getSubHash(config, [product]))
  102
+      @loadFromHash(configFile, @getSubHash(config, [company]))
  103
+      @loadFromHash(configFile, @getSubHash(config, []))
55 104
 
56 105
   headers: ->
57 106
     {'User-Agent': @options.user_agent}

0 notes on commit 7b90199

Please sign in to comment.
Something went wrong with that request. Please try again.