Sha256: 66ad7e5175dd62343f3d57719fbd5cd323d571872c4c209dfea1ae1f797e9be0

Contents?: true

Size: 897 Bytes

Versions: 8

Compression:

Stored size: 897 Bytes

Contents

require 'nokogiri'

module Correios
  module CEP
    class Parser
      ADDRESS_MAP = {
        "end"          => :address,
        "bairro"       => :neighborhood,
        "cidade"       => :city,
        "uf"           => :state,
        "cep"          => :zipcode,
        "complemento"  => :complement,
        "complemento2" => :complement2,
      }.freeze

      def address(xml)
        result = Nokogiri::XML(xml).xpath("//return")
        return if result.nil? || result.empty?

        address = {}
        result.children.each do |element|
          address[ADDRESS_MAP[element.name]] = element.text if ADDRESS_MAP[element.name]
        end

        join_complements! address
        address
      end

      private

      def join_complements!(address)
        address[:complement] += " #{address.delete(:complement2)}"
        address[:complement].strip!
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
correios-cep-0.3.1 lib/correios/cep/parser.rb
correios-cep-0.3.0 lib/correios/cep/parser.rb
correios-cep-0.2.0 lib/correios/cep/parser.rb
correios-cep-0.1.4 lib/correios/cep/parser.rb
correios-cep-0.1.3 lib/correios/cep/parser.rb
correios-cep-0.1.2 lib/correios/cep/parser.rb
correios-cep-0.1.1 lib/correios/cep/parser.rb
correios-cep-0.1.0 lib/correios/cep/parser.rb