Class: Pho::ResourceHash::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/pho/converter.rb

Overview

Class for converting to and from resource hashes

Class Method Summary collapse

Class Method Details

.parse(data, format = :rdfxml) ⇒ Object

Convert specified format into a ResourceHash

format

one of :rdfxml, :ntriples, :turtle

data

String containing the data to be parsed



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pho/converter.rb', line 42

def Converter.parse(data, format=:rdfxml)
  graph = RDF::Graph.new()
  io = StringIO.new( data )
  
  RDF::Reader.for(format).new(io) do |reader|
    reader.each_statement do |statement|
      graph << statement
    end
  end
  
  json = StringIO.new()
  
  RDF::Writer.for(:json).new(json) do |writer|
    writer << graph
  end
  
  return Converter.parse_json( json.string )        
end

.parse_json(json) ⇒ Object

Parse JSON structured according to the RDF-in-JSON specification into a Ruby resource hash. Simply invokes the JSON parser.

json

valid RDF-in-JSON



13
14
15
# File 'lib/pho/converter.rb', line 13

def Converter.parse_json(json)
  return JSON.parse(json)
end

.parse_ntriples(ntriples) ⇒ Object

Parse a string containing N-Triples into a resource hash

ntriples

a String containing N-Triples



27
28
29
# File 'lib/pho/converter.rb', line 27

def Converter.parse_ntriples(ntriples)
  return Converter.parse(ntriples, :ntriples)
end

.parse_rdfxml(rdfxml) ⇒ Object

Parse a string containing RDF/XML into a resource hash

rdfxml: a String containing RDF/XML



20
21
22
# File 'lib/pho/converter.rb', line 20

def Converter.parse_rdfxml(rdfxml)
  return Converter.parse(rdfxml, :rdfxml)
end

.parse_turtle(turtle) ⇒ Object

Parse a string containing Turtle into a resource hash

ntriples

a String containing Turtle



34
35
36
# File 'lib/pho/converter.rb', line 34

def Converter.parse_turtle(turtle)
  return Converter.parse(turtle, :turtle)
end

.serialize_json(hash) ⇒ Object

Serialize a resource hash as RDF-in-JSON

hash

the resource hash to serialize



64
65
66
# File 'lib/pho/converter.rb', line 64

def Converter.serialize_json(hash)
    return JSON.dump(hash)  
end