Method: Cid::Datapackage#create

Defined in:
lib/cid/datapackage.rb

#createObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cid/datapackage.rb', line 19

def create

  paths = Dir.glob("#{@path}/*/")

  paths.each do |path|

    if File.file?("#{path}schema.json")
      schema = JSON.parse(File.new(Dir["#{path}schema.json"][0]).read)
    else
      schema = nil
    end

    Dir["#{path}*.csv"].each do |csv|
      ref = csv.split("/").last(2).join("/")

      schema = schema_for_file(ref, true) rescue nil if schema.nil?

      resource = {
        name: File.basename(csv, ".csv"),
        path: ref,
        format: "csv",
        mediatype: "text/csv",
        bytes: File.size(csv)
      }

      resource[:schema] = { fields: schema["fields"] } unless schema.nil?

      @datapackage["resources"].reject! { |r| r["path"] == ref }
      @datapackage["resources"] << resource
    end
  end

  @json = @datapackage.to_json
end