Class: Inspec::Targets::UrlHelper
- Inherits:
-
Object
- Object
- Inspec::Targets::UrlHelper
show all
- Defined in:
- lib/inspec/targets/url.rb
Instance Method Summary
collapse
Instance Method Details
#download_archive(url, archive, opts) ⇒ Object
download url into archive using opts, returns File object and content-type from HTTP headers
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/inspec/targets/url.rb', line 36
def download_archive(url, archive, opts)
archive.binmode
remote = open(
url,
http_basic_authentication: [opts['user'] || '', opts['password'] || ''],
)
archive.write(remote.read)
archive.rewind
archive.close
[archive, remote.meta['content-type']]
end
|
#handles?(target) ⇒ Boolean
12
13
14
15
16
17
18
|
# File 'lib/inspec/targets/url.rb', line 12
def handles?(target)
uri = URI.parse(target)
return false if uri.nil? or uri.scheme.nil?
%{ http https }.include? uri.scheme
rescue URI::Error => _e
false
end
|
#resolve(target, opts = {}) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/inspec/targets/url.rb', line 20
def resolve(target, opts = {})
return nil unless target.start_with?('https://', 'http://')
m = %r{^https?://(www\.)?github\.com/(?<user>[\w-]+)/(?<repo>[\w-]+)(\.git)?(/)?$}.match(target)
if m
url = "https://github.com/#{m[:user]}/#{m[:repo]}/archive/master.tar.gz"
else
url = target
end
resolve_archive(url, opts)
end
|
#resolve_archive(url, opts) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/inspec/targets/url.rb', line 51
def resolve_archive(url, opts)
archive, content_type = download_archive(url, Tempfile.new(['inspec-dl-', '.tar.gz']), opts)
if ['application/x-zip-compressed', 'application/zip'].include?(content_type)
pn = Pathname.new(archive.path)
new_path = pn.dirname.join(pn.basename.to_s.gsub('tar.gz', 'zip'))
File.rename(pn.to_s, new_path.to_s)
content = ZipHelper.new.resolve(new_path)
File.unlink(new_path)
elsif ['application/x-gzip', 'application/gzip'].include?(content_type)
content = TarHelper.new.resolve(archive.path)
archive.unlink
end
content
end
|
#to_s ⇒ Object
72
73
74
|
# File 'lib/inspec/targets/url.rb', line 72
def to_s
'URL Loader'
end
|