require 'hashparam'

class Ajax < HashAmb
  ignore_case true

  Options = [ # defaults, description
    :asynchronous, # true
    :contentType, # application/x-www-form-urlencoded
    :encoding, # UTF-8
    :method, # post
    :parameters, # ''
    :postBody, # None
    :requestHeaders, # auto prototype headers, values passed augment defaults
    :evalJS, # true
    :evalJSON, # true
    :sanitizeJSON # false local, true otherwise
  ]
  Callbacks = [
    :onCreate, # when Ajax.Request is initialized
    :onComplete, # very end of lifecycle, after any of Success/Failure/XYZ
    :onException, # XHR error
    :onFailure, # complete, but status code != 2xy
    :onInteractive, # (not guaranteed) when requester recieves part of resp
    :onLoading, # (not guaranteed) when connection opened
    :onLoaded, # (not guaranteed) connection opened, XHR fully setup
    :onSuccess # complete, code == 2xy or undef
    # onXYZ complete, status == XYZ, supresses Success/Failure
  ]
  map :js, :evalJS
  map :json, :evalJSON
  add_param *(Callbacks + Options)
end

class Updater < Ajax
  add_param :evalScripts, :insertion
end
