Sha256: 620fcfd30996bc7389ced4321d9f7e521abc9aab5fda798e816a684a46bba2f2

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

module Veritrans
  class Result
    attr_reader :data
    attr_reader :status
    attr_reader :response

    def initialize(response, url, options, time)
      begin
        if url =~ %r{/v2/.+/transcript$}
          @data = {}
        else
          @data = Veritrans._json_decode(response.body)

          # Failback for Hash#symbolize_keys
          @data.keys.each do |key|
            @data[(key.to_sym rescue key) || key] = @data.delete(key)
          end
        end

      rescue => e
        Veritrans.logger.info "Error parsing papi response #{e.message}"
        Veritrans.logger.info e.backtrace.join("\n")
        @data = {}
      end

      @time = time
      @status = response.status
      @response = response
      @url = url
      @options = options
    end

    def success?
      @data[:status_code] == '200' || @data[:status_code] == '201'
    end

    # for VT-Link
    def created?
      @data[:status_code] == '201'
    end

    # Docs http://docs.veritrans.co.id/sandbox/status_code.html
    def status_code
      @data[:status_code].to_i
    end

    def status_message
      @data[:status_message]
    end

    def redirect_url
      @data[:redirect_url]
    end

    def messages
      if @data[:message].present?
        @data[:message].chomp(']').sub(/^\[/, '').split(',').map(&:strip)
      else
        []
      end
    end

    def body
      response.body
    end

    def method_missing(method_name, *args)
      if args.size == 0 && @data && @data.has_key?(method_name)
        return @data[method_name]
      else
        super
      end
    end

    def inspect
      time_ms = (@time * 1000).round
      data = @data.inspect.gsub(/:([^\s]+)=>/, "\\1: ")
      "#<Veritrans::Result:#{object_id} ^^ http_status: #{@status} time: #{time_ms}ms ^^ data: #{data}>"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
veritrans-2.0.2 lib/veritrans/result.rb
veritrans-2.0.0 lib/veritrans/result.rb
veritrans-2.0.0beta lib/veritrans/result.rb