Class: Braspag::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/baby-braspag/utils.rb

Class Method Summary collapse

Class Method Details

.convert_decimal_to_string(value) ⇒ Object


3
4
5
# File 'lib/baby-braspag/utils.rb', line 3

def self.convert_decimal_to_string(value)
  ("%.2f" % value.to_f).gsub('.', ',')
end

.convert_to_map(document, map = {}) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/baby-braspag/utils.rb', line 7

def self.convert_to_map(document, map = {})
  document = Nokogiri::XML(document)

  map.each do |key, value|
    if value.is_a?(String) || value.nil?
      value = key if value.nil?

      new_value = document.search(value).first

      if new_value.nil?
        map[key] = nil
      else
        new_value = new_value.content.to_s
        map[key] = new_value unless new_value == ""
        map[key] = nil if new_value == ""
      end

    elsif value.is_a?(Proc)
      map[key] = value.call(document)
    end
  end

  map
end