Class: App42::Util
- Inherits:
-
Object
- Object
- App42::Util
- Defined in:
- lib/util/util.rb
Instance Method Summary collapse
-
#compute_hmac(secret_key, sorted_params) ⇒ Object
Encoding and decoding of the queries are done using the Hmac Algorithm.
-
#extractFileExtension(fileName) ⇒ Object
Taking extension out.
-
#get_timestamp_utc ⇒ Object
Finds the current time.
-
#get_timestamp_utc_from_date(date) ⇒ Object
Finds the particular time for a particular date.
- #get_timestamp_utc_from_date_discount(date) ⇒ Object
- #my_strip(str) ⇒ Object
-
#sign(secret_key, params) ⇒ Object
It signs the request that has to be sent in the Hmac format.
-
#sort_convert_table(table) ⇒ Object
This method sorts all the values that are stored in the table.
-
#throwExceptionIfEmailNotValid(obj, name) ⇒ Object
An exception to check whether the email entered is valid or not.
-
#throwExceptionIfNotValidExtension(fileName, name) ⇒ Object
An exception to check if the file has valid extension or not.
-
#throwExceptionIfNotValidImageExtension(fileName, name) ⇒ Object
To check if the image has a valid extension or not.
-
#throwExceptionIfNullOrBlank(obj, name) ⇒ Object
An exception to check whether the object is null or blank.
-
#validateHowMany(howMany) ⇒ Object
To check whether the value of how many is less than 1000 or not.
-
#validateMax(max) ⇒ Object
To check whether the max value is greater than zero or not.
Instance Method Details
permalink #compute_hmac(secret_key, sorted_params) ⇒ Object
Encoding and decoding of the queries are done using the Hmac Algorithm.
106 107 108 109 110 111 112 |
# File 'lib/util/util.rb', line 106 def compute_hmac(secret_key, sorted_params) signature = Base64.encode64(HMAC::SHA1::digest(secret_key, sorted_params)).strip puts "Signature #{signature}" computed_hmac = CGI.escape(signature) puts "Computed Hmac #{computed_hmac}" computed_hmac end |
permalink #extractFileExtension(fileName) ⇒ Object
Taking extension out
215 216 217 218 |
# File 'lib/util/util.rb', line 215 def extractFileExtension(fileName) ext = File.extname(fileName) return ext; end |
permalink #get_timestamp_utc ⇒ Object
Finds the current time
24 25 26 27 28 29 30 31 |
# File 'lib/util/util.rb', line 24 def #time_stamp = Time.now.utc.iso8601 #time_stamp = Time.now.utc.xmlschema time_stamp = Time.now.utc time_stamp = time_stamp.strftime("%Y-%m-%dT%H:%M:%S.%LZ") puts "TimeStamp - #{time_stamp}" time_stamp end |
permalink #get_timestamp_utc_from_date(date) ⇒ Object
Finds the particular time for a particular date
42 43 44 45 46 47 |
# File 'lib/util/util.rb', line 42 def (date) time = Time.parse(date).utc time_stamp = time.strftime("%Y-%m-%dT%H:%M:%S.%LZ") puts "TimeStamp - #{time_stamp}" time_stamp end |
permalink #get_timestamp_utc_from_date_discount(date) ⇒ Object
[View source]
49 50 51 52 53 54 |
# File 'lib/util/util.rb', line 49 def (date) time = Time.parse(date).utc time_stamp = time.strftime("%a %b %d %l:%M:%S %Z %Y") puts "TimeStamp - #{time_stamp}" time_stamp end |
permalink #my_strip(str) ⇒ Object
[View source]
114 115 116 117 |
# File 'lib/util/util.rb', line 114 def my_strip(str) s = str.gsub(/ /,'') return s end |
permalink #sign(secret_key, params) ⇒ Object
It signs the request that has to be sent in the Hmac format.
86 87 88 89 90 91 92 |
# File 'lib/util/util.rb', line 86 def sign(secret_key, params) sorted_params = sort_convert_table(params) puts sorted_params signature = compute_hmac(secret_key, sorted_params) puts "Signature #{signature}" signature end |
permalink #sort_convert_table(table) ⇒ Object
This method sorts all the values that are stored in the table.
64 65 66 67 68 69 70 71 72 |
# File 'lib/util/util.rb', line 64 def sort_convert_table(table) sorted_params = "" table.sort {|a,b| a[0] <=> b[0]}.each{ |key, val| sorted_params << key sorted_params << val } puts "Sorted params #{sorted_params}" sorted_params end |
permalink #throwExceptionIfEmailNotValid(obj, name) ⇒ Object
An exception to check whether the email entered is valid or not.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/util/util.rb', line 166 def throwExceptionIfEmailNotValid(obj, name) if(obj==nil) raise App42Exception.new(name +" parameter can not be null") end email_regex = %r{ ^ # Start of string [0-9a-z] # First character [0-9a-z.+]+ # Middle characters [0-9a-z] # Last character @ # Separating @ character [0-9a-z] # Domain name begin [0-9a-z.-]+ # Domain name middle [0-9a-z] # Domain name end $ # End of string }xi if (obj =~ email_regex) == 0 else raise App42Exception.new(name + " is not valid. ") end end |
permalink #throwExceptionIfNotValidExtension(fileName, name) ⇒ Object
An exception to check if the file has valid extension or not
198 199 200 201 202 203 204 205 |
# File 'lib/util/util.rb', line 198 def throwExceptionIfNotValidExtension(fileName, name) if(fileName==nil) raise App42Exception.new(name + " parameter can not be null") end if(fileName.index('.') == -1) raise App42Exception.new(name + " does not contain valid extension. ") end end |
permalink #throwExceptionIfNotValidImageExtension(fileName, name) ⇒ Object
To check if the image has a valid extension or not.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/util/util.rb', line 227 def throwExceptionIfNotValidImageExtension(fileName, name) if(fileName == nil) raise App42Exception.new(name + " parameter can not be null") end if(fileName.index('.') == -1) raise App42Exception.new(name + " does not contain valid extension. ") end ext = extractFileExtension(fileName); if((ext.eql?(".jpg") == false) && (ext.eql?(".JPG") == false) && (ext.eql?(".jpeg") == false) && (ext.eql?(".JPEG") == false) && (ext.eql?(".gif") == false) && (ext.eql?(".GIF") == false) && (ext.eql?(".png") == false) && (ext.eql?(".PNG") == false)) raise App42Exception.new( "The Request parameters are invalid. Only file with extensions jpg, jpeg, gif and png are supported"); end end |
permalink #throwExceptionIfNullOrBlank(obj, name) ⇒ Object
An exception to check whether the object is null or blank.
126 127 128 129 130 131 132 133 |
# File 'lib/util/util.rb', line 126 def throwExceptionIfNullOrBlank(obj,name) obj.kind_of?(String) ? trm = my_strip(obj) : trm = obj raise App42Exception.new(name +" parameter can not be null") if trm==nil raise App42Exception.new(name +" parameter can not be blank") if trm == "" end |
permalink #validateHowMany(howMany) ⇒ Object
To check whether the value of how many is less than 1000 or not.
153 154 155 156 157 |
# File 'lib/util/util.rb', line 153 def validateHowMany(howMany) if(howMany.to_i > 1000) raise App42Exception.new("How Many should be less than 1000"); end end |
permalink #validateMax(max) ⇒ Object
To check whether the max value is greater than zero or not.
141 142 143 144 145 |
# File 'lib/util/util.rb', line 141 def validateMax(max) if(max.to_i < 1) raise App42Exception.new("Max must be greater than Zero."); end end |