Module: Utils
- Defined in:
- lib/paystack/utils/utils.rb
Class Method Summary collapse
- .hasCardExpired(year, month) ⇒ Object
- .hasMonthPassed(year, month) ⇒ Object
- .hasYearPassed(year) ⇒ Object
- .isEmpty(value) ⇒ Object
- .isLuthValidNumber(number) ⇒ Object
- .isWholePositiveNumber(value) ⇒ Object
- .nullifyString(value) ⇒ Object
- .serverErrorHandler(e) ⇒ Object
Class Method Details
.hasCardExpired(year, month) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/paystack/utils/utils.rb', line 65 def Utils.hasCardExpired(year, month) # Normalize Year value e.g 14 becomes 2014 or 2114 etc. year_int = year.strip.to_i if(year_int < 100 && year_int >= 0) cal_year = Time.new.year.to_s year_int = ("#{cal_year[0..1]}#{year.strip}").to_i end # Check for expiration return !hasYearPassed(year_int) && !hasMonthPassed(year_int, month.to_i) end |
.hasMonthPassed(year, month) ⇒ Object
60 61 62 63 |
# File 'lib/paystack/utils/utils.rb', line 60 def Utils.hasMonthPassed(year, month) t = Time.new return hasYearPassed(year) || year == t.year && month < (t.month) end |
.hasYearPassed(year) ⇒ Object
57 58 59 |
# File 'lib/paystack/utils/utils.rb', line 57 def Utils.hasYearPassed(year) return year < Time.new.year end |
.isEmpty(value) ⇒ Object
53 54 55 |
# File 'lib/paystack/utils/utils.rb', line 53 def Utils.isEmpty(value) return (value.nil? || value.strip.eql?("")) end |
.isLuthValidNumber(number) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/paystack/utils/utils.rb', line 33 def Utils.isLuthValidNumber(number) sum = 0 length = number.strip.length; for i in 0..(length-1) c = number[length - 1 -i] if((c =~ /[[:digit:]]/) == nil) return false end digit = c.to_i if (i % 2 == 1) digit *= 2 end sum += digit > 9 ? digit - 9 : digit end return (sum % 10 == 0) end |
.isWholePositiveNumber(value) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/paystack/utils/utils.rb', line 17 def Utils.isWholePositiveNumber(value) if(value == nil) return false end length = value.length; for i in 0..(length-1) c = value[i] if((c =~ /[[:digit:]]/) == nil) return false end end return true end |
.nullifyString(value) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/paystack/utils/utils.rb', line 6 def Utils.nullifyString(value) if value.nil? return nil end if (value.strip.eql? "") return nil end return value; end |
.serverErrorHandler(e) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/paystack/utils/utils.rb', line 82 def Utils.serverErrorHandler(e) if(e.response == nil) raise e return end error = PaystackServerError.new(e.response); case e.response.code when 400 raise error, "HTTP Code 400: A validation or client side error occurred and the request was not fulfilled. " when 401 raise error, "HTTP Code 401: The request was not authorized. This can be triggered by passing an invalid secret key in the authorization header or the lack of one" when 404 raise error, "HTTP Code 404: Request could not be fulfilled as the request resource does not exist." when 500, 501,502,503,504 raise error, "HTTP Code #{e.response.code}: Request could not be fulfilled due to an error on Paystack's end. This shouldn't happen so please report as soon as you encounter any instance of this." else raise error, "HTTP Code #{e.response.code}: #{e.response.body}" end end |