class XeroGateway::Report

Attributes

body[RW]
column_names[RW]
errors[R]
report_date[RW]
report_id[RW]
report_name[RW]
report_titles[RW]
report_type[RW]
rows[RW]
updated_at[RW]

Public Class Methods

from_xml(report_element) click to toggle source
# File lib/xero_gateway/report.rb, line 28
def from_xml(report_element)
  report = Report.new
  report_element.children.each do | element |
    case element.name
      when 'ReportID'         then report.report_id = element.text
      when 'ReportName'       then report.report_name = element.text
      when 'ReportType'       then report.report_type = element.text
      when 'ReportTitles'
        each_title(element) do |title|
          report.report_titles << title
        end
      when 'ReportDate'       then report.report_date = Date.parse(element.text)
      when 'UpdatedDateUTC'   then report.updated_at = parse_date_time_utc(element.text)
      when 'Rows'
        report.column_names ||= find_body_column_names(element)
        each_row_content(element) do |row|
          report.body << row
        end
    end
  end
  report
end
new(params={}) click to toggle source
# File lib/xero_gateway/report.rb, line 16
def initialize(params={})
  @errors         ||= []
  @report_titles  ||= []
  @body           ||= []

  params.each do |k,v|
    self.send("#{k}=", v)
  end
end

Private Class Methods

collect_attributes(xml_cell) click to toggle source

Collects “<Attribute>” elements into a hash

# File lib/xero_gateway/report.rb, line 87
def collect_attributes(xml_cell)
  Array.wrap(xml_cell.elements["Attributes/Attribute"]).inject({}) do |hash, xml_attribute|
    if (key   = xml_attribute.elements["Id"].try(:text)) &&
      (value = xml_attribute.elements["Value"].try(:text))

      hash[key] = value
    end
    hash
  end.symbolize_keys
end
each_row_content(xml_element) { |row| ... } click to toggle source
# File lib/xero_gateway/report.rb, line 53
def each_row_content(xml_element, &block)
  column_names    = find_body_column_names(xml_element).values
  report_sections = REXML::XPath.each(xml_element, "//RowType[text()='Section']/parent::Row")

  report_sections.each do |section_row|
    section_name = section_row.get_elements("Title").first.try(:text)
    section_row.elements.each("Rows/Row") do |xpath_cells|
      values = find_body_cell_values(xpath_cells)
      yield Row.new(column_names, values, section_name)
    end
  end
end
each_title(xml_element) { |title| ... } click to toggle source
# File lib/xero_gateway/report.rb, line 66
def each_title(xml_element, &block)
  xpath_titles = REXML::XPath.first(xml_element, "//ReportTitles")
  xpath_titles.elements.each("//ReportTitle") do |xpath_title|
    title = xpath_title.text.strip
    yield title if block_given?
  end
end
find_body_cell_values(xml_cells) click to toggle source
# File lib/xero_gateway/report.rb, line 74
def find_body_cell_values(xml_cells)
  values = []
  xml_cells.elements.each("Cells/Cell") do |xml_cell|
    if value = xml_cell.children.first # finds <Value>...</Value>
      values << Cell.new(value.text.try(:strip), collect_attributes(xml_cell))
      next
    end
    values << nil
  end
  values
end
find_body_column_names(body) click to toggle source

returns something like { column_1: “Amount”, column_2: “Description”, … }

# File lib/xero_gateway/report.rb, line 99
def find_body_column_names(body)
  header       = REXML::XPath.first(body, "//RowType[text()='Header']")
  names_map    = {}
  column_count = 0
  header.parent.elements.each("Cells/Cell") do |header_cell|
    column_count += 1
    column_key    = "column_#{column_count}".to_sym
    column_name   = nil
    name_value    = header_cell.children.first
    column_name   = name_value.text.strip unless name_value.blank? # finds <Value>...</Value>
    names_map[column_key] = column_name
  end
  names_map
end