1. Alex Zylman
  2. mandrill-api-node

Commits

Alex Zylman  committed 61b46a3

cleanup

  • Participants
  • Parent commits bea79d6
  • Branches master

Comments (0)

Files changed (1)

File mandrill.coffee

View file
  • Ignore whitespace
-
 https = require 'https'
 
 OPTS = {
         @urls = new Urls(this)
         @webhooks = new Webhooks(this)
         @senders = new Senders(this)
-
-        if @apikey == null then @apikey = process.env['MANDRILL_APIKEY']
+        @apikey ?= process.env['MANDRILL_APIKEY']
 
     call: (uri, params={}, onresult, onerror) ->
         params.key = @apikey
                     json = JSON.parse(json)
                 catch e
                     json = {status: 'error', name: 'GeneralError', message: e}
-                
+
                 json ?= {status: 'error', name: 'GeneralError', message: 'An unexpected error occurred'}
                 if res.statusCode != 200
                     if onerror then onerror(json) else @onerror(json)
     onerror: (err) ->
         throw {name: err.name, message: err.message, toString: -> "#{err.name}: #{err.message}"}
 
+parseArgs = (params, onsuccess, onerror) ->
+    return [params, onsuccess, onerror] if typeof params isnt 'function'
+    [params, onsuccess, onerror] = [{}, params, onsuccess]
+
 class Templates
     constructor: (@master) ->
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     add: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["from_email"] ?= null
         params["from_name"] ?= null
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     info: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('templates/info', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     update: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["from_email"] ?= null
         params["from_name"] ?= null
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     publish: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('templates/publish', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     delete: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('templates/delete', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     list: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('templates/list', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     timeSeries: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('templates/time-series', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     render: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["merge_vars"] ?= null
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     info: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('exports/info', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     list: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('exports/list', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     rejects: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["notify_email"] ?= null
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     whitelist: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["notify_email"] ?= null
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     activity: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["notify_email"] ?= null
         params["date_from"] ?= null
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     info: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('users/info', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     ping: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('users/ping', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     ping2: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('users/ping2', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     senders: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('users/senders', params, onsuccess, onerror)
 class Rejects
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     add: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('rejects/add', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     list: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["email"] ?= null
         params["include_expired"] ?= false
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     delete: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('rejects/delete', params, onsuccess, onerror)
 class Inbound
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     domains: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('inbound/domains', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     routes: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('inbound/routes', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     sendRaw: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["to"] ?= null
         params["mail_from"] ?= null
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     list: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('tags/list', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     delete: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('tags/delete', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     info: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('tags/info', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     timeSeries: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('tags/time-series', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     allTimeSeries: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('tags/all-time-series', params, onsuccess, onerror)
 class Messages
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     send: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["async"] ?= false
         params["ip_pool"] ?= null
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     sendTemplate: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["async"] ?= false
         params["ip_pool"] ?= null
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     search: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["query"] ?= '*'
         params["date_from"] ?= null
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     info: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('messages/info', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     parse: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('messages/parse', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     sendRaw: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["from_email"] ?= null
         params["from_name"] ?= null
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     listScheduled: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["to"] ?= null
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     cancelScheduled: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('messages/cancel-scheduled', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     reschedule: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('messages/reschedule', params, onsuccess, onerror)
 class Whitelists
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     add: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('whitelists/add', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     list: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["email"] ?= null
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     delete: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('whitelists/delete', params, onsuccess, onerror)
 class Internal
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     list: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('urls/list', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     search: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('urls/search', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     timeSeries: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('urls/time-series', params, onsuccess, onerror)
 class Webhooks
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     list: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('webhooks/list', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     add: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["description"] ?= null
         params["events"] ?= []
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     info: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('webhooks/info', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     update: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         params["description"] ?= null
         params["events"] ?= []
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     delete: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('webhooks/delete', params, onsuccess, onerror)
 class Senders
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     list: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('senders/list', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     domains: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('senders/domains', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     info: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('senders/info', params, onsuccess, onerror)
 
     @param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
     ###
     timeSeries: (params={}, onsuccess, onerror) ->
-        if typeof params == 'function'
-            onerror = onsuccess
-            onsuccess = params
-            params = {}
-
+        [params, onsuccess, onerror] = parseArgs params, onsuccess, onerror
 
         @master.call('senders/time-series', params, onsuccess, onerror)
-
-