Class: Diffbot::Frontpage::DmlParser
- Inherits:
-
Object
- Object
- Diffbot::Frontpage::DmlParser
- Defined in:
- lib/diffbot/frontpage/dml_parser.rb
Overview
Parser that takes the XML generated from Diffbot’s Frontpage API call and returns a hash suitable for Diffbot::Frontpage.
Defined Under Namespace
Classes: ItemParser
Instance Attribute Summary collapse
-
#dml ⇒ Object
readonly
The root element of the DML document.
Class Method Summary collapse
-
.parse(dml) ⇒ Object
Take the string of DML and convert it into a nice little hash we can pass to Diffbot::Frontpage.
Instance Method Summary collapse
- #extract_text(node, selector) ⇒ Object
-
#initialize(node) ⇒ DmlParser
constructor
Initialize the parser with a DML node.
-
#parse ⇒ Object
Parses the Diffbot Markup Language and generates a Hash that we can pass to Frontpage.new.
Constructor Details
#initialize(node) ⇒ DmlParser
Initialize the parser with a DML node.
dml - The root XML::Element
19 20 21 |
# File 'lib/diffbot/frontpage/dml_parser.rb', line 19 def initialize(node) @dml = node end |
Instance Attribute Details
#dml ⇒ Object (readonly)
The root element of the DML document.
24 25 26 |
# File 'lib/diffbot/frontpage/dml_parser.rb', line 24 def dml @dml end |
Class Method Details
.parse(dml) ⇒ Object
Take the string of DML and convert it into a nice little hash we can pass to Diffbot::Frontpage.
dml - A string of DML.
Returns a Hash.
10 11 12 13 14 |
# File 'lib/diffbot/frontpage/dml_parser.rb', line 10 def self.parse(dml) node = Nokogiri(dml).root parser = new(node) parser.parse end |
Instance Method Details
#extract_text(node, selector) ⇒ Object
47 48 49 50 |
# File 'lib/diffbot/frontpage/dml_parser.rb', line 47 def extract_text(node, selector) tag = node % selector tag && tag.text end |
#parse ⇒ Object
Parses the Diffbot Markup Language and generates a Hash that we can pass to Frontpage.new.
Returns a Hash.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/diffbot/frontpage/dml_parser.rb', line 30 def parse attrs = {} info = dml % "info" attrs["title"] = extract_text(info, "title") attrs["icon"] = extract_text(info, "icon") attrs["sourceType"] = extract_text(info, "sourceType") attrs["sourceURL"] = extract_text(info, "sourceURL") items = dml / "item" attrs["items"] = items.map do |item| ItemParser.new(item).parse end attrs end |