Class: DataPackage::Package

Inherits:
Hash
  • Object
show all
Defined in:
lib/datapackage/package.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package = nil, schema = :base, opts = {}) ⇒ Package

Parse or create a data package

Supports reading data from JSON file, directory, and a URL

package

Hash or a String

schema

Hash, Symbol or String

opts

Options used to customize reading and parsing



15
16
17
18
19
20
21
22
23
# File 'lib/datapackage/package.rb', line 15

def initialize(package = nil, schema = :base, opts = {})
  @opts = opts
  @schema = DataPackage::Schema.new(schema || :base)
  @dead_resources = []

  self.merge! parse_package(package)
  define_properties!
  load_resources!
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/datapackage/package.rb', line 5

def errors
  @errors
end

#optsObject (readonly)

Returns the value of attribute opts.



5
6
7
# File 'lib/datapackage/package.rb', line 5

def opts
  @opts
end

#resourcesObject



57
58
59
60
# File 'lib/datapackage/package.rb', line 57

def resources
  update_resources!
  @resources
end

Instance Method Details

#baseObject

Returns the directory for a local file package or base url for a remote Returns nil for an in-memory object (because it has no base as yet)



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/datapackage/package.rb', line 38

def base
  # user can override base
  return @opts[:base] if @opts[:base]
  return '' unless @location
  # work out base directory or uri
  if local?
      return File.dirname(@location)
  else
      return @location.split('/')[0..-2].join('/')
  end
end

#local?Boolean

Is this a local package? Returns true if created from an in-memory object or a file/directory reference

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/datapackage/package.rb', line 51

def local?
  return @local if @local
  return !@location.start_with?('http') if @location
  true
end

#parse_package(package) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/datapackage/package.rb', line 25

def parse_package(package)
  # TODO: base directory/url
  if package.nil?
    {}
  elsif package.class == Hash
    package
  else
    read_package(package)
  end
end

#property(property, default = nil) ⇒ Object



62
63
64
# File 'lib/datapackage/package.rb', line 62

def property(property, default = nil)
  self[property] || default
end

#resource_exists?(location) ⇒ Boolean

Returns:

  • (Boolean)


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

def resource_exists?(location)
  @dead_resources.include?(location)
end

#to_jsonObject



80
81
82
# File 'lib/datapackage/package.rb', line 80

def to_json
  self.to_json
end

#valid?Boolean

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/datapackage/package.rb', line 66

def valid?
  validate
  @valid
end

#validateObject



71
72
73
74
# File 'lib/datapackage/package.rb', line 71

def validate
  @errors = @schema.validation_errors(self)
  @valid = @schema.valid?(self)
end