Class: DataPackage::Resource

Inherits:
Hash
  • Object
show all
Includes:
Helpers
Defined in:
lib/datapackage/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#base_path, #dereference_descriptor, #is_fully_qualified_url?, #is_safe_path?, #join_paths, #load_json, #resolve_json_reference

Constructor Details

#initialize(resource, base_path = '') ⇒ Resource

Returns a new instance of Resource.



36
37
38
39
40
41
42
43
44
# 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

Instance Attribute Details

#errorsObject (readonly)

Public



7
8
9
# File 'lib/datapackage/resource.rb', line 7

def errors
  @errors
end

#nameObject (readonly)

Public



7
8
9
# File 'lib/datapackage/resource.rb', line 7

def name
  @name
end

#profileObject (readonly)

Public



7
8
9
# File 'lib/datapackage/resource.rb', line 7

def profile
  @profile
end

#sourceObject (readonly)

Public



7
8
9
# File 'lib/datapackage/resource.rb', line 7

def source
  @source
end

Class Method Details

.infer(filepath) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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

Instance Method Details

#descriptorObject



60
61
62
# File 'lib/datapackage/resource.rb', line 60

def descriptor
  self.to_h
end

#headersObject



97
98
99
100
101
102
# File 'lib/datapackage/resource.rb', line 97

def headers
  if !tabular
    nil
  end
  get_table.headers
end

#inline?Boolean Also known as: inline

Returns:

  • (Boolean)


64
65
66
# File 'lib/datapackage/resource.rb', line 64

def inline?
  @source_type == 'inline'
end

#iter(*args, &block) ⇒ Object



111
112
113
114
115
116
117
# 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_errorsObject



56
57
58
# File 'lib/datapackage/resource.rb', line 56

def iter_errors
  @profile.iter_errors(self){ |err| yield err }
end

#local?Boolean Also known as: local

Returns:

  • (Boolean)


70
71
72
# File 'lib/datapackage/resource.rb', line 70

def local?
  @source_type == 'local'
end

#miltipart?Boolean Also known as: miltipart

Returns:

  • (Boolean)


82
83
84
# File 'lib/datapackage/resource.rb', line 82

def miltipart?
  false
end

#read(*args, &block) ⇒ Object



119
120
121
122
123
124
125
# 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?Boolean Also known as: remote

Returns:

  • (Boolean)


76
77
78
# File 'lib/datapackage/resource.rb', line 76

def remote?
  @source_type == 'remote'
end

#schemaObject



104
105
106
107
108
109
# File 'lib/datapackage/resource.rb', line 104

def schema
  if !tabular
    nil
  end
  get_table.schema
end

#tableObject

Deprecated



129
130
131
# File 'lib/datapackage/resource.rb', line 129

def table
  get_table
end

#tabular?Boolean Also known as: tabular

Returns:

  • (Boolean)


88
89
90
91
92
93
# 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

#valid?Boolean Also known as: valid

Returns:

  • (Boolean)


46
47
48
# File 'lib/datapackage/resource.rb', line 46

def valid?
  @profile.valid?(self)
end

#validateObject



52
53
54
# File 'lib/datapackage/resource.rb', line 52

def validate
  @profile.validate(self)
end