class Ssg::Downloader

Downloads SCAP datastreams from the SCAP Security Guide github.com/ComplianceAsCode/content

Constants

RELEASES_API
SSG_DS_REGEX

Public Class Methods

download!(versions = []) click to toggle source
# File lib/ssg/downloader.rb, line 20
def self.download!(versions = [])
  versions.uniq.map do |version|
    [version, new(version).fetch_datastream_file]
  end.to_h
end
new(version = 'latest') click to toggle source
# File lib/ssg/downloader.rb, line 14
def initialize(version = 'latest')
  @release_uri = URI(
    "#{RELEASES_API}#{'tags/' unless version[/^latest$/]}#{version}"
  )
end

Public Instance Methods

fetch_datastream_file() click to toggle source
# File lib/ssg/downloader.rb, line 26
def fetch_datastream_file
  puts "Fetching #{datastream_filename}"
  get_chunked(datastream_uri)

  datastream_filename
end

Private Instance Methods

check_response(response, &block) click to toggle source
# File lib/ssg/downloader.rb, line 64
def check_response(response, &block)
  case response
  when Net::HTTPSuccess
    response
  when Net::HTTPRedirection
    get(URI(response['location']), &block)
  else
    response.value
  end
end
datastream_filename() click to toggle source
# File lib/ssg/downloader.rb, line 86
def datastream_filename
  datastream_uri.path.split('/').last[SSG_DS_REGEX]
end
datastream_uri() click to toggle source
# File lib/ssg/downloader.rb, line 35
def datastream_uri
  @datastream_uri ||= URI(
    download_urls.find { |url| url[SSG_DS_REGEX] }
  )
end
download_urls() click to toggle source
# File lib/ssg/downloader.rb, line 41
def download_urls
  get_json(@release_uri).dig('assets').map do |asset|
    asset.dig('browser_download_url')
  end
end
fetch(request, &block) click to toggle source
# File lib/ssg/downloader.rb, line 47
def fetch(request, &block)
  Net::HTTP.start(
    request.uri.host, request.uri.port,
    use_ssl: request.uri.scheme['https']
  ) do |http|
    check_response(http.request(request, &block), &block)
  end
end
get(uri, &block) click to toggle source
# File lib/ssg/downloader.rb, line 56
def get(uri, &block)
  fetch(Net::HTTP::Get.new(uri), &block)
end
get_chunked(uri, filename: datastream_filename) click to toggle source
# File lib/ssg/downloader.rb, line 75
def get_chunked(uri, filename: datastream_filename)
  head(uri) do |response|
    next unless Net::HTTPSuccess === response
    open(filename, 'wb') do |file|
      response.read_body do |chunk|
        file.write(chunk)
      end
    end
  end
end
get_json(uri) click to toggle source
# File lib/ssg/downloader.rb, line 90
def get_json(uri)
  JSON.parse(get(uri).body)
end
head(uri, &block) click to toggle source
# File lib/ssg/downloader.rb, line 60
def head(uri, &block)
  fetch(Net::HTTP::Head.new(uri), &block)
end