-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from javaguirre/http-refactor
Http and response refactor
- Loading branch information
Showing
5 changed files
with
154 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
fs = require 'fs' | ||
|
||
RestClientResponse = require './rest-client-response' | ||
|
||
module.exports = | ||
class RestClientEditor | ||
TMP_DIR_ERROR_MESSAGE: 'Cannot save to tmp directory..' | ||
|
||
constructor: (text, file_name) -> | ||
@text = text | ||
@file_name = @processFilename(file_name) | ||
@path = "/tmp/#{@file_name}" | ||
|
||
open: -> | ||
openned = false | ||
|
||
if [RestClientResponse.DEFAULT_RESULT, ""].indexOf(@text) == -1 | ||
# ideally, i want to open it without saving a file, but i don't think that'll work due to atom limitations | ||
fs.writeFile(@path, @text, @processOpen) | ||
openned = true | ||
|
||
openned | ||
|
||
processOpen: (err) => | ||
if err | ||
@showErrorOnOpen(err) | ||
else | ||
@openOnWorkspace(@path) | ||
|
||
processFilename: (file_name) -> | ||
file_name.replace(/https?:\/\//, '').replace(/\//g, '') | ||
|
||
showErrorOnOpen: (err) -> | ||
atom.confirm( | ||
message: @TMP_DIR_ERROR_MESSAGE, | ||
detailedMessage: JSON.stringify(err) | ||
) | ||
|
||
openOnWorkspace: (path) -> | ||
atom.workspace.open(path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
request = require 'request' | ||
|
||
module.exports = | ||
class RestClientHttp | ||
@METHODS: [ | ||
'get', | ||
'post', | ||
'put', | ||
'patch', | ||
'delete', | ||
'head', | ||
'options' | ||
] | ||
|
||
@encodePayload: (payload) -> | ||
encodeURIComponent(payload) | ||
|
||
@decodePayload: (payload) -> | ||
decodeURIComponent(payload) | ||
|
||
@send: (request_options, callback) -> | ||
request(request_options, callback) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
RestClientEditor = require '../lib/rest-client-editor' | ||
RestClientResponse = require '../lib/rest-client-response' | ||
|
||
describe 'RestClientEditor', -> | ||
DEFAULT_TEXT = 'body content' | ||
DEFAULT_FILENAME = 'GET - http://example.com' | ||
|
||
it 'Process filename', -> | ||
editor = new RestClientEditor(DEFAULT_TEXT, DEFAULT_FILENAME) | ||
|
||
expect(editor.processFilename(DEFAULT_FILENAME)).toEqual('GET - example.com') | ||
|
||
it 'Process filename url with a path', -> | ||
file_name = DEFAULT_FILENAME + '/path/' | ||
editor = new RestClientEditor(DEFAULT_TEXT, file_name) | ||
expect(editor.processFilename(file_name)).toEqual('GET - example.compath') | ||
|
||
it 'Open file', -> | ||
editor = new RestClientEditor(DEFAULT_TEXT, DEFAULT_FILENAME) | ||
expect(editor.open()).toEqual(true) | ||
|
||
it 'Not open file, no body content', -> | ||
text = "" | ||
editor = new RestClientEditor(text, DEFAULT_FILENAME) | ||
|
||
expect(editor.open()).toEqual(false) | ||
|
||
it 'Not open file, default body', -> | ||
text = RestClientResponse.DEFAULT_RESULT | ||
editor = new RestClientEditor(text, DEFAULT_FILENAME) | ||
|
||
expect(editor.open()).toEqual(false) |