class Aspose::Cloud::Common::Utils

Public Class Methods

get_field_count(url,field_name) click to toggle source

Gets the count of a particular field in the response

  • :localfile holds the local file path along with name

  • :url holds the required url to use while uploading the file to Aspose Storage

# File lib/Common/utils.rb, line 81
def self.get_field_count(url,field_name)            
  response = RestClient.get(url, :accept => 'application/xml')
  doc = REXML::Document.new(response.body)
  pages = []            
  doc.elements.each(field_name) do |ele|
    pages << ele.text
  end
  return pages.size
end
get_filename(file) click to toggle source
# File lib/Common/utils.rb, line 97
def self.get_filename(file)
     
  filename = File.basename(file, File.extname(file) );
     
  return filename
     
end
parse_date(date_string) click to toggle source

Parses JSON date value to a valid date format

  • :datestring holds the JSON Date value

# File lib/Common/utils.rb, line 66
def self.parse_date(date_string)
  seconds_since_epoch = date_string.scan(/[0-9]+/)[0].to_i
  return Time.at((seconds_since_epoch-(21600000 + 18000000))/1000)
end
save_file(response_stream,local_file) click to toggle source

Saves the response stream to a local file.

# File lib/Common/utils.rb, line 91
def self.save_file(response_stream,local_file)
  open(local_file, 'wb') do |file|
    file.write(response_stream.body)
  end
end
sign(url) click to toggle source

Signs a URI with your appSID and Key.

  • :url describes the URL to sign

# File lib/Common/utils.rb, line 9
def self.sign(url)
  url = URI.escape(url)
  parsed_url = URI.parse(url)

  url_to_sign =''
  if parsed_url.query.nil? 
    url_to_sign = parsed_url.scheme+'://' + parsed_url.host + parsed_url.path + '?appSID=' + $app_sid
  else
    url_to_sign = parsed_url.scheme+'://' + parsed_url.host + parsed_url.path + '?' + parsed_url.query + '&appSID=' + $app_sid
  end

  # create a signature using the private key and the URL
  raw_signature = OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), $app_key, url_to_sign)

  #Convert raw to encoded string
  signature = Base64.strict_encode64(raw_signature).tr('+/','-_')

  #remove invalid character
  signature = signature.gsub(/[=_-]/,'=' => '','_' => '%2f','-' => '%2b')

  #Define expression
  pat = Regexp.new('%[0-9a-f]{2}')

  #Replace the portion matched to the above pattern to upper case
  for i in 0..5
    signature = signature.sub(pat,pat.match(signature).to_s.upcase)
  end

  # prepend the server and append the signature.
  signed_url = url_to_sign + "&signature=#{signature}"
  return signed_url
end
upload_file_binary(localfile,url) click to toggle source

Uploads a binary file from the client system

  • :localfile holds the local file path along with name

  • :url holds the required url to use while uploading the file to Aspose Storage

# File lib/Common/utils.rb, line 74
def self.upload_file_binary(localfile,url)
  RestClient.put( url,File.new(localfile, 'rb'))
end
validate_output(result) click to toggle source
# File lib/Common/utils.rb, line 42
def self.validate_output(result)
 
  validate = ['Unknown file format.', 'Unable to read beyond the end of the stream', 
    'Index was out of range', 'Cannot read that as a ZipFile', 'Not a Microsoft PowerPoint 2007 presentation',
    'Index was outside the bounds of the array', 'An attempt was made to move the position before the beginning of the stream',
  ]
 
  invalid = false
 
  validate.each do |value|
    if result.index(value) != nil
      invalid = ture
    end
  end
 
  if invalid == true
    return result
  else
    return ''
  end         
end