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, base_uri, format = "rdfxml") ⇒ Object

Convert specified format into a ResourceHash

format

one of rdfxml, ntriples, turtle

data

String containing the data to be parsed

base_uri

base uri of the data



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pho/converter.rb', line 53

def Converter.parse(data, base_uri, format="rdfxml")
  model = Redland::Model.new()
  case format
    when "rdfxml" then mime="application/rdf+xml"
    when "json" then mime="application/json"
    else mime=""     
  end
  
  parser = Redland::Parser.new(format, mime)
  parser.parse_string_into_model(model, data, base_uri)
  serializer = Redland::Serializer.new( "json", "application/json" )
  json = serializer.model_to_string(Redland::Uri.new(base_uri), model)
  return Converter.parse_json( json )        
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



23
24
25
# File 'lib/pho/converter.rb', line 23

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

.parse_ntriples(ntriples, base_uri) ⇒ Object

Parse a string containing N-Triples into a resource hash

ntriples

a String containing N-Triples



37
38
39
# File 'lib/pho/converter.rb', line 37

def Converter.parse_ntriples(ntriples, base_uri)
  return Converter.parse(ntriples, base_uri, "ntriples")
end

.parse_rdfxml(rdfxml, base_uri) ⇒ Object

Parse a string containing RDF/XML into a resource hash

rdfxml: a String containing RDF/XML



30
31
32
# File 'lib/pho/converter.rb', line 30

def Converter.parse_rdfxml(rdfxml, base_uri)
  return Converter.parse(rdfxml, base_uri, "rdfxml")
end

.parse_turtle(turtle, base_uri) ⇒ Object

Parse a string containing Turtle into a resource hash

ntriples

a String containing Turtle



44
45
46
# File 'lib/pho/converter.rb', line 44

def Converter.parse_turtle(turtle, base_uri)
  return Converter.parse(turtle, base_uri, "turtle")
end

.serialize_json(hash) ⇒ Object

Serialize a resource hash as RDF-in-JSON

hash

the resource hash to serialize



71
72
73
# File 'lib/pho/converter.rb', line 71

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