class AlphaCard::Resource

Alpha Card resource base class.

Constants

ORIGIN_TRANSACTION_VARIABLES

Original AlphaCard transaction variables names

Public Instance Methods

attributes_for_request(attrs = filled_attributes) click to toggle source

Returns only filled attributes with the original Alpha Card Services transaction variables names.

@param attrs [Hash]

Attributes that must be converted to AlphaCard request params/
Default value is <code>filled_attributes</code>.

@example

order = AlphaCard::Order.new(id: '1', tax: nil, po_number: 'PO123')
order.attributes_for_request

#=> { orderid: '1', ponumber: 'PO123' }
# File lib/alpha_card/resource.rb, line 26
def attributes_for_request(attrs = filled_attributes)
  return attrs if self.class::ORIGIN_TRANSACTION_VARIABLES.empty?

  attrs.each_with_object({}) do |(attr, value), request_attrs|
    request_attrs[self.class::ORIGIN_TRANSACTION_VARIABLES.fetch(attr, attr)] = value
  end
end

Protected Instance Methods

filled_attributes() click to toggle source

Returns the Hash with only filled attributes of the AlphaCard::AlphaCardObject.

@return [Hash]

Filled attributes of the <code>AlphaCard::AlphaCardObject</code>.

@example

order = AlphaCard::Order.new(id: '1', tax: nil, po_number: 'PO123')
order.filled_attributes

#=> { id: '1', po_number: 'PO123' }
# File lib/alpha_card/resource.rb, line 48
def filled_attributes
  attributes.select { |_, value| !value.nil? }
end
validate_required_attributes!() click to toggle source

Validate required attributes to be filled. Raises an exception if one of the attribute is not specified.

@raise [AlphaCard::InvalidObjectError] error if required attributes not set

# File lib/alpha_card/resource.rb, line 58
def validate_required_attributes!
  unless required_attributes?
    blank_attribute = required_attributes.detect { |attr| self[attr].nil? || self[attr].empty? }

    raise ValidationError, "#{blank_attribute} can't be blank"
  end
end