class BillsPayment::BillsPaymentObject

Attributes

id[RW]
params[R]

Public Class Methods

new(params) click to toggle source
# File lib/bills-payment/bills_payment_object.rb, line 6
def initialize(params)
  if params.is_a?(Hash)
    @params = params
    assign_attributes(@params)
  elsif params.is_a?(String)
    @params = { id: params }
    assign_attributes(@params)
  else
    @params = {}
  end
end

Public Instance Methods

api_url() click to toggle source
# File lib/bills-payment/bills_payment_object.rb, line 53
def api_url
  "#{CGI.escape(class_name.downcase)}s"
end
attributes() click to toggle source
# File lib/bills-payment/bills_payment_object.rb, line 24
def attributes
  attrs = {}
  instance_variables.reject { |var| var == '@params' }.each do |var|
    str = var.to_s.gsub(/^@/, '')
    next unless respond_to?("#{str}=")
    instance_var = instance_variable_get(var)
    if Util.object_classes[str]
      if instance_var.is_a?(BillsPaymentObject)
        attrs[Util.to_camel_case_lower(str).to_sym] = parse_bills_payment_obj(instance_var)
      elsif instance_var.is_a?(Array)
        objs = []
        instance_var.each do |object|
          objs << parse_billspayment_obj(object)
        end
        attrs[Util.to_camel_case_lower(str).to_sym] = objs
      else
        attrs[Util.to_camel_case_lower(str).to_sym] = instance_var
      end
    else
      attrs[Util.to_camel_case_lower(str).to_sym] = instance_var
    end
  end
  attrs
end
class_name() click to toggle source
# File lib/bills-payment/bills_payment_object.rb, line 49
def class_name
  self.class.name.split('::').last
end
parse_response(response) click to toggle source
# File lib/bills-payment/bills_payment_object.rb, line 18
def parse_response(response)
  @params = response
  assign_attributes(response)
  self
end

Private Instance Methods

add_attrs(attrs) click to toggle source
# File lib/bills-payment/bills_payment_object.rb, line 93
def add_attrs(attrs)
  attrs.each do |var, value|
    self.class.class_eval { attr_accessor var }
    instance_variable_set "@#{var}", value
  end
end
assign_attributes(params) click to toggle source
# File lib/bills-payment/bills_payment_object.rb, line 68
def assign_attributes(params)
  params.each do |key, value|
    key_underscore = Util.to_underscore(key)

    if (klass = Util.object_classes[key.to_s])
      case value
      when Array
        objs = []
        value.each do |v|
          objs << klass.new(v)
        end
        value = objs
      when Hash
        value = klass.new(value)
      end
    end

    if respond_to?("#{key_underscore}=")
      send(:"#{key_underscore}=", value)
    else
      add_attrs(key_underscore.to_s => value)
    end
  end
end
parse_bills_payment_obj(obj) click to toggle source
# File lib/bills-payment/bills_payment_object.rb, line 59
def parse_bills_payment_obj(obj)
  return unless obj.is_a?(BillsPaymentObject)
  if obj.respond_to?('id') && obj.id && (obj.is_a?(Destination) || obj.is_a?(Recipient) || obj.is_a?(Task))
    obj.id
  else
    obj.attributes
  end
end