Sha256: fe09d027625f1706d7c809d14ce898372ed91086344f066a022629f2e084d044

Contents?: true

Size: 1.17 KB

Versions: 9

Compression:

Stored size: 1.17 KB

Contents

require 'ox'

class Gillbus
  class BaseResponse
    extend Fields
    include UpdateAttrs

    attr_accessor :data
    attr_accessor :session_id

    attr_accessor :error_code
    attr_accessor :error_message
    attr_accessor :external_error_message
    attr_accessor :request_time

    def error?
      !error_code.nil?
    end

    def self.parse(data, instance: new, options: {})
      # ugly
      if data['MESSAGE']
        instance.error_code = data['MESSAGE']['CODE'].to_i
        instance.error_message = data['MESSAGE']['TEXT']
        instance.external_error_message = data['MESSAGE']['EXT_TEXT']
      else
        super(data, instance: instance, options: options)
      end
      # for debugging?
      instance.data = data
      instance
    end

    def self.parse_string(xml_string, **options)
      xml = Ox.load(xml_string, mode: :hash, symbolize_keys: false)
      # <DATA/> is a valid response
      return ParseError.new(xml_string, 'DATA attribute missing') unless xml.key?('DATA')
      data = xml['DATA'] || {}
      parse(data, instance: new, options: options)
    rescue Ox::ParseError, ArgumentError => e
      ParseError.new(xml_string, e.message)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
gillbus-0.18.9 lib/gillbus/base_response.rb
gillbus-0.18.8 lib/gillbus/base_response.rb
gillbus-0.18.7 lib/gillbus/base_response.rb
gillbus-0.18.6 lib/gillbus/base_response.rb
gillbus-0.18.5 lib/gillbus/base_response.rb
gillbus-0.18.4 lib/gillbus/base_response.rb
gillbus-0.18.3 lib/gillbus/base_response.rb
gillbus-0.18.2 lib/gillbus/base_response.rb
gillbus-0.18.1 lib/gillbus/base_response.rb