Sha256: 018d54d60897aee7c9a0eede279370f852ffc6f54b18fd78aacfc02a0f0ef6c3

Contents?: true

Size: 1.3 KB

Versions: 10

Compression:

Stored size: 1.3 KB

Contents

module SoapyCake
  class Response
    include Helper

    attr_accessor :time_offset
    attr_reader :body, :addedit

    def initialize(body, addedit)
      @body = body
      @addedit = addedit
    end

    def collection
      check_errors!

      return typed_element(sax.at_depth(3).first) if addedit

      sax.at_depth(5).map do |element|
        typed_element(element)
      end
    end

    private

    def typed_element(element)
      walk_tree(element) do |value, key|
        next value.to_i if key.to_s.end_with?('_id') && !key.to_s.end_with?('tax_id')

        if /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.?\d*\z/.match(value)
          next DateTime.parse(value + format('%+03d:00', time_offset.to_i))
        end

        next false if value == 'false'
        next true if value == 'true'

        value
      end
    end

    def sax
      @sax ||= Saxerator.parser(StringIO.new(body)) do |config|
        config.symbolize_keys!
        config.ignore_namespaces!
      end
    end

    def check_errors!
      fault = sax.for_tag(:fault).first
      fail RequestFailed, fault[:reason][:text] if fault
      fail RequestFailed, error_message unless sax.for_tag(:success).first == 'true'
    end

    def error_message
      sax.for_tag(:message).first || sax.for_tag(:Text).first || 'Unknown error'
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
soapy_cake-1.5.0 lib/soapy_cake/response.rb
soapy_cake-1.4.0 lib/soapy_cake/response.rb
soapy_cake-1.3.6 lib/soapy_cake/response.rb
soapy_cake-1.3.5 lib/soapy_cake/response.rb
soapy_cake-1.3.4 lib/soapy_cake/response.rb
soapy_cake-1.3.3 lib/soapy_cake/response.rb
soapy_cake-1.3.2 lib/soapy_cake/response.rb
soapy_cake-1.3.1 lib/soapy_cake/response.rb
soapy_cake-1.3.0 lib/soapy_cake/response.rb
soapy_cake-1.2.0 lib/soapy_cake/response.rb