Sha256: 58aa959099c6285a454ca49729ca8536c3677847310a4fc9dc78bb8341489cbb

Contents?: true

Size: 1.19 KB

Versions: 12

Compression:

Stored size: 1.19 KB

Contents

require 'net/https'

module Scaffoldhub

  class NotFoundException < RuntimeError
  end

  class NetworkErrorException < RuntimeError
  end

  class RemoteFile

    attr_accessor :url

    def initialize(url = nil, status_proc = nil)
      @status_proc = status_proc
      @url = url
    end

    def exists?
      http_request.code.to_i == 200
    end

    def remote_file_contents!
      resp = http_request
      if resp.code.to_i == 200
        resp.body
      elsif resp.code.to_i == 404
        raise NotFoundException.new(url)
      else
        raise NetworkErrorException.new(url)
      end
    end

    def http_request
      begin
        @status_proc.call(url) if @status_proc
        uri = URI.parse(url)
        http = Net::HTTP.new(uri.host, uri.port)
        if uri.port == 443
          http.use_ssl = true
          http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        end
        http.start do |http|
          http.get(uri.path)
        end
      rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Errno::ECONNREFUSED,
             Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
        raise NetworkErrorException.new(url)
      end
    end

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
scaffoldhub-0.1.3 lib/scaffoldhub/remote_file.rb
scaffoldhub-0.1.2 lib/scaffoldhub/remote_file.rb
scaffoldhub-0.1.1 lib/scaffoldhub/remote_file.rb
scaffoldhub-0.1.0 lib/scaffoldhub/remote_file.rb
scaffoldhub-0.0.14 lib/scaffoldhub/remote_file.rb
scaffoldhub-0.0.13 lib/scaffoldhub/remote_file.rb
scaffoldhub-0.0.12 lib/scaffoldhub/remote_file.rb
scaffoldhub-0.0.11 lib/scaffoldhub/remote_file.rb
scaffoldhub-0.0.10 lib/scaffoldhub/remote_file.rb
scaffoldhub-0.0.8 lib/scaffoldhub/remote_file.rb
scaffoldhub-0.0.7 lib/scaffoldhub/remote_file.rb
scaffoldhub-0.0.6 lib/scaffoldhub/remote_file.rb