module DataKitten::PublishingFormats::Datapackage

Datapackage metadata format module. Automatically mixed into {Dataset} for datasets that include a datapackage.json.

@see Dataset

Private Class Methods

supported?(instance) click to toggle source
# File lib/data_kitten/publishing_formats/datapackage.rb, line 13
def self.supported?(instance)
  begin
    if instance.send(:origin) == :git
      metadata = instance.send(:load_file, "datapackage.json")
      datapackage = DataPackage::Package.new( JSON.parse( metadata ) )
      return datapackage.datapackage_version != nil
    else
      datapackage = DataPackage::Package.new( instance.url )
      return datapackage.datapackage_version != nil
    end
  rescue => e
    false
  end
end

Public Instance Methods

change_history() click to toggle source

A history of changes to the Dataset.

If {Dataset#source} is :git, this is the git changelog for the actual distribution files, rather then the full unfiltered log.

@return [Array] An array of changes. Exact format depends on the source.

@see Dataset#change_history

# File lib/data_kitten/publishing_formats/datapackage.rb, line 131
def change_history
  @change_history ||= begin
    if origin == :git
      # Get a log for each file in the local repo
      logs = distributions.map do |file|
        if file.path
          log = repository.log.path(file.path)
          # Convert to list of commits
          log.map{|commit| commit}
        else
          []
        end
      end
      # combine all logs, make unique, and re-sort in date order
      logs.flatten.uniq.sort_by{|x| x.committer.date}.reverse
    else
      []
    end
  end
end
contributors() click to toggle source

A list of contributors.

@see Dataset#contributors

# File lib/data_kitten/publishing_formats/datapackage.rb, line 75
def contributors
  package.contributors.map do |x|
    Agent.new(:name => x['name'], :uri => x['web'], :email => x['email'])
  end
end
data_title() click to toggle source

The human-readable title of the dataset.

@see Dataset#data_title

# File lib/data_kitten/publishing_formats/datapackage.rb, line 91
def data_title
  package.title || package.name
end
description() click to toggle source

A brief description of the dataset

@see Dataset#description

# File lib/data_kitten/publishing_formats/datapackage.rb, line 98
def description
  package.description
end
distributions() click to toggle source

A list of distributions, referred to as resources by Datapackage.

@see Dataset#distributions

# File lib/data_kitten/publishing_formats/datapackage.rb, line 84
def distributions
  package.resources.map { |resource| Distribution.new(self, datapackage_resource: resource) }
end
keywords() click to toggle source

Keywords for the dataset

@see Dataset#keywords

# File lib/data_kitten/publishing_formats/datapackage.rb, line 105
def keywords
  package.keywords
end
licenses() click to toggle source

A list of licenses.

@see Dataset#licenses

# File lib/data_kitten/publishing_formats/datapackage.rb, line 58
def licenses
  package.licenses.map do |x| 
    License.new(:id => x['id'], :uri => x['url'], :name => x['name'])
  end
end
maintainers() click to toggle source

A list of maintainers.

@see Dataset#maintainers

# File lib/data_kitten/publishing_formats/datapackage.rb, line 40
def maintainers
  package.maintainers.map do |x|
    Agent.new(:name => x['name'], :uri => x['web'], :email => x['email'])
  end
end
modified() click to toggle source

Date the dataset was modified

# File lib/data_kitten/publishing_formats/datapackage.rb, line 119
def modified
  package.last_modified
end
publishers() click to toggle source

A list of publishers.

@see Dataset#publishers

# File lib/data_kitten/publishing_formats/datapackage.rb, line 49
def publishers
  package.publisher.map do |x|
    Agent.new(:name => x['name'], :uri => x['web'], :email => x['email'])
  end
end
publishing_format() click to toggle source

The publishing format for the dataset. @return [Symbol] :datapackage @see Dataset#publishing_format

# File lib/data_kitten/publishing_formats/datapackage.rb, line 33
def publishing_format
  :datapackage
end
rights() click to toggle source
# File lib/data_kitten/publishing_formats/datapackage.rb, line 64
def rights
   if package.property("rights")
      Rights.new( ( package.property("rights", [])).each_with_object({}){|(k,v), h| h[k.to_sym] = v} )
   else
      nil
   end 
end
sources() click to toggle source

Where the data is sourced from

@see Dataset#sources

# File lib/data_kitten/publishing_formats/datapackage.rb, line 112
def sources
  package.sources.map do |x| 
    Source.new(:label => x['name'], :resource => x['web'])
  end
end

Private Instance Methods

package() click to toggle source
# File lib/data_kitten/publishing_formats/datapackage.rb, line 154
def package
  if !@datapackage
    if origin == :git
      metadata = load_file("datapackage.json")
      @datapackage = DataPackage::Package.new( JSON.parse( metadata ) )
    else
      @datapackage = DataPackage::Package.new( url )
    end        
  end
  @datapackage         
end