Sha256: 04c1456f4d99206bfde6575b849cf813a433ab822022cd1439d53f54fc1694dd

Contents?: true

Size: 1.39 KB

Versions: 9

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require_relative 'initializable'

class Wegift::Response
  include Initializable

  STATUS = { success: 'SUCCESS', error: 'ERROR' }.freeze

  # Field Name Contents
  #
  # status        - The status of the transaction. Usually “SUCCESS” or “ERROR”
  # error_code    - The error code, i.e. SE002
  # error_string  - Description of the error code, i.e. Required field missing from request
  # error_details - Details of the particular error, i.e. Field "delivery" missing from request

  # global shared body
  attr_accessor :payload, :status, :error_code, :error_string, :error_details

  def is_successful?
    @status&.eql?(STATUS[:success])
  end

  def parse(response = {})
    # TODO: JSON responses, when requested?
    # let's fix that with a simpel catch all
    if response.success? && response['content-type'].eql?('application/json')
      @payload = JSON.parse(response.body)
      # TODO: @payload['status'] is only returned for orders! (products etc are plain objects)
      @status = @payload['status'] || STATUS[:success]
      @error_code = @payload['error_code']
      @error_string = @payload['error_string']
      @error_details = @payload['error_details']
    else
      @payload = {}
      @status = STATUS[:error]
      @error_code = response.status
      @error_string = response.reason_phrase
      @error_details = response.reason_phrase
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
wegift-ruby-client-1.10.2 lib/wegift/models/response.rb
wegift-ruby-client-1.10.0 lib/wegift/models/response.rb
wegift-ruby-client-1.9.1 lib/wegift/models/response.rb
wegift-ruby-client-1.9.0 lib/wegift/models/response.rb
wegift-ruby-client-1.8.4 lib/wegift/models/response.rb
wegift-ruby-client-1.8.3 lib/wegift/models/response.rb
wegift-ruby-client-1.8.2 lib/wegift/models/response.rb
wegift-ruby-client-1.8.1 lib/wegift/models/response.rb
wegift-ruby-client-1.8.0 lib/wegift/models/response.rb