Class: LicenseScout::Exporter::CSV

Inherits:
Object
  • Object
show all
Defined in:
lib/license_scout/exporter/csv.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_file) ⇒ CSV

Returns a new instance of CSV.



27
28
29
30
# 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

Instance Attribute Details

#jsonObject (readonly)

Returns the value of attribute json.



24
25
26
# File 'lib/license_scout/exporter/csv.rb', line 24

def json
  @json
end

#output_fileObject (readonly)

Returns the value of attribute output_file.



25
26
27
# File 'lib/license_scout/exporter/csv.rb', line 25

def output_file
  @output_file
end

Instance Method Details

#exportObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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