Class: Scaffoldhub::RemoteFile

Inherits:
Object
  • Object
show all
Defined in:
lib/scaffoldhub/remote_file.rb

Direct Known Subclasses

ScaffoldSpec, TemplateFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, status_proc = nil) ⇒ RemoteFile

Returns a new instance of RemoteFile.



15
16
17
18
# File 'lib/scaffoldhub/remote_file.rb', line 15

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

Instance Attribute Details

#urlObject

Returns the value of attribute url.



13
14
15
# File 'lib/scaffoldhub/remote_file.rb', line 13

def url
  @url
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/scaffoldhub/remote_file.rb', line 20

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

#http_requestObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/scaffoldhub/remote_file.rb', line 35

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

#remote_file_contents!Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/scaffoldhub/remote_file.rb', line 24

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