Class: Aspose::Cloud::Common::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/Common/utils.rb

Class Method Summary collapse

Class Method Details

.get_field_count(url, field_name) ⇒ Object

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



81
82
83
84
85
86
87
88
89
# 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) ⇒ Object



97
98
99
100
101
102
103
# 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) ⇒ Object

Parses JSON date value to a valid date format

  • :datestring holds the JSON Date value



66
67
68
69
# 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) ⇒ Object

Saves the response stream to a local file.



91
92
93
94
95
# 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) ⇒ Object

Signs a URI with your appSID and Key.

  • :url describes the URL to sign



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object

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



74
75
76
# 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) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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