<commit_before># a simple reimplentation
# to use traffic cop
methodMap = {
    'create': 'POST',
    'update': 'PUT',
    'patch':  'PATCH',
    'delete': 'DELETE',
    'read':   'GET'
}

getValue = (object, prop) ->
    if !(object && object[prop])
        return null
    return if _.isFunction(object[prop]) then object[prop]() else object[prop]

Lanes.Models.Sync = {
    paramsMap:
        fields  : 'f'
        with    : 'w'
        query   : 'q'
        include : 'i'
        order   : 'o'
        limit   : 'l'
        start   : 's'
        format  : 'df'

    urlError: ->
        throw new Error('A "url" property or function must be specified')

    state: (method, model, options = {}) ->
        options.url ||= _.result(model, "url")
        if _.contains(['create', 'update', 'patch'], method)
            isSave = true
            options.data ||= model.dataForSave(options)
        model.requestInProgress = options
        model.trigger("request", model, method, options)
        return new _.Promise (resolve, reject) ->
            handler = (reply) ->
                delete model.requestInProgress
                model.lastServerMessage = reply.message
                if reply.errors
                    Lanes.warn reply.errors
                    model.errors = reply.errors
                    model.trigger("error", model, options)
                model.setFromServer(reply.data, options, method)
                model.trigger("save", model, reply, options) if isSave
                model.trigger("sync", model, reply, options)
                resolve(model)

            Lanes.Models.Sync.perform(method, options).then (reply) ->
                handler(reply)
            , (err) ->
                reply = { errors: { http: err.error.message } }
                try
                    reply = JSON.parse(err.body) unless _.isEmpty(err?.body)
                finally
                    handler(reply)



    perform: (method, options = {}) ->
        query = {}
        for key, value of options
            query[ this.paramsMap[key] ] = value if this.paramsMap[key]

        # Default JSON-request options.
        _.defaults(options, {
            method: methodMap[method]
            dataType: "json"
            data: query
        })

        # Ensure that we have a URL.
        options.url or Lanes.Models.Sync.urlError()
        options.url += '.json'
        unless _.isEmpty(query)
            options.url += '?' + Lanes.lib.objToParam(query)
        if options.data and !_.isString(options.data)
            options.originalData = options.data
            options.data = JSON.stringify( options.data )
        options.headers ||= {}
        options.headers['X_CSRF_TOKEN'] = Lanes.config.csrf_token
        options.contentType = "application/json"

        # Make the request, allowing the user to override any Ajax options.
        # xhr = options.xhr = Lanes.$.ajax(_.extend(params, options))
        new _.Promise( (resolve, reject) ->
            options.xhr = Lanes.Vendor.xhr(options, (err, resp, body) ->
                if err
                    reject(error:err, body:options.xhr.responseText)
                else
                    resolve(JSON.parse(body))
            )
        )


}<commit_msg>Set json property for xhr lib<commit_after># a simple reimplentation
# to use traffic cop
methodMap = {
    'create': 'POST',
    'update': 'PUT',
    'patch':  'PATCH',
    'delete': 'DELETE',
    'read':   'GET'
}

getValue = (object, prop) ->
    if !(object && object[prop])
        return null
    return if _.isFunction(object[prop]) then object[prop]() else object[prop]

Lanes.Models.Sync = {
    paramsMap:
        fields  : 'f'
        with    : 'w'
        query   : 'q'
        include : 'i'
        order   : 'o'
        limit   : 'l'
        start   : 's'
        format  : 'df'

    urlError: ->
        throw new Error('A "url" property or function must be specified')

    state: (method, model, options = {}) ->
        options.url ||= _.result(model, "url")
        if _.contains(['create', 'update', 'patch'], method)
            isSave = true
            options.json ||= model.dataForSave(options)
        model.requestInProgress = options
        model.trigger("request", model, method, options)
        return new _.Promise (resolve, reject) ->
            handler = (reply) ->
                delete model.requestInProgress
                model.lastServerMessage = reply.message
                if reply.errors
                    Lanes.warn reply.errors
                    model.errors = reply.errors
                    model.trigger("error", model, options)
                model.setFromServer(reply.data, options, method)
                model.trigger("save", model, reply, options) if isSave
                model.trigger("sync", model, reply, options)
                resolve(model)

            Lanes.Models.Sync.perform(method, options).then (reply) ->
                handler(reply)
            , (err) ->
                reply = { errors: { http: err.error.message } }
                try
                    reply = JSON.parse(err.body) unless _.isEmpty(err?.body)
                finally
                    handler(reply)



    perform: (method, options = {}) ->
        query = {}
        for key, value of options
            query[ this.paramsMap[key] ] = value if this.paramsMap[key]

        # Default JSON-request options.
        _.defaults(options, {
            method: methodMap[method]
            dataType: "json"
            json: query
        })

        # Ensure that we have a URL.
        options.url or Lanes.Models.Sync.urlError()
        options.url += '.json'
        unless _.isEmpty(query)
            options.url += '?' + Lanes.lib.objToParam(query)

        # if options.data and !_.isString(options.data)
        #     options.originalData = options.data
        #     options.json = options.data
        options.headers ||= {}
        # options.json = true
        options.headers['X_CSRF_TOKEN'] = Lanes.config.csrf_token
        options.contentType = "application/json"

        # Make the request, allowing the user to override any Ajax options.
        # xhr = options.xhr = Lanes.$.ajax(_.extend(params, options))
        new _.Promise( (resolve, reject) ->
            options.xhr = Lanes.Vendor.xhr(options, (err, resp, body) ->
                if err
                    reject(error:err, body:options.xhr.responseText)
                else
                    resolve(body)
            )
        )


}
{'fraction_non_alphanumeric': 0.11996939556235654, 'fraction_numerical': 0.0, 'mean_word_length': 1.6601546601546602, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7367541', 'n_tokens_mistral': 1820, 'n_tokens_neox': 1769, 'n_words': 442}