class LicenseScout::Exporter::CSV

Attributes

json[R]
output_file[R]

Public Class Methods

new(json_file) click to toggle source
# File lib/license_scout/exporter/csv.rb, line 27
def initialize(json_file)
  @json = FFI_Yajl::Parser.parse(File.read(json_file))
  @output_file = json_file.gsub("json", "csv")
end

Public Instance Methods

export() click to toggle source
# File lib/license_scout/exporter/csv.rb, line 32
def export
  headers = [
    "Type",
    "Name",
    "Version",
    "Has Exception",
    "Exception Reason",
    "License ID",
    "License Source",
    "License Content",
  ]

  ::CSV.open(output_file, "w+") do |csv|
    csv << headers

    json["dependencies"].each do |dependency|
      type = dependency["type"]
      name = dependency["name"]
      version = dependency["version"]
      has_exception = dependency["has_exception"]
      exception_reason = dependency["exception_reason"]
      licenses = dependency["licenses"]

      licenses.each do |license|
        id = license["id"]
        source = license["source"]
        content = license["content"]

        csv << [
          type,
          name,
          version,
          (has_exception.nil? ? "Yes" : "No"),
          (exception_reason.nil? ? "" : exception_reason),
          id,
          source,
          content,
        ]
      end
    end
  end
end