Skip to content

Commit

Permalink
Merge pull request #29 from ddavison/save-load-requests
Browse files Browse the repository at this point in the history
Save load requests
  • Loading branch information
ddavison committed Mar 15, 2016
2 parents fa9bfcd + 8132c07 commit 6152c94
Show file tree
Hide file tree
Showing 9 changed files with 383 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .gitignore
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
Binary file modified images/example.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions lib/rest-client-event.coffee
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'
73 changes: 73 additions & 0 deletions lib/rest-client-persist.coffee
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()
4 changes: 4 additions & 0 deletions lib/rest-client-request.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports =
class RestClientRequest
constructor: (data) ->
@data = data
Loading

0 comments on commit 6152c94

Please sign in to comment.