Sha256: 20afd8d080dbced4154306f6d0316f1ecf3da543b908640141bbdfd87a8d7a82

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

module Laundry
  module PaymentsGateway

    class MerchantNotSetError < StandardError; end

    class ResponseModel

      attr_accessor :record
      attr_accessor :merchant

      def self.from_response(response, merchant)
        model = self.new
        model.merchant = merchant
        model.initialize_with_response response
        model
      end

      def initialize_with_response(response)
        self.record = response.try(:to_hash) || {}
      end

      def to_hash
        record.try(:to_hash)
      end

      def blank?
        record == {} || record.nil? || !record
      end

      def require_merchant!
        unless merchant && merchant.class == Laundry::PaymentsGateway::Merchant
          raise MerchantNotSetError, "Tried to call a method that requires a merchant to be set on the model. Try calling merchant= first."
        end
      end

      def method_missing(id, *args)
        return record[id.to_sym] if record.is_a?(Hash) && record.has_key?(id.to_sym)
        super
      end

      ## Support cleaner serialization to ActiveRecord
      ## Apparently supported in Rails >= 3.1

      # We don't want to serialize the merchant because it has upsetting
      # amounts of passwords in it.
      def dumpable
        d = self.dup
        d.merchant = nil
        d
      end

      def self.dump(model)
        model ? YAML::dump(model.dumpable) : nil
      end

      def self.load(model_text)
        model_text ? YAML::load(model_text) : nil
      end

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
laundry-0.0.8 lib/laundry/payments_gateway/models/response_model.rb
laundry-0.0.7 lib/laundry/payments_gateway/models/response_model.rb
laundry-0.0.6 lib/laundry/payments_gateway/models/response_model.rb