Class: BibframeRuby::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/bibframe_ruby/parser.rb

Overview

Parses serialized RDF input (JSON-LD, Turtle, RDF/XML) into an RDF::Graph using the appropriate reader.

Constant Summary collapse

EXTENSION_MAP =
{
  ".jsonld" => :jsonld,
  ".ttl" => :turtle,
  ".rdf" => :rdfxml
}.freeze
READER_MAP =
{
  jsonld: JSON::LD::Reader,
  turtle: RDF::Turtle::Reader,
  rdfxml: RDF::RDFXML::Reader
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, format: :jsonld) ⇒ Parser

Returns a new instance of Parser.



24
25
26
27
# File 'lib/bibframe_ruby/parser.rb', line 24

def initialize(input, format: :jsonld)
  @input = input
  @format = format
end

Class Method Details

.format_for_extension(ext) ⇒ Object



39
40
41
42
43
# File 'lib/bibframe_ruby/parser.rb', line 39

def self.format_for_extension(ext)
  EXTENSION_MAP.fetch(ext) do
    raise BibframeRuby::Error, "Unknown file extension: #{ext}"
  end
end

Instance Method Details

#parse ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/bibframe_ruby/parser.rb', line 29

def parse
  reader_class = READER_MAP.fetch(@format) do
    raise BibframeRuby::Error, "Unsupported format: #{@format}"
  end

  graph = RDF::Graph.new
  reader_class.new(@input) { |reader| graph << reader }
  graph
end