class SunatInvoice::ResponseParser

Constants

ALLOWED_PARSERS
IN_PROCESS
STATUS_CODES
VALID_PROCESS

Attributes

cdr[R]
document_number[R]
message[R]
status_code[R]
ticket[R]

Public Class Methods

new(body, parser_type) click to toggle source
# File lib/sunat_invoice/response_parser.rb, line 19
def initialize(body, parser_type)
  # body: SOAP body as a Hash. Typically Savon Response body.
  # parser_type: kind of parser to use.
  raise InvalidResponseParser unless ALLOWED_PARSERS.include?(parser_type)

  send("parse_#{parser_type}", body)
end

Private Instance Methods

decrypt_zip(encrypted_zip) click to toggle source
# File lib/sunat_invoice/response_parser.rb, line 59
def decrypt_zip(encrypted_zip)
  decoded = Base64.decode64(encrypted_zip)
  Zip::InputStream.open(StringIO.new(decoded)) do |io|
    while (entry = io.get_next_entry)
      parse_xml(io.read) if entry.name.include?('.xml')
    end
  end
end
parse_invoice(body) click to toggle source
# File lib/sunat_invoice/response_parser.rb, line 29
def parse_invoice(body)
  encrypted_zip = body[:send_bill_response][:application_response]
  decrypt_zip(encrypted_zip)
end
parse_status(body) click to toggle source
# File lib/sunat_invoice/response_parser.rb, line 46
def parse_status(body)
  status_hash = body[:get_status_response][:status]
  @status_code = status_hash[:status_code]
  if VALID_PROCESS.include?(status_code)
    encrypted_zip = status_hash[:content]
    decrypt_zip(encrypted_zip)
  elsif IN_PROCESS.include?(status_code)
    @message = 'Your ticket is still in process'
  else
    @message = status_hash[:content]
  end
end
parse_summary(body) click to toggle source
# File lib/sunat_invoice/response_parser.rb, line 42
def parse_summary(body)
  @ticket = body[:send_summary_response][:ticket]
end
parse_xml(cdr_xml) click to toggle source
# File lib/sunat_invoice/response_parser.rb, line 34
def parse_xml(cdr_xml)
  @cdr = Nokogiri::XML(cdr_xml)
  response_node = @cdr.at('//cac:DocumentResponse/cac:Response')
  @status_code = response_node.at('//cbc:ResponseCode').content
  @document_number = response_node.at('//cbc:ReferenceID').content
  @message = response_node.at('//cbc:Description').content
end