class Pakyow::Assets::External::Downloader

@api private

Attributes

body[R]
path[R]
status[R]

Public Class Methods

new(uri) click to toggle source
# File lib/pakyow/assets/external.rb, line 101
def initialize(uri)
  @uri = URI.parse(uri)
end

Public Instance Methods

perform() click to toggle source
# File lib/pakyow/assets/external.rb, line 105
def perform
  get(@uri.path); self
end

Private Instance Methods

build_uri(path) click to toggle source
# File lib/pakyow/assets/external.rb, line 138
        def build_uri(path)
  File.join("#{@uri.scheme}://#{@uri.host}", path)
end
get(path) click to toggle source
# File lib/pakyow/assets/external.rb, line 109
        def get(path)
  @path = path
  internet = Async::HTTP::Internet.new

  Async do
    begin
      response = internet.get(build_uri(path))
      @status = response.status

      if response.status == 301 || response.status == 302
        get(response.headers["location"])
      elsif response.status >= 500
        raise Failed, "Unexpected response status: #{response.status}"
      else
        @body = String.new
        while body = response.body.read
          @body << body
        end
      end
    rescue SocketError => error
      raise Failed.build(error)
    ensure
      response&.close
    end
  end.wait
ensure
  internet&.close
end