Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 7b90199f26
Fetching contributors…

Cannot retrieve contributors at this time

file 169 lines (123 sloc) 5.089 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
require('pkginfo')(module)
version = @version

_ = require('underscore')
fs = require('fs')
request = require('request')

class Client
  MAX_RETRIES = 5

  constructor: (company, product, options = {}, defaultOptions = {}, extraOptionsList = []) ->
    coreDefaultOptions =
      user_agent: @version()

    @env = options.env
    @env ||= process.env[company.toUpperCase() + '_' + product.toUpperCase() + '_ENV'] || process.env[product.toUpperCase() + '_ENV']

    @optionsList = ['scheme', 'host', 'port', 'user_agent'].concat(extraOptionsList)

    @options = {}

    @loadFromHash('params', options)

    @loadFromConfig(company, product, options.config)

    @loadFromConfig(company, product, process.env[company.toUpperCase() + '_' + product.toUpperCase() + '_CONFIG'])
    @loadFromConfig(company, product, process.env[company.toUpperCase() + '_CONFIG'])

    @loadFromEnv(company.toUpperCase() + '_' + product.toUpperCase())
    @loadFromEnv(company.toUpperCase())

    suffixes = []

    if @env?
      suffixes.push("-#{@env}")
      suffixes.push("_#{@env}")

    suffixes.push('')

    for suffix in suffixes
      for ext in ['.json']
        for config_base in ["#{company}-#{product}", "#{company}_#{product}", company]
          @loadFromConfig(company, product, "#{config_base}#{suffix}#{ext}")
          @loadFromConfig(company, product, ".#{config_base}#{suffix}#{ext}")
          @loadFromConfig(company, product, "~/#{config_base}#{suffix}#{ext}")
          @loadFromConfig(company, product, "~/.#{config_base}#{suffix}#{ext}")

    @loadFromHash('defaults', defaultOptions)
    @loadFromHash('defaults', coreDefaultOptions)

  version: ->
    "iron_core_node-#{version}"

  setOption: (source, name, value) ->
    if (not @options[name]?) and value?
      @options[name] = value

  loadFromHash: (source, hash) ->
    if hash?
      @setOption(source, option, hash[option]) for option in @optionsList

  loadFromEnv: (prefix) ->
    @setOption('environment variable', option, process.env[prefix + '_' + option.toUpperCase()]) for option in @optionsList

  getSubHash: (hash, subs) ->
    return null unless hash?

    for sub in subs
      return null unless hash[sub]?

      hash = hash[sub]

    hash

  loadFromConfig: (company, product, configFile) ->
    if configFile?
      realConfigFile = configFile.replace(/^~/, process.env.HOME)

      configData = '{}'

      try
        configData = fs.readFileSync(realConfigFile)

      config = JSON.parse(configData)

      if @env?
        @loadFromHash(configFile, @getSubHash(config, [@env, "#{company}_#{product}"]))
        @loadFromHash(configFile, @getSubHash(config, [@env, company, product]))
        @loadFromHash(configFile, @getSubHash(config, [@env, product]))
        @loadFromHash(configFile, @getSubHash(config, [@env, company]))

        @loadFromHash(configFile, @getSubHash(config, ["#{company}_#{product}", @env]))
        @loadFromHash(configFile, @getSubHash(config, [company, product, @env]))
        @loadFromHash(configFile, @getSubHash(config, [product, @env]))
        @loadFromHash(configFile, @getSubHash(config, [company, @env]))

        @loadFromHash(configFile, @getSubHash(config, [@env]))

      @loadFromHash(configFile, @getSubHash(config, ["#{company}_#{product}"]))
      @loadFromHash(configFile, @getSubHash(config, [company, product]))
      @loadFromHash(configFile, @getSubHash(config, [product]))
      @loadFromHash(configFile, @getSubHash(config, [company]))
      @loadFromHash(configFile, @getSubHash(config, []))

  headers: ->
    {'User-Agent': @options.user_agent}

  url: ->
    "#{@options.scheme}://#{@options.host}:#{@options.port}/"

  request: (requestInfo, cb, retry = 0) ->
    requestBind = _.bind(@request, @)

    request(requestInfo, (error, response, body) ->
      if response.statusCode == 200
        cb(error, response, body)
      else
        if response.statusCode == 503 and retry < @MAX_RETRIES
          delay = Math.pow(4, retry) * 100 * Math.random()
          _.delay(requestBind, delay, requestInfo, cb, retry + 1)
        else
          cb(error, response, body)
    )

  get: (method, params, cb) ->
    requestInfo =
      method: 'GET'
      uri: @url() + method
      headers: @headers()
      qs: params

    @request(requestInfo, cb)

  post: (method, params, cb) ->
    requestInfo =
      method: 'POST'
      uri: @url() + method
      headers: @headers()
      json: params

    @request(requestInfo, cb)

  put: (method, params, cb) ->
    requestInfo =
      method: 'PUT'
      uri: @url() + method
      headers: @headers()
      json: params

    @request(requestInfo, cb)

  delete: (method, params, cb) ->
    requestInfo =
      method: 'DELETE'
      uri: @url() + method
      headers: @headers()
      qs: params

    @request(requestInfo, cb)

  parseResponse: (error, response, body, cb, parseJson = true) ->
    if response.statusCode == 200
      body = JSON.parse(body) if parseJson and typeof(body) == 'string'

      cb(null, body)
    else
      cb(new Error(body), null)

module.exports.Client = Client
Something went wrong with that request. Please try again.