Module: Utils

Defined in:
lib/paystack/utils/utils.rb

Class Method Summary collapse

Class Method Details

.hasCardExpired(year, month) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/paystack/utils/utils.rb', line 64

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



59
60
61
62
# File 'lib/paystack/utils/utils.rb', line 59

def Utils.hasMonthPassed(year, month)
	t = Time.new
	return hasYearPassed(year) || year == t.year && month < (t.month)
end

.hasYearPassed(year) ⇒ Object



56
57
58
# File 'lib/paystack/utils/utils.rb', line 56

def Utils.hasYearPassed(year)
	return year < Time.new.year
end

.isEmpty(value) ⇒ Object



52
53
54
# File 'lib/paystack/utils/utils.rb', line 52

def Utils.isEmpty(value)
	return (value.nil? || value.strip.eql?(""))
end

.isLuhnValidNumber(number) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/paystack/utils/utils.rb', line 32

def Utils.isLuhnValidNumber(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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/paystack/utils/utils.rb', line 16

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



5
6
7
8
9
10
11
12
13
14
# File 'lib/paystack/utils/utils.rb', line 5

def Utils.nullifyString(value)
	if value.nil?
		return nil
	end

	if (value.strip.eql? "")
		return nil
	end
	return value;
end

.serverErrorHandler(e) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/paystack/utils/utils.rb', line 81

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