Class: RDFObject::Parser
- Inherits:
-
Object
- Object
- RDFObject::Parser
- Defined in:
- lib/rdf_objects/parsers.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
Returns the value of attribute base_uri.
-
#collection ⇒ Object
Returns the value of attribute collection.
Class Method Summary collapse
- .init_parser(rdf, options = {}) ⇒ Object
-
.parse(rdf, options = {}) ⇒ Object
Choose the best format parser from an admittedly small group of choices.
Instance Method Summary collapse
-
#initialize(data = nil) ⇒ Parser
constructor
A new instance of Parser.
- #sanitize_uri(uri) ⇒ Object
Constructor Details
#initialize(data = nil) ⇒ Parser
Returns a new instance of Parser.
133 134 135 136 |
# File 'lib/rdf_objects/parsers.rb', line 133 def initialize(data=nil) @collection = Collection.new self.data=(data) if data end |
Instance Attribute Details
#base_uri ⇒ Object
Returns the value of attribute base_uri.
63 64 65 |
# File 'lib/rdf_objects/parsers.rb', line 63 def base_uri @base_uri end |
#collection ⇒ Object
Returns the value of attribute collection.
132 133 134 |
# File 'lib/rdf_objects/parsers.rb', line 132 def collection @collection end |
Class Method Details
.init_parser(rdf, options = {}) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/rdf_objects/parsers.rb', line 70 def self.init_parser(rdf, ={}) if [:format] parser = case [:format] when 'rdfxml' then XMLParser.new(rdf) when 'rdfa' then RDFAParser.new(rdf) when 'ntriples' then NTriplesParser.new(rdf) when 'json' then JSONParser.new(rdf) end else # Check to see if it is a URI being passed if rdf.is_a?(String) begin uri = Addressable::URI.parse(rdf) if uri.ip_based? response = HTTPClient.fetch(rdf) rdf = response[:content] [:base_uri] = response[:uri] end rescue URI::InvalidURIError end end # Check if the format is XML or RDFa doc = XMLTestDocument.new p = Nokogiri::XML::SAX::Parser.new(doc) if rdf.respond_to?(:read) p.parse(rdf.read) else p.parse(rdf) end if doc.is_doc? if rdf.respond_to?(:read) rdf.rewind end if doc.is_html? parser = RDFAParser.new(rdf) elsif doc.is_rdf? parser = XMLParser.new(rdf) end else begin if rdf.respond_to?(:read) rdf.rewind json = JSON.parse(rdf.read) else json = JSON.parse(rdf) end parser = JSONParser.new(json) rescue JSON::ParserError if rdf.respond_to?(:read) rdf.rewind end parser = NTriplesParser.new(rdf) end end end if [:base_uri] && parser parser.base_uri = [:base_uri] end parser end |
.parse(rdf, options = {}) ⇒ Object
Choose the best format parser from an admittedly small group of choices.
65 66 67 68 |
# File 'lib/rdf_objects/parsers.rb', line 65 def self.parse(rdf, ={}) parser = init_parser(rdf, ) parser.parse if parser end |
Instance Method Details
#sanitize_uri(uri) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/rdf_objects/parsers.rb', line 150 def sanitize_uri(uri) # Fix some weirdness surrounding escaped ampersands in URIs. uri.gsub!(/&/,"&") # Return if there's nothing to sanitize with return uri unless self.base_uri begin u = Addressable::URI.parse(uri).normalize return uri if u.host fq_uri = self.base_uri+u fq_uri.to_s rescue URI::InvalidURIError uri end end |