Skip to content

Commit

Permalink
Make sure registry_uri is processed for each invocation, allowing it …
Browse files Browse the repository at this point in the history
…to be passed as a parameter.

Add --registry argument to script/parse to allow it to be specified.
  • Loading branch information
gkellogg committed Mar 2, 2012
1 parent 5bfe383 commit 9fdd3dc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
23 changes: 13 additions & 10 deletions lib/rdf/microdata/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,21 @@ class Registry
##
# Initialize the registry from a URI or file path
#
# @param [Hash] json
def self.load_registry(json)
# @param [String] registry_uri
def self.load_registry(registry_uri)
return if @registry_uri == registry_uri

json = RDF::Util::File.open_file(registry_uri) { |f| JSON.load(f) }

@prefixes = {}
json.each do |prefix, elements|
next unless elements.is_a?(Hash)
propertyURI = elements.fetch("propertyURI", "vocabulary").to_sym
multipleValues = elements.fetch("multipleValues", "unordered").to_sym
properties = elements.fetch("properties", {})
@prefixes[prefix] = Registry.new(prefix, propertyURI, multipleValues, properties)
end
@registry_uri = registry_uri
end

##
Expand Down Expand Up @@ -219,14 +225,11 @@ def initialize(input = $stdin, options = {}, &block)
add_debug(@doc, "library = #{@library}")

# Load registry
unless Registry.loaded?
registry = options[:registry_uri] || DEFAULT_REGISTRY
begin
json = RDF::Util::File.open_file(registry) { |f| JSON.load(f) }
rescue JSON::ParserError => e
raise RDF::ReaderError, "Failed to parse registry: #{e.message}"
end
Registry.load_registry(json)
begin
registry_uri = options[:registry_uri] || DEFAULT_REGISTRY
Registry.load_registry(registry_uri)
rescue JSON::ParserError => e
raise RDF::ReaderError, "Failed to parse registry: #{e.message}"
end

if block_given?
Expand Down
3 changes: 2 additions & 1 deletion script/parse
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ options = {
:output_format => :turtle,
:input_format => :microdata,
:standard_prefixes => true,
:rdf_terms => true,
:base_uri => "http://example.com",
}
input = nil
Expand All @@ -62,6 +61,7 @@ opts = GetoptLong.new(
["--input-format", GetoptLong::REQUIRED_ARGUMENT],
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT],
["--quiet", GetoptLong::NO_ARGUMENT],
["--registry", GetoptLong::REQUIRED_ARGUMENT],
["--template", GetoptLong::REQUIRED_ARGUMENT],
["--uri", GetoptLong::REQUIRED_ARGUMENT],
["--validate", GetoptLong::NO_ARGUMENT],
Expand All @@ -75,6 +75,7 @@ opts.each do |opt, arg|
when '--input-format' then options[:input_format] = arg.to_sym
when '--quiet' then options[:quiet] = true
when '--output' then options[:output] = File.open(arg, "w")
when '--registry' then options[:registry_uri] = arg
when '--template' then options[:haml] = arg.to_sym
when '--uri' then options[:base_uri] = arg
when '--verbose' then options[:verbose] = true
Expand Down

0 comments on commit 9fdd3dc

Please sign in to comment.