From 9457a28e5d21da1f96e30698bf909e76d3c5ab5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Mon, 10 Oct 2016 19:07:41 +0200 Subject: [PATCH 1/3] Infer the file extension from the content-type response header. --- lib/rest-client-view.coffee | 10 ++++++++-- package.json | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/rest-client-view.coffee b/lib/rest-client-view.coffee index c49ea9f..6fabe9b 100644 --- a/lib/rest-client-view.coffee +++ b/lib/rest-client-view.coffee @@ -1,6 +1,7 @@ {$, ScrollView} = require 'atom-space-pen-views' {Emitter} = require 'atom' querystring = require 'querystring' +mime = require 'mime-types' RestClientResponse = require './rest-client-response' RestClientEditor = require './rest-client-editor' @@ -136,6 +137,7 @@ class RestClientView extends ScrollView @RECENT_REQUESTS_PATH = "#{PACKAGE_PATH}/recent.json" @lastRequest = null + @lastResponse = null @recentRequests = new RestClientPersist(@RECENT_REQUESTS_PATH) @recentRequests.setRequestFileLimit(RECENT_REQUESTS_FILE_LIMIT) @@ -194,7 +196,8 @@ class RestClientView extends ScrollView openInEditor: -> textResult = $(rest_form.result).text() - file_name = "#{current_method} - #{$(rest_form.url).val()}" + extension = mime.extension(@lastResponse.headers['content-type']); + file_name = "#{@lastRequest.method} #{@lastRequest.url}#{'.' + extension if extension}" editor = new RestClientEditor(textResult, file_name) editor.open() @@ -267,6 +270,7 @@ class RestClientView extends ScrollView body: @getRequestBody() onResponse: (error, response, body) => + @setLastResponse(response) if !error statusMessage = response.statusCode + " " + response.statusMessage @@ -282,7 +286,6 @@ class RestClientView extends ScrollView else @showErrorResponse(DEFAULT_NORESPONSE) result = error - $(rest_form.result).text(result) $(rest_form.result_headers).text(headers).hide() @emitter.emit RestClientEvent.REQUEST_FINISHED, response @@ -386,6 +389,9 @@ class RestClientView extends ScrollView setLastRequest: (request) => @lastRequest = request + setLastResponse: (response) => + @lastResponse = response + getHeadersAsString: (headers) -> output = '' diff --git a/package.json b/package.json index 79f54a3..d3a49d2 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "dependencies": { "atom-space-pen-views": "^2.0.3", + "mime-types": "^2.1.12", "request": "*" } } From 0df6ea60de59ee9abc5fc581b02fd66fc6063732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Mon, 10 Oct 2016 19:13:17 +0200 Subject: [PATCH 2/3] Place the *Open in separate editor* option on the top. --- lib/rest-client-view.coffee | 2 +- styles/rest-client.less | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/rest-client-view.coffee b/lib/rest-client-view.coffee index 6fabe9b..27b9dd1 100644 --- a/lib/rest-client-view.coffee +++ b/lib/rest-client-view.coffee @@ -126,11 +126,11 @@ class RestClientView extends ScrollView @a class: "#{rest_form.result_headers_link.split('.')[1]}", 'Headers' @span ' | ' @span class: "#{rest_form.status.split('.')[1]}" + @span class: "text-info lnk #{rest_form.open_in_editor.split('.')[1]}", 'Open in separate editor' @span class: "#{rest_form.loading.split('.')[1]} loading loading-spinner-small inline-block", style: 'display: none;' @pre class: "#{rest_form.result_headers.split('.')[1]}", "" @pre class: "#{rest_form.result.split('.')[1]}", "#{RestClientResponse.DEFAULT_RESPONSE}" - @div class: "text-info lnk #{rest_form.open_in_editor.split('.')[1]}", 'Open in separate editor' initialize: -> @COLLECTIONS_PATH = "#{PACKAGE_PATH}/collections.json" diff --git a/styles/rest-client.less b/styles/rest-client.less index f7cb4db..b2e7799 100644 --- a/styles/rest-client.less +++ b/styles/rest-client.less @@ -30,6 +30,10 @@ float: right; width: 50%; + .rest-client-open-in-editor { + float: right; + } + @media only screen and (min-width: 800px) { padding-top: 0; } From 53dccdb92816b5210aadc1659e9df32dc4b88a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Mon, 10 Oct 2016 19:31:01 +0200 Subject: [PATCH 3/3] Add a spec. --- lib/rest-client-view.coffee | 9 ++++++--- spec/rest-client-view-spec.coffee | 6 ++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/rest-client-view.coffee b/lib/rest-client-view.coffee index 27b9dd1..8ffc67a 100644 --- a/lib/rest-client-view.coffee +++ b/lib/rest-client-view.coffee @@ -196,11 +196,13 @@ class RestClientView extends ScrollView openInEditor: -> textResult = $(rest_form.result).text() - extension = mime.extension(@lastResponse.headers['content-type']); - file_name = "#{@lastRequest.method} #{@lastRequest.url}#{'.' + extension if extension}" - editor = new RestClientEditor(textResult, file_name) + editor = new RestClientEditor(textResult, @constructFileName()) editor.open() + constructFileName: -> + extension = mime.extension(@lastResponse.headers['content-type']) + "#{@lastRequest.method} #{@lastRequest.url}#{'.' + extension if extension}" + encodePayload: -> $(rest_form.payload).val( RestClientHttp.encodePayload($(rest_form.payload).val()) @@ -286,6 +288,7 @@ class RestClientView extends ScrollView else @showErrorResponse(DEFAULT_NORESPONSE) result = error + $(rest_form.result).text(result) $(rest_form.result_headers).text(headers).hide() @emitter.emit RestClientEvent.REQUEST_FINISHED, response diff --git a/spec/rest-client-view-spec.coffee b/spec/rest-client-view-spec.coffee index 86ca5a7..b12976b 100644 --- a/spec/rest-client-view-spec.coffee +++ b/spec/rest-client-view-spec.coffee @@ -9,3 +9,9 @@ describe "RestClientView test", -> describe "View", -> it "the view is loaded", -> expect(restClient.find('.rest-client-send')).toExist() + + describe "constructFileName", -> + it "contains the url, the method and an extension inferred from the response content-type", -> + restClient.setLastRequest { url: "http://example.com", method: "GET" } + restClient.setLastResponse { headers: { "content-type": "application/json" } } + expect(restClient.constructFileName()).toEqual "GET http://example.com.json"