class App42::Util

Public Instance Methods

compute_hmac(secret_key, sorted_params) click to toggle source

Encoding and decoding of the queries are done using the Hmac Algorithm.

@param baseString @param key

@throws NoSuchAlgorithmException @throws InvalidKeyException @throws IllegalStateException @throws UnsupportedEncodingException

# 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
extractFileExtension(fileName) click to toggle source

Taking extension out

@param fileName

@return extension

# File lib/util/util.rb, line 221
def extractFileExtension(fileName)
  ext = File.extname(fileName)
  return ext;
end
get_timestamp_utc() click to toggle source

Finds the current time

@return time format

# File lib/util/util.rb, line 24
def get_timestamp_utc
  #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
get_timestamp_utc_from_date(date) click to toggle source

Finds the particular time for a particular date

@param date

@return time format

# File lib/util/util.rb, line 42
def get_timestamp_utc_from_date(date)
  time = Time.parse(date).utc
  time_stamp = time.strftime("%Y-%m-%dT%H:%M:%S.%LZ")
  puts "TimeStamp - #{time_stamp}"
  time_stamp
end
get_timestamp_utc_from_date_discount(date) click to toggle source
# File lib/util/util.rb, line 49
def get_timestamp_utc_from_date_discount(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
my_strip(str) click to toggle source
# File lib/util/util.rb, line 114
def my_strip(str)
  s =  str.gsub(/ /,'')
  return s
end
sign(secret_key, params) click to toggle source

It signs the request that has to be sent in the Hmac format.

@param secretKey @param params

@throws InvalidKeyException @throws NoSuchAlgorithmException @throws IllegalStateException @throws UnsupportedEncodingException

# 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
sort_convert_table(table) click to toggle source

This method sorts all the values that are stored in the table.

@param table @return sorted string

# 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
throwExceptionIfEmailNotValid(obj, name) click to toggle source

An exception to check whether the email entered is valid or not.

@param obj @param name

# File lib/util/util.rb, line 172
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
throwExceptionIfNotValidExtension(fileName, name) click to toggle source

An exception to check if the file has valid extension or not

@param fileName @param name

# File lib/util/util.rb, line 204
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
throwExceptionIfNotValidImageExtension(fileName, name) click to toggle source

To check if the image has a valid extension or not.

@param fileName @param name

# File lib/util/util.rb, line 233
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
throwExceptionIfNullOrBlank(obj,name) click to toggle source

An exception to check whether the object is null or blank.

@param obj @param name

# File lib/util/util.rb, line 126
def throwExceptionIfNullOrBlank(obj,name)
  if obj.kind_of?(String)
    trm = my_strip(obj)
  else
    trm = obj
  end

  if(trm==nil)
    raise App42Exception.new(name +" parameter can not be null");
  end
  if(trm == "")
    raise App42Exception.new(name +" parameter can not be blank");
  end
end
validateHowMany(howMany) click to toggle source

To check whether the value of how many is less than 1000 or not.

@param howMany

# File lib/util/util.rb, line 159
def validateHowMany(howMany)
  if(howMany.to_i > 1000)
    raise App42Exception.new("How Many should be less than 1000");
  end
end
validateMax(max) click to toggle source

To check whether the max value is greater than zero or not.

@param max

# File lib/util/util.rb, line 147
def validateMax(max)
  if(max.to_i < 1)
    raise App42Exception.new("Max must be greater than Zero.");
  end
end