Sha256: 67974bf0ce62564640c6dcbf70e389755b73ba39be319f4e221a9c9232fbb52c

Contents?: true

Size: 1.34 KB

Versions: 11

Compression:

Stored size: 1.34 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

11 entries across 11 versions & 1 rubygems

Version Path
pagarme-1.9.2 lib/pagarme/plan.rb
pagarme-1.9.1 lib/pagarme/plan.rb
pagarme-1.9 lib/pagarme/plan.rb
pagarme-1.8 lib/pagarme/plan.rb
pagarme-1.7 lib/pagarme/plan.rb
pagarme-1.5 lib/pagarme/plan.rb
pagarme-1.4 lib/pagarme/plan.rb
pagarme-1.3 lib/pagarme/plan.rb
pagarme-1.2 lib/pagarme/plan.rb
pagarme-1.1 lib/pagarme/plan.rb
pagarme-1.0 lib/pagarme/plan.rb