Class: Inspec::Targets::UrlHelper
- Inherits:
-
Object
- Object
- Inspec::Targets::UrlHelper
- Defined in:
- lib/inspec/targets/url.rb
Instance Method Summary collapse
-
#download_archive(url, archive, opts) ⇒ Object
download url into archive using opts, returns File object and content-type from HTTP headers.
- #handles?(target) ⇒ Boolean
- #resolve(target, opts = {}) ⇒ Object
- #resolve_zip(url, opts) ⇒ Object
- #to_s ⇒ Object
Instance Method Details
#download_archive(url, archive, opts) ⇒ Object
download url into archive using opts, returns File object and content-type from HTTP headers
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/inspec/targets/url.rb', line 35 def download_archive(url, archive, opts) archive.binmode remote = open( url, http_basic_authentication: [opts['user'] || '', opts['password'] || ''], ) # download content archive.write(remote.read) archive.rewind archive.close [archive, remote.['content-type']] end |
#handles?(target) ⇒ Boolean
12 13 14 15 16 |
# 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 end |
#resolve(target, opts = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/inspec/targets/url.rb', line 18 def resolve(target, opts = {}) # abort if the target does not start with http or https return nil unless target.start_with?('https://', 'http://') # support for github url 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_zip(url, opts) end |
#resolve_zip(url, opts) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/inspec/targets/url.rb', line 50 def resolve_zip(url, opts) archive, content_type = download_archive(url, Tempfile.new(['inspec-dl-', '.tar.gz']), opts) # replace extension with zip if we detected a zip content type if ['application/x-zip-compressed', 'application/zip'].include?(content_type) # rename file for proper detection in DirHelper 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) # use tar helper as default (otherwise returns nil) content = TarHelper.new.resolve(archive.path) archive.unlink end content end |
#to_s ⇒ Object
71 72 73 |
# File 'lib/inspec/targets/url.rb', line 71 def to_s 'URL Loader' end |