Sha256: 96a5e2d5c840e343a7cd36911330701e47cca952992512c3303b46dd8c1cec39

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

# encoding: utf-8

module PagarMe
  class Plan < Model
    def initialize(response = {})
      super
      before_set_filter :amount, :format_amount
    end

    def create
      validate
      super
    end

    def save
      raise PagarMeError.new('O plano precisa estar criado para poder ser atualizado!', 'internal_error') unless self.id
      super
    end

    def format_amount(amount)
      if amount.kind_of?(String)
        value = amount.gsub(/\./, "")
        value = value.strip
        value = value.match(/\d+/)[0]
        amount = value
      end
      amount
    end

    private

    def validate
      error = PagarMeError.new
      if !self[:amount] || self.amount.to_i <= 0
        error.errors << PagarMeError.new('Valor invalido', 'amount')
      end
      if !self[:days] || self.days.to_i <= 0
        error.errors << PagarMeError.new('Numero de dias invalido', 'days')
      end
      if !self[:name] || self.name.length <= 0
        error.errors << PagarMeError.new('Nome invalido', 'name')
      end
      if self[:trial_days] && self.trial_days.to_i < 0
        error.errors << PagarMeError.new('Dias de teste invalido', 'trial_days')
      end

      if(error.errors.any?)
        error.message = error.errors.map {|e| e.message}
        error.message = error.message.join(',')
        raise error
      else
        nil
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pagarme-1.9.5 lib/pagarme/plan.rb
pagarme-1.9.4 lib/pagarme/plan.rb
pagarme-1.9.3 lib/pagarme/plan.rb