Class: Aspose::Cloud::Common::Utils
- Inherits:
-
Object
- Object
- Aspose::Cloud::Common::Utils
- Defined in:
- lib/Common/utils.rb
Class Method Summary collapse
-
.append_storage(uri, remote_folder = '', storage_name = '', storage_type = 'Aspose') ⇒ Object
appends storage name to the uri.
-
.build_uri(path, qry_data = nil) ⇒ Object
build uri.
- .download_file(remote_filename, output_filename, remote_folder = '', storage_name = '', storage_type = 'Aspose') ⇒ Object
-
.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.
- .get_filename(file) ⇒ Object
-
.parse_date(date_string) ⇒ Object
Parses JSON date value to a valid date format * :datestring holds the JSON Date value.
- .process_command(url, method = 'GET', header_type = 'XML', src = '') ⇒ Object
-
.save_file(response_stream, local_file) ⇒ Object
Saves the response stream to a local file.
-
.sign(url) ⇒ Object
Signs a URI with your appSID and Key.
-
.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.
- .validate_output(result) ⇒ Object
Class Method Details
.append_storage(uri, remote_folder = '', storage_name = '', storage_type = 'Aspose') ⇒ Object
appends storage name to the uri
144 145 146 147 148 149 150 |
# File 'lib/Common/utils.rb', line 144 def self.append_storage(uri, remote_folder='', storage_name='', storage_type='Aspose') tmp_uri = "folder=#{remote_folder}&" unless remote_folder.empty? tmp_uri = "#{tmp_uri}storage=#{storage_name}" unless storage_name.empty? unless storage_type.eql? "Aspose" tmp_uri = uri.include?('?') ? "&#{tmp_uri}" : "?#{tmp_uri}" unless tmp_uri.nil? tmp_uri = tmp_uri[0..-2] if tmp_uri[-1].eql?('&') unless tmp_uri.nil? tmp_uri.nil? ? uri : uri + tmp_uri end |
.build_uri(path, qry_data = nil) ⇒ Object
build uri
153 154 155 156 157 158 |
# File 'lib/Common/utils.rb', line 153 def self.build_uri(path,qry_data=nil) qry_str = '' qry_data.each { |key,value| qry_str = "#{qry_str}#{key}=#{value}&" } uri = qry_str.empty? ? "#{path}" : "#{path}?#{qry_str}" uri[-1].eql?('&') ? uri[0..-2] : uri end |
.download_file(remote_filename, output_filename, remote_folder = '', storage_name = '', storage_type = 'Aspose') ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/Common/utils.rb', line 160 def self.download_file(remote_filename, output_filename, remote_folder='', storage_name='', storage_type='Aspose') folder = Aspose::Cloud::AsposeStorage::Folder.new remote_filename = "#{remote_folder}/#{remote_filename}" unless remote_folder.empty? output_stream = folder.get_file(remote_filename,storage_type,storage_name) dst_path = "#{Aspose::Cloud::Common::AsposeApp.output_location}#{output_filename}" Aspose::Cloud::Common::Utils.save_file(output_stream, dst_path) return { local_path: dst_path, remote_path: remote_filename } end |
.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
120 121 122 123 124 125 126 127 128 |
# File 'lib/Common/utils.rb', line 120 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 pages.size end |
.get_filename(file) ⇒ Object
139 140 141 |
# File 'lib/Common/utils.rb', line 139 def self.get_filename(file) File.basename(file, File.extname(file)) end |
.parse_date(date_string) ⇒ Object
Parses JSON date value to a valid date format
-
:datestring holds the JSON Date value
105 106 107 108 |
# File 'lib/Common/utils.rb', line 105 def self.parse_date(date_string) seconds_since_epoch = date_string.scan(/[0-9]+/)[0].to_i Time.at((seconds_since_epoch-(21600000 + 18000000))/1000) end |
.process_command(url, method = 'GET', header_type = 'XML', src = '') ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/Common/utils.rb', line 23 def self.process_command(url, method='GET', header_type='XML', src='') uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = false http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = case method when 'GET' then Net::HTTP::Get.new(url) when 'DELETE' then Net::HTTP::Delete.new(url) when 'POST' then Net::HTTP::Post.new(url) else nil end request.body = src unless src.empty? if header_type == 'XML' request.add_field('Content-Type', 'application/xml') request.add_field('x-aspose-client', 'RubySDK/v1.0') elsif header_type == 'JSON' request.add_field('Content-Type', 'application/json') request.add_field('x-aspose-client', 'RubySDK/v1.0') end http.request(request).body end |
.save_file(response_stream, local_file) ⇒ Object
Saves the response stream to a local file.
131 132 133 134 135 136 137 |
# File 'lib/Common/utils.rb', line 131 def self.save_file(response_stream, local_file) open(local_file, 'wb') do |file| file.write(response_stream.body) end return local_file end |
.sign(url) ⇒ Object
Signs a URI with your appSID and Key.
-
:url describes the URL to sign
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/Common/utils.rb', line 53 def self.sign(url) url = url[0..-2] if url[-1].eql? '/' url = URI.escape(url) parsed_url = URI.parse(url) url_to_sign = "#{parsed_url.scheme}://#{parsed_url.host}#{parsed_url.path}?appSID=#{Aspose::Cloud::Common::AsposeApp.app_sid}" url_to_sign += "&#{parsed_url.query}" if parsed_url.query # create a signature using the private key and the URL raw_signature = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), Aspose::Cloud::Common::AsposeApp.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 6.times do signature = signature.sub(pat, pat.match(signature).to_s.upcase) end # prepend the server and append the signature. url_to_sign + "&signature=#{signature}" 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
113 114 115 |
# File 'lib/Common/utils.rb', line 113 def self.upload_file_binary(localfile, url) RestClient.put(url, File.new(localfile, 'rb'), :accept => 'application/json') end |
.validate_output(result) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/Common/utils.rb', line 84 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', ] validate.each do |value| return result if result.index(value) end '' end |