module DataKitten::PublishingFormats::LinkedData

Constants

ACCEPT_HEADER

Private Class Methods

create_graph(uri) click to toggle source

Attempt to create an RDF graph for this object

Supports content negotiation for various RDF serializations. Attempts “dataset autodiscovery” if it receives an HTML response. This leaves the RDFa Publishing Format to just parse RDFa responses

# File lib/data_kitten/publishing_formats/linked_data.rb, line 28
def self.create_graph(uri)

  resp = RestClient.get uri, 
      :accept=>ACCEPT_HEADER            
  return false if resp.code != 200

  if resp.headers[:content_type] =~ /text\/html/
      doc = Nokogiri::HTML( resp.body )
      link = doc.search('link[rel=alternate]').detect { |n| n[:type] == 'application/rdf+xml' }
      if link                
          resp = RestClient.get link["href"], 
              :accept=>ACCEPT_HEADER            
          return false if resp.code != 200
      else
          return false
      end
  end
  
  reader = RDF::Reader.for( :content_type => resp.headers[:content_type] )

  if !reader
      extension = File.extname( uri ).gsub(".", "")          
      reader = RDF::Reader.for( :file_extension => extension ) if extension != ""
  end
  return false unless reader

  graph = RDF::Graph.new()
  graph << reader.new( StringIO.new( resp.body ))
         
  return graph     
rescue
  nil
end
first_of_type(graph, classes) click to toggle source

Find first resource with one of the specified RDF types

# File lib/data_kitten/publishing_formats/linked_data.rb, line 14
def self.first_of_type(graph, classes)
    term = nil
    classes.each do |clazz|
        term = graph.first_subject(
            RDF::Query::Pattern.new( nil, RDF.type, clazz ) )
         break if term
    end
    term
end
supported?(instance) click to toggle source

Can we create an RDF graph for this object containing the description of a dataset?

# File lib/data_kitten/publishing_formats/linked_data.rb, line 63
def self.supported?(instance)
    graph = create_graph(instance.url)
    return false unless graph
    return true if first_of_type(graph, 
        [RDF::Vocabulary.new("http://www.w3.org/ns/dcat#").Dataset, 
         RDF::Vocabulary.new("http://rdfs.org/ns/void#").Dataset])
    return false          
end

Public Instance Methods

publishing_format() click to toggle source

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

# File lib/data_kitten/publishing_formats/linked_data.rb, line 77
def publishing_format
  :rdf
end

Private Instance Methods

dataset_uri() click to toggle source
# File lib/data_kitten/publishing_formats/linked_data.rb, line 83
def dataset_uri
  url
end
graph() click to toggle source
# File lib/data_kitten/publishing_formats/linked_data.rb, line 87
def graph
  @graph ||= LinkedData.create_graph(dataset_uri)
end