class JustInform::Parser

Attributes

doc[RW]

Public Class Methods

new() click to toggle source
# File lib/just_inform/parser.rb, line 6
def initialize
  load
end

Public Instance Methods

inspect() click to toggle source
# File lib/just_inform/parser.rb, line 10
def inspect
  "#{self.class} - #{self.doc.children.first.name}"
end
reports(doc=@doc) click to toggle source
# File lib/just_inform/parser.rb, line 32
def reports(doc=@doc)
  reports = []
  
  doc.xpath('//InformationCollectionRequest').map {|r| reports << InformationCollectionRequest.new(r) }
  reports
end
top(limit=10, attrib_name=:burden_hours) click to toggle source

Show top 10 forms, PRA.top(sort_by_symbol, number_of_results) parser.top(:burden,2) => [<Nokogiri::XML::Element>, <Nokogiri::XML::Element>]

# File lib/just_inform/parser.rb, line 16
def top(limit=10, attrib_name=:burden_hours)
  @topx = []
  @topx_size = limit
  sort_method = attrib_name.to_sym
  
  reports.each_with_index do |report, index|
    if @topx.last
      add_report_to_topx(report.send(sort_method), index) if report.send(sort_method) > @topx.last[0]
    else
      @topx << [report.send(sort_method), reports[index]]
    end
  end
  
  @topx.map! {|x| [number_to_delimited(x[0]), x[1]]}
end

Private Instance Methods

add_report_to_topx(sort_value, index) click to toggle source

add report search value (e.g. value of burden hours, responses, or cost) and index to @topx array

# File lib/just_inform/parser.rb, line 47
def add_report_to_topx(sort_value, index)
  (0..@topx_size).map do |n|
    if sort_value > @topx[n][0]
      @topx.insert(n, [sort_value, reports[index]])
      break
    else
      next
    end
  end
  
  @topx = @topx[0..(@topx_size-1)]
end
load() click to toggle source

load XML data into Nokogiri and set @doc instance variable

# File lib/just_inform/parser.rb, line 61
def load
  data = Downloader.get_latest
  @doc = ::Nokogiri::XML(data)
end
number_to_delimited(number) click to toggle source

pretify number results

# File lib/just_inform/parser.rb, line 42
def number_to_delimited(number)
  number.to_s.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
end