Module: Utils

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

Class Method Summary collapse

Class Method Details

.hasCardExpired(year, month) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/paystack/utils/utils.rb', line 61

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



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

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

.hasYearPassed(year) ⇒ Object



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

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

.isEmpty(value) ⇒ Object



49
50
51
# File 'lib/paystack/utils/utils.rb', line 49

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

.isLuthValidNumber(number) ⇒ Object



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

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/paystack/utils/utils.rb', line 13

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
# File 'lib/paystack/utils/utils.rb', line 6

def Utils.nullifyString(value)
	if(value.strip.eql? "" || value.nil?)
		return nil
	end
	return value;
end

.serverErrorHandler(e) ⇒ Object



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

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