Class: Cid::Datapackage
- Inherits:
-
Object
- Object
- Cid::Datapackage
- Defined in:
- lib/cid/datapackage.rb
Instance Attribute Summary collapse
-
#json ⇒ Object
Returns the value of attribute json.
Instance Method Summary collapse
- #create ⇒ Object
-
#initialize(path) ⇒ Datapackage
constructor
A new instance of Datapackage.
- #publish(token = ) ⇒ Object
- #schema_for_file(path) ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(path) ⇒ Datapackage
Returns a new instance of Datapackage.
10 11 12 13 14 15 16 17 |
# File 'lib/cid/datapackage.rb', line 10 def initialize(path) @path = path begin @datapackage = JSON.parse(File.new("#{@path}/datapackage.json").read) rescue return "No datapackage present!" end end |
Instance Attribute Details
#json ⇒ Object
Returns the value of attribute json.
8 9 10 |
# File 'lib/cid/datapackage.rb', line 8 def json @json end |
Instance Method Details
#create ⇒ Object
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 |
# File 'lib/cid/datapackage.rb', line 19 def create # Clear out all the resources @datapackage["resources"] = [] paths = Dir.glob("#{@path}/*/") paths.each do |path| schema = JSON.parse(File.new(Dir["#{path}/schema.json"][0]).read) Dir["#{path}*.csv"].each do |csv| @datapackage["resources"] << { name: File.basename(csv, ".csv"), path: csv.split("/").last(2).join("/"), format: "csv", mediatype: "text/csv", bytes: File.size(csv), schema: { fields: schema["fields"] } } end end @json = @datapackage.to_json end |
#publish(token = ) ⇒ Object
50 51 52 53 |
# File 'lib/cid/datapackage.rb', line 50 def publish(token = ENV['GITHUB_OAUTH_TOKEN']) git = Cid::Publish.new(@path, token) git.publish end |
#schema_for_file(path) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/cid/datapackage.rb', line 55 def schema_for_file(path) begin schema = @datapackage["resources"].select { |r| r["path"] == path }.first["schema"] Csvlint::Schema.from_json_table(nil, schema) rescue NoMethodError nil end end |
#write ⇒ Object
45 46 47 48 |
# File 'lib/cid/datapackage.rb', line 45 def write self.create if @json.nil? File.open("#{@path}/datapackage.json", 'w') { |f| f.write(@json) } end |