Class: Twingly::Analytics::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/twingly-analytics/parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(document) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/twingly-analytics/parser.rb', line 6

def parse(document)
  result = Result.new
  nokogiri = Nokogiri::XML(document)

  failure = nokogiri.at_xpath('//name:blogstream/name:operationResult[@resultType="failure"]', :name => 'http://www.twingly.com')
  fail failure.text if failure

  result.number_of_matches_returned = nokogiri.at_xpath('/twinglydata/@numberOfMatchesReturned').value.to_i
  result.number_of_matches_total = nokogiri.at_xpath('/twinglydata/@numberOfMatchesTotal').value.to_i
  result.seconds_elapsed = nokogiri.at_xpath('/twinglydata/@secondsElapsed').value.to_f

  nokogiri.xpath('//post').each do |post|
    result.posts << parse_post(post)
  end

  result
end