Sha256: 8f1d5cb14e4db74f2cfec1b10216785a1a72374854746687d8b8940e6719b86e

Contents?: true

Size: 927 Bytes

Versions: 5

Compression:

Stored size: 927 Bytes

Contents

module Workarea
  module Klarna
    class Gateway
      class Response
        attr_reader :request, :raw_response

        def initialize(request, raw_response)
          @request = request
          @raw_response = raw_response
        end

        def body
          @body ||=
            if raw_response&.body.present?
              JSON.parse(raw_response.body)
            else
              {}
            end
        end

        alias_method :params, :body

        def success?
          raw_response.present? && raw_response.status.in?([200, 201, 202, 204])
        end

        def message
          if success?
            I18n.t('workarea.klarna.gateway.response.success', summary: request.summary)
          else
            errors = body.fetch('error_messages', []).join('. ')
            I18n.t('workarea.klarna.gateway.response.failure', summary: errors)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
workarea-klarna-1.1.0 lib/workarea/klarna/gateway/response.rb
workarea-klarna-1.1.0.beta1 lib/workarea/klarna/gateway/response.rb
workarea-klarna-1.0.0 lib/workarea/klarna/gateway/response.rb
workarea-klarna-1.0.0.beta2 lib/workarea/klarna/gateway/response.rb
workarea-klarna-1.0.0.beta1 lib/workarea/klarna/gateway/response.rb