Class: DataPackage::Resource
- Inherits:
-
Hash
- Object
- Hash
- DataPackage::Resource
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
#errors ⇒ Object
7
8
9
|
# File 'lib/datapackage/resource.rb', line 7
def errors
@errors
end
|
#name ⇒ Object
7
8
9
|
# File 'lib/datapackage/resource.rb', line 7
def name
@name
end
|
#profile ⇒ Object
7
8
9
|
# File 'lib/datapackage/resource.rb', line 7
def profile
@profile
end
|
#source ⇒ Object
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..each do ||
field = { 'name' => , 'type' => 'string'}
field.merge! interpreter.type_and_format_at()
descr['schema']['fields'] << field
end
new(descr)
end
|
Instance Method Details
#descriptor ⇒ Object
60
61
62
|
# File 'lib/datapackage/resource.rb', line 60
def descriptor
self.to_h
end
|
97
98
99
100
101
102
|
# File 'lib/datapackage/resource.rb', line 97
def
if !tabular
nil
end
get_table.
end
|
#inline? ⇒ Boolean
Also known as:
inline
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_errors ⇒ Object
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
70
71
72
|
# File 'lib/datapackage/resource.rb', line 70
def local?
@source_type == 'local'
end
|
#miltipart? ⇒ Boolean
Also known as:
miltipart
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
76
77
78
|
# File 'lib/datapackage/resource.rb', line 76
def remote?
@source_type == 'remote'
end
|
#schema ⇒ Object
104
105
106
107
108
109
|
# File 'lib/datapackage/resource.rb', line 104
def schema
if !tabular
nil
end
get_table.schema
end
|
#table ⇒ Object
129
130
131
|
# File 'lib/datapackage/resource.rb', line 129
def table
get_table
end
|
#tabular? ⇒ Boolean
Also known as:
tabular
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
46
47
48
|
# File 'lib/datapackage/resource.rb', line 46
def valid?
@profile.valid?(self)
end
|
#validate ⇒ Object
52
53
54
|
# File 'lib/datapackage/resource.rb', line 52
def validate
@profile.validate(self)
end
|