Class: Diffbot::Frontpage::DmlParser::ItemParser

Inherits:
Object
  • Object
show all
Defined in:
lib/diffbot/frontpage/dml_parser.rb

Overview

Parser that takes the XML from a particular item from the XML returned from the frontpage API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item_node) ⇒ ItemParser

Initialize the parser with an Item node.

item_node - The root node of the item.



65
66
67
# File 'lib/diffbot/frontpage/dml_parser.rb', line 65

def initialize(item_node)
  @item = item_node
end

Instance Attribute Details

#itemObject (readonly)

The root element of each item.



60
61
62
# File 'lib/diffbot/frontpage/dml_parser.rb', line 60

def item
  @item
end

Class Method Details

.parse(node) ⇒ Object



55
56
57
# File 'lib/diffbot/frontpage/dml_parser.rb', line 55

def self.parse(node)
  node = Nokogiri(node)
end

Instance Method Details

#parseObject

Parses the item’s DML and generates a Hash that we can add to the DML parser’s parser’s “items” key together with the other items.

Returns a Hash.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/diffbot/frontpage/dml_parser.rb', line 73

def parse
  attrs = {}

  %w(title link pubDate description textSummary).each do |attr|
    node = item % attr
    attrs[attr] = node && node.text
  end

  %w(type img id xroot cluster).each do |attr|
    attrs[attr] = item[attr]
  end

  attrs["stats"] = %w(fresh sp sr).each_with_object({}) do |attr, hash|
    hash[attr] = item[attr].to_f
  end

  attrs
end