Class: DataPackage::Package

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#metadataObject (readonly)

Returns the value of attribute metadata.



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

def 
  @metadata
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
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)



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

#contributorsObject



103
104
105
# File 'lib/datapackage/package.rb', line 103

def contributors
    @metadata["contributors"] || []
end

#datapackage_versionObject

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

#dependenciesObject



115
116
117
# File 'lib/datapackage/package.rb', line 115

def dependencies
    @metadata["dependencies"]
end

#descriptionObject



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

def description
    @metadata["description"]
end

#homepageObject



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

def homepage
    @metadata["homepage"]
end

#imageObject



95
96
97
# File 'lib/datapackage/package.rb', line 95

def image
    @metadata["image"]
end

#keywordsObject



87
88
89
# File 'lib/datapackage/package.rb', line 87

def keywords
    @metadata["keywords"] || []
end

#last_modifiedObject



91
92
93
# File 'lib/datapackage/package.rb', line 91

def last_modified
    DateTime.parse @metadata["last_modified"] rescue nil 
end

#licensesObject 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

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/datapackage/package.rb', line 47

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

#maintainersObject



99
100
101
# File 'lib/datapackage/package.rb', line 99

def maintainers
    @metadata["maintainers"] || []
end

#nameObject



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

#publisherObject



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

Returns:

  • (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

#resourcesObject



111
112
113
# File 'lib/datapackage/package.rb', line 111

def resources
    @metadata["resources"] || []
end

#sourcesObject



83
84
85
# File 'lib/datapackage/package.rb', line 83

def sources
    @metadata["sources"] || []
end

#titleObject



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

def title
    @metadata["title"]
end

#valid?(profile = :datapackage, strict = false) ⇒ Boolean

Returns:

  • (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

#versionObject

What is the version of this specific data package?



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

def version
    @metadata["version"]
end