lib/lolcommits/plugin/lolsrv.rb in lolcommits-lolsrv-0.0.3 vs lib/lolcommits/plugin/lolsrv.rb in lolcommits-lolsrv-0.0.4

- old
+ new

@@ -45,11 +45,11 @@ options = super if options['enabled'] print "server: " options.merge!('server' => parse_user_input(gets.strip)) puts '---------------------------------------------------------------' - puts ' Lolsrv - Upload and sync lolcommits to a remote server' + puts ' Lolsrv - Sync lolcommits to a remote server' puts '' puts ' Handle POST /uplol with these request params' puts '' puts ' `lol` - captured lolcommit image file' puts ' `url` - remote repository URL (with commit SHA appended)' @@ -81,69 +81,58 @@ private ## - # Message to show if syncing fails # - # @return [String] message text - # - def fail_message - "failed :( (try again with --debug)\n" - end - - ## - # # Syncs lolcommmit images to the remote server # - # Fetches from /lols and iterates over objects in the JSON array - # For each image found in the local loldir folder, check if it has already - # been uploaded. If not upload the image with a POST request and + # Fetches from /lols and iterates over shas in the JSON array. Then for + # each image found in the local loldir folder, check if it has already + # been uploaded. If not, upload the image with a POST request and # upload_params. # - # Upload requests that fail are skipped. + # Upload requests that fail abort the sync # def sync - print "Syncing with lolsrv ... " - existing = existing_lols + print "Syncing lols ... " + raise 'failed fetching existing lols' unless existing_shas - # abort sync when invalid response or error from lols_endpoint - unless existing - print fail_message; return - end - Dir[runner.config.loldir + '/*.{jpg,gif}'].each do |image| sha = File.basename(image, '.*') - response = upload(image, sha) unless existing.include?(sha) || sha == 'tmp_snapshot' - unless response - print fail_message; return + unless existing_shas.include?(sha) || sha == 'tmp_snapshot' + response = upload(image, sha) + raise "failed uploading #{image}" if response.nil? end end print "done!\n" + rescue RuntimeError => e + print "#{e.message} (try again with --debug)\n" end ## # # Fetch and parse JSON response from `server/lols`, returning an array of - # commit SHA's. Logs error and returns nil on NET/HTTP and JSON parsing - # errors. + # commit SHA's. Logs and returns nil on NET/HTTP and JSON parsing errors. # # @return [Array] containing commit SHA's # @return [Nil] if an error occurred # - def existing_lols - lols = JSON.parse(RestClient.get(lols_endpoint)) - lols.map { |lol| lol['sha'] } - rescue JSON::ParserError, SocketError, RestClient::RequestFailed => e - log_error(e, "ERROR: existing lols could not be retrieved #{e.class} - #{e.message}") - return nil + def existing_shas + @existing_shas ||= begin + lols = JSON.parse(RestClient.get(lols_endpoint)) + lols.map { |lol| lol['sha'] } + rescue JSON::ParserError, SocketError, RestClient::RequestFailed => e + log_error(e, "ERROR: existing lols could not be retrieved #{e.class} - #{e.message}") + return nil + end end ## # # Upload the lolcommit image to `server/uplol` with commit params. Logs - # error and returns nil on NET/HTTP errors. + # and returns nil on NET/HTTP errors. # # @return [RestClient::Response] response object from POST request # def upload(image, sha) RestClient.post(upload_endpoint, upload_params_for(image, sha))