Class: IDRCloudClient
- Inherits:
-
Object
- Object
- IDRCloudClient
- Defined in:
- lib/idr_cloud_client.rb,
lib/idr_cloud_client/version.rb
Overview
Used to interact with IDRsolutions’ web services For detailed usage instructions, see GitHub
Constant Summary collapse
- DOWNLOAD =
'download'- UPLOAD =
'upload'- JPEDAL =
'jpedal'- BUILDVU =
'buildvu'- VERSION =
"1.0.1"
Instance Method Summary collapse
-
#convert(**params) ⇒ Object
Converts the given file and returns a hash collection with the conversion results.
-
#download_result(results, output_file_path, file_name = nil) ⇒ Object
Downloads the zip file produced by the microservice.
-
#initialize(url, conversion_timeout: 30, auth: nil) ⇒ IDRCloudClient
constructor
- Constructor, setup the converter details Params:
url -
string, the URL of the web service.
- Constructor, setup the converter details Params:
Constructor Details
#initialize(url, conversion_timeout: 30, auth: nil) ⇒ IDRCloudClient
Constructor, setup the converter details Params:
url-
string, the URL of the web service.
conversion_timeout-
int, (optional) the time to wait (in seconds) before timing out. Set to 30 by default.
auth-
array, (optional) the username and password to use for HTTP Authentication. Set to nil by default
45 46 47 48 49 50 |
# File 'lib/idr_cloud_client.rb', line 45 def initialize(url, conversion_timeout: 30, auth: nil) @base_endpoint = url @endpoint = @base_endpoint @convert_timeout = conversion_timeout @auth = auth end |
Instance Method Details
#convert(**params) ⇒ Object
Converts the given file and returns a hash collection with the conversion results. Requires the ‘input’ and either ‘url’ or ‘file’ parameters to run. You can then use the values from the hash, or use methods like download_result(). Params:
input-
string, the method of inputting a file. Examples are IDRCloudClient::UPLOAD or IDRCloudClient::DOWNLOAD
file-
string, (optional) Location of the PDF to convert, i.e ‘path/to/input.pdf’
url-
string, (optional) the url for the server to download a PDF from
Returns: hash [string: string], The results of the conversion
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/idr_cloud_client.rb', line 60 def convert(**params) uuid = upload params response = nil # check conversion status once every second until complete or error / timeout (0..@convert_timeout).each do |i| sleep 1 response = poll_status uuid break if response['state'] == 'processed' break unless params[:callbackUrl].nil? if response['state'] == 'error' exMessage = "Failed: Error with conversion\n" response.each do | key, val | exMessage = exMessage + "#{key}: $#{val}\n" if key != 'state' end raise(exMessage) end raise('Failed: File took longer than ' + @convert_timeout.to_s + ' seconds to convert') if i == @convert_timeout end response end |
#download_result(results, output_file_path, file_name = nil) ⇒ Object
Downloads the zip file produced by the microservice. Provide ‘.’ as the output_file_path if you wish to use the current directory. Will use the filename of the zip on the server if none is specified. Params:
output_file_path-
string, the output location to save the zip file
file_name-
string, (optional) the custom name for the zip file. This should not include .zip
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/idr_cloud_client.rb', line 92 def download_result(results, output_file_path, file_name=nil) download_url = results['downloadUrl'] raise('Error: downloadUrl parameter is empty') if download_url.nil? if file_name.nil? output_file_path += '/' + download_url.split('/').last else output_file_path += '/' + file_name + '.zip' end download download_url, output_file_path end |