lib/ridley/connection.rb in ridley-1.0.0.rc3 vs lib/ridley/connection.rb in ridley-1.0.0

- old
+ new

@@ -4,10 +4,11 @@ require 'zlib' module Ridley class Connection < Faraday::Connection include Celluloid + task_class TaskThread VALID_OPTIONS = [ :retries, :retry_interval, :ssl, @@ -98,11 +99,11 @@ end # Override Faraday::Connection#run_request to catch exceptions from {Ridley::Middleware} that # we expect. Caught exceptions are re-raised with Celluloid#abort so we don't crash the connection. def run_request(*args) - defer { super } + super rescue Errors::HTTPError => ex abort ex rescue Faraday::Error::ConnectionFailed => ex abort Errors::ConnectionFailed.new(ex) rescue Faraday::Error::TimeoutError => ex @@ -138,25 +139,23 @@ end local = Tempfile.new('ridley-stream') local.binmode - defer { - retryable(tries: retries, on: OpenURI::HTTPError, sleep: retry_interval) do - open(target, 'rb', headers) do |remote| - body = remote.read - case remote.content_encoding - when ['gzip'] - body = Zlib::GzipReader.new(StringIO.new(body), encoding: 'ASCII-8BIT').read - when ['deflate'] - body = Zlib::Inflate.inflate(body) - end - local.write(body) + retryable(tries: retries, on: OpenURI::HTTPError, sleep: retry_interval) do + open(target, 'rb', headers) do |remote| + body = remote.read + case remote.content_encoding + when ['gzip'] + body = Zlib::GzipReader.new(StringIO.new(body), encoding: 'ASCII-8BIT').read + when ['deflate'] + body = Zlib::Inflate.inflate(body) end + local.write(body) end + end - local.flush - } + local.flush FileUtils.mv(local.path, destination) rescue OpenURI::HTTPError => ex abort(ex) ensure