Sha256: a16afa8ccb4fc8d3e16f1026cc8452f9ddb867d188ae94b1bcf4f12414145a1a

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

module SoapyCake
  class Response
    attr_accessor :time_offset
    attr_reader :body

    def initialize(body)
      @body = body
    end

    def collection
      check_errors!

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

    private

    def typed_element(element)
      Helper.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
      unless sax.for_tag(:success).first == 'true'
        message = sax.for_tag(:message).first || sax.for_tag(:Text).first || 'Unknown error'
        fail RequestFailed, message
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
soapy_cake-1.1.2 lib/soapy_cake/response.rb
soapy_cake-1.1.1 lib/soapy_cake/response.rb
soapy_cake-1.1.0 lib/soapy_cake/response.rb
soapy_cake-1.0.1 lib/soapy_cake/response.rb