Sha256: 5bf60c176dff09ad27385b0fcb4a477fa9a0814954000e5fc32c8fc4f0274223

Contents?: true

Size: 993 Bytes

Versions: 1

Compression:

Stored size: 993 Bytes

Contents

require 'creditcard'

class PgCreditcard
  
  def initialize(options)
    raise ArgumentError if options.nil?
    @name   = options[:name]
    @number = options[:number]
    @month  = options[:month]
    @year   = set_year(options[:year])
    @cvv    = options[:cvv]
  end
  
  def type
    return nil if @number.nil?
    @type ||= @number.to_s.creditcard_type
  end
  
  def valid?
    @number.to_s.creditcard?
  end
  
  def expired?
    today = Time.now
    expiry = Time.new(@year, @month, days_in_a_month(@year, @month))
    today > expiry
  end
  
  private
  
  def set_year(x)
    if x.length == 2
      year = "20".concat(x)
    else
      year = x
    end
    year
  end
  
  def days_in_a_month(year, month)
    if month == 12
      year += 1
      next_month = 1
    else
      next_month = month.to_i + 1
    end
    next_month_first_day = Time.new(year, next_month)
    last_day_current_month = (next_month_first_day-1).day
    days = last_day_current_month
    days
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pg_creditcard-0.1.0 lib/pg_creditcard.rb