-
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 #29 from ddavison/save-load-requests
Save load requests
- Loading branch information
Showing
9 changed files
with
383 additions
and
25 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.DS_Store | ||
npm-debug.log | ||
node_modules | ||
recent.json | ||
collections.json |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,4 @@ | ||
module.exports = | ||
class RestClientEvent | ||
@NEW_REQUEST = 'new-request' | ||
@REQUEST_FINISHED = 'request-finished' |
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,73 @@ | ||
fs = require 'fs' | ||
|
||
module.exports = | ||
class RestClientPersist | ||
REQUEST_FILE_LIMIT: 100 | ||
requests: [] | ||
requestFileLimit: @REQUEST_FILE_LIMIT | ||
|
||
constructor: (path) -> | ||
@path = path | ||
@initPath() | ||
|
||
load: (callback) -> | ||
fs.readFile(@path, callback) | ||
|
||
save: (request) => | ||
@requests.unshift(request) | ||
@requests = @requests.slice(0, @REQUESTS_LIMIT) | ||
@saveFile() | ||
|
||
initPath: -> | ||
try | ||
stat = fs.lstatSync(@path) | ||
if !stat.isFile() | ||
@saveFile() | ||
catch statErr | ||
@saveFile() | ||
|
||
saveFile: -> | ||
requestsToBeSaved = @get(@requestFileLimit) | ||
fs.writeFile( | ||
"#{@path}", | ||
JSON.stringify(requestsToBeSaved), | ||
@showErrorOnPersist | ||
) | ||
|
||
update: (requests) -> | ||
@requests = requests | ||
|
||
get: (limit = false) -> | ||
if limit | ||
return @requests.slice(0, limit) | ||
|
||
@requests | ||
|
||
remove: (removed_request) -> | ||
for request, index in @requests | ||
if @requestEquals(removed_request, request) | ||
@requests.splice(index, 1) | ||
@saveFile() | ||
break | ||
|
||
requestEquals: (request1, request2) -> | ||
return (request1.url == request2.url and | ||
request1.method == request2.method) | ||
|
||
|
||
showErrorOnPersist: (err) => | ||
if err | ||
atom.confirm( | ||
message: 'Cannot save file: ' + @path, | ||
detailedMessage: JSON.stringify(err) | ||
) | ||
|
||
getRequestFileLimit: () -> | ||
return @requestFileLimit | ||
|
||
setRequestFileLimit: (limit) -> | ||
@requestFileLimit = limit | ||
|
||
clear: -> | ||
@requests = [] | ||
@saveFile() |
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,4 @@ | ||
module.exports = | ||
class RestClientRequest | ||
constructor: (data) -> | ||
@data = data |
Oops, something went wrong.