Class: Sendregning::InvoiceParser

Inherits:
Object
  • Object
show all
Defined in:
lib/sendregning/invoice_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, invoice) ⇒ InvoiceParser

Returns a new instance of InvoiceParser.



11
12
13
14
# File 'lib/sendregning/invoice_parser.rb', line 11

def initialize(response, invoice)
  @response = response
  @invoice = invoice
end

Class Method Details

.parse(response, invoice = Sendregning::Invoice.new) ⇒ Object



6
7
8
# File 'lib/sendregning/invoice_parser.rb', line 6

def parse(response, invoice = Sendregning::Invoice.new)
  new(response, invoice).parse
end

Instance Method Details

#parseObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sendregning/invoice_parser.rb', line 16

def parse
  attributes = @response["invoices"]["invoice"]

  # Flatten optional and shipment attributes
  %w[optional shipment].each do |section|
    if attributes.key?(section)
      attributes = attributes.merge(attributes[section])
      attributes.delete(section)
    end
  end

  lines = attributes["lines"]["line"]
  attributes.delete("lines")

  @invoice.update(stringify_hash(attributes))
  @invoice.lines = lines.map do |l|
    Sendregning::Line.new(stringify_hash(l))
  end
  @invoice
end