Permalink
Browse files

Initial implementation.

  • Loading branch information...
1 parent 3962adc commit 3bfa06f67e51967a3048bbab9262f75af16184f4 Andrew Kirilenko committed Jul 7, 2012
Showing with 161 additions and 14 deletions.
  1. +2 −14 .gitignore
  2. +22 −0 LICENSE
  3. +4 −0 Makefile
  4. +23 −0 package.json
  5. +110 −0 src/client.coffee
View
@@ -1,15 +1,3 @@
-lib-cov
-*.seed
-*.log
-*.csv
-*.dat
-*.out
-*.pid
-*.gz
-
-pids
-logs
-results
-
+lib
node_modules
-npm-debug.log
+npm-debug.log
View
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Iron.io, Inc
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
@@ -0,0 +1,4 @@
+build:
+ `npm bin`/coffee -o lib -c src/*.coffee
+
+all: build
View
@@ -0,0 +1,23 @@
+{
+ "name": "iron_core",
+ "version": "0.0.1",
+ "description": "Core library for Iron products",
+ "homepage": "https://github.com/iron-io/iron_core_node",
+ "author": "Andrew Kirilenko, Iron.io, Inc",
+ "main": "./lib/client",
+ "dependencies": {
+ "pkginfo": "",
+ "underscore": "",
+ "request": ""
+ },
+ "devDependencies": {
+ "coffee-script": "1"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/iron-io/iron_core_node.git"
+ },
+ "engines": {
+ "node": "0.6"
+ }
+}
View
@@ -0,0 +1,110 @@
+require('pkginfo')(module)
+version = @version
+
+_ = require('underscore')
+fs = require('fs')
+request = require('request')
+
+max_retries = 5
+
+class Client
+ constructor: (company, product, options = {}, default_options = {}, extra_options_list = []) ->
+ @options_list = ['scheme', 'host', 'port', 'user_agent'].concat(extra_options_list)
+
+ @options = {}
+
+ @load_from_hash('params', options)
+ @load_from_config(company, product, options.config_file)
+ @load_from_env(company.toUpperCase() + '_' + product.toUpperCase())
+ @load_from_env(company.toUpperCase())
+ @load_from_config(company, product, "./.#{company}.json")
+ @load_from_config(company, product, "./#{company}.json")
+ @load_from_config(company, product, "~/.#{company}.json")
+ @load_from_hash('defaults', default_options)
+ @load_from_hash('defaults', {'user_agent': "iron_core_node-#{version}"})
+
+ set_option: (source, name, value) ->
+ if (not @options[name]?) and value?
+ console.log("Setting #{name} to '#{value}' from #{source}")
+
+ @options[name] = value
+
+ load_from_hash: (source, hash) ->
+ if hash?
+ @set_option(source, option, hash[option]) for option in @options_list
+
+ load_from_env: (prefix) ->
+ @set_option('environment_variable', option, process.env[prefix + '_' + option.toUpperCase()]) for option in @options_list
+
+ load_from_config: (company, product, config_file) ->
+ if config_file?
+ try
+ real_config_file = config_file.replace(/^~/, process.env.HOME)
+
+ config = JSON.parse(fs.readFileSync(real_config_file))
+
+ @load_from_hash(config_file, config["#{company}_#{product}"])
+ @load_from_hash(config_file, config[company])
+ @load_from_hash(config_file, config)
+
+ headers: ->
+ {'User-Agent': @options.user_agent}
+
+ url: ->
+ "#{@options.scheme}://#{@options.host}:#{@options.port}/"
+
+ request: (request_info, handler, retry = 0) ->
+ request_bind = _.bind(@request, @)
+
+ request(request_info, (error, response, body) ->
+ if response.statusCode == 200
+ handler(error, response, body)
+ else
+ if response.statusCode == 503 and retry < max_retries
+ delay = Math.pow(4, retry) * 100 * Math.random()
+ _.delay(request_bind, delay, request_info, handler, retry + 1)
+ else
+ handler(error, response, body)
+ )
+
+ get: (method, params, handler) ->
+ request_info = {
+ 'method': 'GET',
+ 'uri': @url() + method,
+ 'headers': @headers,
+ 'qs': params
+ }
+
+ @request(request_info, handler)
+
+ post: (method, params, handler) ->
+ request_info = {
+ 'method': 'POST',
+ 'uri': @url() + method,
+ 'headers': @headers,
+ 'json': params
+ }
+
+ @request(request_info, handler)
+
+ put: (method, params, handler) ->
+ request_info = {
+ 'method': 'PUT',
+ 'uri': @url() + method,
+ 'headers': @headers,
+ 'json': params
+ }
+
+ @request(request_info, handler)
+
+ delete: (method, params, handler) ->
+ request_info = {
+ 'method': 'DELETE',
+ 'uri': @url() + method,
+ 'headers': @headers,
+ 'qs': params
+ }
+
+ @request(request_info, handler)
+
+module.exports.Client = Client

0 comments on commit 3bfa06f

Please sign in to comment.