class RDF::Serializers::HndJSONParser

Constants

HEX_DATATYPE
HEX_GRAPH
HEX_LANGUAGE
HEX_OBJECT
HEX_PREDICATE
HEX_SUBJECT

Private Class Methods

parse(body) click to toggle source
# File lib/rdf/serializers/hdnjson_parser.rb, line 64
def parse(body)
  new.parse_body(body)
end

Public Instance Methods

parse_body(body) click to toggle source
# File lib/rdf/serializers/hdnjson_parser.rb, line 15
def parse_body(body)
  body.split("\n").map { |line| parse_hex(JSON.parse(line)) }
end
parse_hex(hex) click to toggle source
# File lib/rdf/serializers/hdnjson_parser.rb, line 19
def parse_hex(hex)
  subject = parse_subject(hex[HEX_SUBJECT])
  predicate = RDF::URI(hex[HEX_PREDICATE])
  object = parse_object(hex[HEX_OBJECT], hex[HEX_DATATYPE], hex[HEX_LANGUAGE])
  graph = hex[HEX_GRAPH].present? ? RDF::URI(hex[HEX_GRAPH]) : RDF::Serializers.config.default_graph

  RDF::Statement.new(
    subject,
    predicate,
    object,
    graph_name: graph
  )
end

Private Instance Methods

blank_node(id) click to toggle source
# File lib/rdf/serializers/hdnjson_parser.rb, line 35
def blank_node(id)
  @blank_nodes ||= {}
  @blank_nodes[id] ||= RDF::Node(id)
end
parse_object(value, datatype, language) click to toggle source
# File lib/rdf/serializers/hdnjson_parser.rb, line 40
def parse_object(value, datatype, language)
  case datatype
  when 'http://www.w3.org/1999/02/22-rdf-syntax-ns#namedNode'
    RDF::URI(value)
  when 'http://www.w3.org/1999/02/22-rdf-syntax-ns#blankNode'
    blank_node(value.sub('_:', ''))
  when language
    RDF::Literal(value, datatype: RDF.langString, language: language)
  else
    xsd_to_rdf(datatype, value, language: language.presence)
  end
end
parse_subject(subject) click to toggle source
# File lib/rdf/serializers/hdnjson_parser.rb, line 53
def parse_subject(subject)
  if subject.is_a?(RDF::Resource)
    subject
  elsif subject.start_with?('_')
    blank_node(subject.sub('_:', ''))
  else
    RDF::URI(subject)
  end
end