Sha256: 540743b5d58e65eb65704f252424fec1e01f00f501f4b7fa3cfeea510fd1b426

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module Fawry
  class FawryResponse
    attr_reader :fawry_api_response

    def initialize(fawry_api_response)
      @fawry_api_response = fawry_api_response

      build_response
    end

    def success?
      status_code == 200
    end

    def failure?
      !success?
    end

    def fawry_api_response_body
      fawry_api_response
    end

    private

    # Adds keys from fawry API response as methods
    # on FawryResponse instance that return the value
    # of each key
    #
    # type => type
    # referenceNumber => reference_number
    # merchantRefNumber => merchant_ref_number
    # expirationTime => expiration_time
    # statusCode => status_code
    # statusDescription => status_description
    #
    # fawry_res = FawryResponse.new(response)
    # fawry_res.status_code => 200
    # fawry_res.reference_number => 1234567
    def build_response
      fawry_api_response.keys.each do |key|
        method_name = key.split(/(?=[A-Z])/).map(&:downcase).join('_') # statusCode => status_code
        instance_variable_set("@#{method_name}", fawry_api_response[key])
        method_body = proc { instance_variable_get("@#{method_name}") }

        self.class.public_send(:define_method, method_name, method_body)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fawry-0.2.0 lib/fawry/fawry_response.rb
fawry-0.1.0 lib/fawry/fawry_response.rb