Class: DataPackage::Package
- Inherits:
-
Object
- Object
- DataPackage::Package
- Defined in:
- lib/datapackage/package.rb
Instance Attribute Summary collapse
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
-
#base ⇒ Object
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).
- #contributors ⇒ Object
-
#datapackage_version ⇒ Object
What version of datapackage specification is this using?.
- #dependencies ⇒ Object
- #description ⇒ Object
- #homepage ⇒ Object
- #image ⇒ Object
-
#initialize(package, opts = {}) ⇒ Package
constructor
Parse a data package.
- #keywords ⇒ Object
- #last_modified ⇒ Object
- #licenses ⇒ Object (also: #licences)
-
#local? ⇒ Boolean
Is this a local package? Returns true if created from an in-memory object or a file/directory reference.
- #maintainers ⇒ Object
- #name ⇒ Object
- #property(property, default = nil) ⇒ Object
- #publisher ⇒ Object
- #resolve(path) ⇒ Object
- #resolve_resource(resource) ⇒ Object
- #resource_exists?(location) ⇒ Boolean
- #resources ⇒ Object
- #sources ⇒ Object
- #title ⇒ Object
- #valid?(profile = :datapackage, strict = false) ⇒ Boolean
- #validate(profile = :datapackage) ⇒ Object
-
#version ⇒ Object
What is the version of this specific data package?.
Constructor Details
#initialize(package, opts = {}) ⇒ Package
Parse a data package
Supports reading data from JSON file, directory, and a URL
- package
-
Hash or a String
- opts
-
Options used to customize reading and parsing
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/datapackage/package.rb', line 15 def initialize(package, opts={}) @opts = opts #TODO base directory/url if package.class == Hash @metadata = package else if !package.start_with?("http") && File.directory?(package) package = File.join(package, opts[:default_filename] || "datapackage.json") end if package.start_with?("http") && !package.end_with?("datapackage.json") package = URI.join(package, "datapackage.json") end @location = package.to_s @metadata = JSON.parse( open(package).read ) end end |
Instance Attribute Details
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
7 8 9 |
# File 'lib/datapackage/package.rb', line 7 def @metadata end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
7 8 9 |
# File 'lib/datapackage/package.rb', line 7 def opts @opts end |
Instance Method Details
#base ⇒ Object
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)
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/datapackage/package.rb', line 34 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 |
#contributors ⇒ Object
103 104 105 |
# File 'lib/datapackage/package.rb', line 103 def contributors @metadata["contributors"] || [] end |
#datapackage_version ⇒ Object
What version of datapackage specification is this using?
74 75 76 |
# File 'lib/datapackage/package.rb', line 74 def datapackage_version @metadata["datapackage_version"] end |
#dependencies ⇒ Object
115 116 117 |
# File 'lib/datapackage/package.rb', line 115 def dependencies @metadata["dependencies"] end |
#description ⇒ Object
60 61 62 |
# File 'lib/datapackage/package.rb', line 60 def description @metadata["description"] end |
#homepage ⇒ Object
64 65 66 |
# File 'lib/datapackage/package.rb', line 64 def homepage @metadata["homepage"] end |
#image ⇒ Object
95 96 97 |
# File 'lib/datapackage/package.rb', line 95 def image @metadata["image"] end |
#keywords ⇒ Object
87 88 89 |
# File 'lib/datapackage/package.rb', line 87 def keywords @metadata["keywords"] || [] end |
#last_modified ⇒ Object
91 92 93 |
# File 'lib/datapackage/package.rb', line 91 def last_modified DateTime.parse @metadata["last_modified"] rescue nil end |
#licenses ⇒ Object Also known as: licences
68 69 70 |
# File 'lib/datapackage/package.rb', line 68 def licenses @metadata["licenses"] || [] end |
#local? ⇒ Boolean
Is this a local package? Returns true if created from an in-memory object or a file/directory reference
47 48 49 50 |
# File 'lib/datapackage/package.rb', line 47 def local? return !@location.start_with?("http") if @location return true end |
#maintainers ⇒ Object
99 100 101 |
# File 'lib/datapackage/package.rb', line 99 def maintainers @metadata["maintainers"] || [] end |
#name ⇒ Object
52 53 54 |
# File 'lib/datapackage/package.rb', line 52 def name @metadata["name"] end |
#property(property, default = nil) ⇒ Object
119 120 121 |
# File 'lib/datapackage/package.rb', line 119 def property(property, default=nil) @metadata[property] || default end |
#publisher ⇒ Object
107 108 109 |
# File 'lib/datapackage/package.rb', line 107 def publisher @metadata["publisher"] || [] end |
#resolve(path) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/datapackage/package.rb', line 137 def resolve(path) if local? return File.join( base , path) if base != "" return path else return URI.join(base, path) end end |
#resolve_resource(resource) ⇒ Object
133 134 135 |
# File 'lib/datapackage/package.rb', line 133 def resolve_resource(resource) return resource["url"] || resolve( resource["path"] ) end |
#resource_exists?(location) ⇒ Boolean
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/datapackage/package.rb', line 146 def resource_exists?(location) if !location.to_s.start_with?("http") return File.exists?( location ) else begin status = RestClient.head( location ).code return status == 200 rescue => e return false end end end |
#resources ⇒ Object
111 112 113 |
# File 'lib/datapackage/package.rb', line 111 def resources @metadata["resources"] || [] end |
#sources ⇒ Object
83 84 85 |
# File 'lib/datapackage/package.rb', line 83 def sources @metadata["sources"] || [] end |
#title ⇒ Object
56 57 58 |
# File 'lib/datapackage/package.rb', line 56 def title @metadata["title"] end |
#valid?(profile = :datapackage, strict = false) ⇒ Boolean
123 124 125 126 |
# File 'lib/datapackage/package.rb', line 123 def valid?(profile=:datapackage, strict=false) validator = DataPackage::Validator.create(profile, @opts) return validator.valid?(self, strict) end |
#validate(profile = :datapackage) ⇒ Object
128 129 130 131 |
# File 'lib/datapackage/package.rb', line 128 def validate(profile=:datapackage) validator = DataPackage::Validator.create(profile, @opts) return validator.validate(self) end |
#version ⇒ Object
What is the version of this specific data package?
79 80 81 |
# File 'lib/datapackage/package.rb', line 79 def version @metadata["version"] end |