class DataPackage::Resource
Attributes
errors[R]
Public
name[R]
Public
profile[R]
Public
source[R]
Public
Public Class Methods
infer(filepath)
click to toggle source
# File lib/datapackage/resource.rb, line 9 def self.infer(filepath) name = File.basename(filepath) if name[-4..-1] != '.csv' raise ResourceException.new('Inferrable resource must have .csv extension') end descr = { 'format' => 'csv', 'mediatype' => 'text/csv', 'name' => name[0...-4], 'path' => filepath, 'schema' => { 'fields' => [] }, } csv = CSV.read(filepath, headers: true) interpreter = DataPackage::Interpreter.new(csv) csv.headers.each do |header| field = { 'name' => header, 'type' => 'string'} field.merge! interpreter.type_and_format_at(header) descr['schema']['fields'] << field end new(descr) end
new(resource, base_path = '')
click to toggle source
# File lib/datapackage/resource.rb, line 36 def initialize(resource, base_path = '') self.merge! dereference_descriptor(resource, base_path: base_path, reference_fields: ['schema', 'dialect']) apply_defaults! @profile = DataPackage::Profile.new(self['profile']) @name = self.fetch('name') get_source!(base_path) apply_table_defaults! if self.tabular? end
Public Instance Methods
descriptor()
click to toggle source
# File lib/datapackage/resource.rb, line 60 def descriptor self.to_h end
headers()
click to toggle source
# File lib/datapackage/resource.rb, line 97 def headers if !tabular nil end get_table.headers end
inline?()
click to toggle source
# File lib/datapackage/resource.rb, line 64 def inline? @source_type == 'inline' end
Also aliased as: inline
iter(*args, &block)
click to toggle source
# File lib/datapackage/resource.rb, line 111 def iter(*args, &block) if !tabular message ='Methods iter/read are not supported for non tabular data' raise ResourceException.new message end get_table.iter(*args, &block) end
iter_errors() { |err| ... }
click to toggle source
# File lib/datapackage/resource.rb, line 56 def iter_errors @profile.iter_errors(self){ |err| yield err } end
local?()
click to toggle source
# File lib/datapackage/resource.rb, line 70 def local? @source_type == 'local' end
Also aliased as: local
miltipart?()
click to toggle source
# File lib/datapackage/resource.rb, line 82 def miltipart? false end
Also aliased as: miltipart
read(*args, &block)
click to toggle source
# File lib/datapackage/resource.rb, line 119 def read(*args, &block) if !tabular message ='Methods iter/read are not supported for non tabular data' raise ResourceException.new message end get_table.read(*args, &block) end
remote?()
click to toggle source
# File lib/datapackage/resource.rb, line 76 def remote? @source_type == 'remote' end
Also aliased as: remote
schema()
click to toggle source
# File lib/datapackage/resource.rb, line 104 def schema if !tabular nil end get_table.schema end
table()
click to toggle source
Deprecated
# File lib/datapackage/resource.rb, line 129 def table get_table end
tabular?()
click to toggle source
# File lib/datapackage/resource.rb, line 88 def tabular? tabular_profile = DataPackage::DEFAULTS[:resource][:tabular_profile] return true if @profile.name == tabular_profile return true if DataPackage::Profile.new(tabular_profile).valid?(self) false end
Also aliased as: tabular
valid?()
click to toggle source
# File lib/datapackage/resource.rb, line 46 def valid? @profile.valid?(self) end
Also aliased as: valid
validate()
click to toggle source
# File lib/datapackage/resource.rb, line 52 def validate @profile.validate(self) end
Private Instance Methods
apply_defaults!()
click to toggle source
# File lib/datapackage/resource.rb, line 156 def apply_defaults! self['profile'] ||= DataPackage::DEFAULTS[:resource][:profile] self['encoding'] ||= DataPackage::DEFAULTS[:resource][:encoding] end
apply_table_defaults!()
click to toggle source
# File lib/datapackage/resource.rb, line 161 def apply_table_defaults! self['profile'] = DataPackage::DEFAULTS[:resource][:tabular_profile] if self.fetch('schema', nil) self['schema']['missingValues'] = DataPackage::DEFAULTS[:schema][:missing_values] self['schema'].fetch('fields', []).each do |field_descriptor| field_descriptor['type'] ||= DataPackage::DEFAULTS[:schema][:type] field_descriptor['format'] ||= DataPackage::DEFAULTS[:schema][:format] end end if self.fetch('dialect', nil) DataPackage::DEFAULTS[:dialect].each do |key, val| self['dialect'][key.to_s] ||= val end end end
get_source!(base_path)
click to toggle source
Private
# File lib/datapackage/resource.rb, line 137 def get_source!(base_path) if self.fetch('data', nil) @source = self['data'] @source_type = 'inline' elsif self.fetch('path', nil) unless is_safe_path?(self['path']) raise ResourceException.new "Path `#{self['path']}` is not safe" end @source = join_paths(base_path, self['path']) @source_type = is_fully_qualified_url?(@source) ? 'remote' : 'local' else raise ResourceException.new 'A resource descriptor must have a `path` or `data` property.' end end
get_table()
click to toggle source
# File lib/datapackage/resource.rb, line 152 def get_table @table ||= TableSchema::Table.new(self.source, schema: self['schema']) if tabular? end