var commands = $("#command_list li"); var request_body = $("#request_body"); var request_button = $("#request_button"); var response_body = $("#response_body"); var response_code = $("#rest_responsecode"); var rest_url = $('#rest_url'); var rest_method = $("#rest_method"); var selected_command = $("#selected_command"); var spinner = $(".loader"); var reminders = $("#rest_url_wrapper .rest_reminders"); var test_warning = $("#test_warning"); var GET = "GET"; var POST = "POST"; var PUT = "PUT"; var DELETE = "DELETE"; var URL_BASE = "https://api.ripple.com:443"; var DOC_BASE = "ripple-rest.html"; function slugify(str) { str = str.replace(/^\s+|\s+$/g, ''); // trim str = str.toLowerCase(); // remove accents, swap ñ for n, etc var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;"; var to = "aaaaeeeeiiiioooouuuunc------"; for (var i=0, l=from.length ; i"+default_val+""); var new_div = $("
").appendTo(rest_url); var new_param = $("").appendTo(new_div); new_param.autosizeInput({"space": 0}); //var new_label = $("").appendTo(new_div); } else { rest_url.append(""+params[i]+""); } } } function select_request(request) { command = requests[request]; if (command.test_only === true) { test_warning.show(); } else { test_warning.hide(); } if (command.description) { $(description).html(command.description); if (command.link) { $(description).append(" Read more"); } $(description).show(); } else if (command.link) { $(description).html("Read more"); } else { $(description).hide(); } selected_command.html($('') .attr('href', DOC_BASE+command.link) .text(command.name)); //rest_url.val(command.path); //rest_url.text(command.path); change_path(command); // rest_method.val(command.method); // rest_method.change(); request_button.val(command.method); request_button.text(command.method+" request"); update_method(request_button); if (command.hasOwnProperty("body")) { cm_request.setValue(JSON.stringify(command.body, null, 2)); } else { //No body, so wipe out the current contents. //This prevents confusion if the user toggles the HTTP method dropdown cm_request.setValue(""); } reset_response_area(); }; //helper to fill the default payment with a new UUID function get_uuid(callback) { $.get(URL_BASE + "/v1/uuid").done(callback); } function get_path() { s = ""; rest_url.find(".non_editable, .editable").each(function() { if (this.tagName == "INPUT") { s += $(this).val(); } else { s += $(this).text(); } }); return s; } function send_request() { //var method = rest_method.val(); var method = request_button.val(); if (method != GET && method != POST && method != PUT && method != DELETE) { console.log("ERROR: unrecognized http method"); return; } //var path = rest_url.val(); var path = get_path(); $(this).addClass('depressed'); response_body.addClass('obscured'); var original_cmd = requests[slugify(selected_command.text())]; if (original_cmd.hasOwnProperty("body")) { var body = cm_request.getValue(); $.ajax({ type: method, url: URL_BASE + path, data: body, contentType: 'application/json', processData: false }).done(success_output).fail(error_output).always(reset_sending_status); } else { $.ajax({ type: method, url: URL_BASE + path }).done(success_output).fail(error_output).always(reset_sending_status); } spinner.show(); } function error_output(xhr,status,statusText) { response_code.text(xhr.status+" "+xhr.statusText); cm_response.setValue(xhr.responseText); } function success_output(body,status,xhr) { response_code.text(xhr.status+" "+xhr.statusText); cm_response.setValue(JSON.stringify(body, null, 2)); } function reset_sending_status() { response_body.removeClass('obscured'); request_button.removeClass('depressed'); spinner.hide(); } function reset_response_area() { cm_response.setValue(""); response_code.text(""); } $(document).ready(function() { request_button.click(send_request); //rest_method.change(update_method); get_uuid(function(resp,status,xhr) { requests["submit-payment"].body.client_resource_id = resp.uuid; if (window.location.hash == "#submit-payment") { //we might have already loaded the call by the time the AJAX // completes, so refresh the default body. // Debatably a bad idea, because if the AJAX takes so long that the // user has already started editing the call, it'll reset it. select_request("submit-payment"); } }); if (window.location.hash) { var cmd = window.location.hash.slice(1).toLowerCase(); var keys = Object.keys(requests); var index = keys.indexOf(cmd); if (index === -1) return; var el = commands.eq(index); select_request(cmd); $(el).siblings().removeClass('selected'); $(el).addClass('selected'); } else { select_request('generate-wallet'); } });