class ConvertApi::Task
Public Class Methods
new(from_format, to_format, params, conversion_timeout: nil)
click to toggle source
# File lib/convert_api/task.rb, line 3 def initialize(from_format, to_format, params, conversion_timeout: nil) @from_format = from_format @to_format = to_format @params = params @conversion_timeout = conversion_timeout || config.conversion_timeout end
Public Instance Methods
run()
click to toggle source
# File lib/convert_api/task.rb, line 10 def run params = normalize_params(@params).merge( Timeout: @conversion_timeout, StoreFile: true, ) from_format = @from_format || detect_format(params) read_timeout = @conversion_timeout + config.conversion_timeout_delta if @conversion_timeout converter = params[:converter] ? "/converter/#{params[:converter]}" : '' response = ConvertApi.client.post( "convert/#{from_format}/to/#{@to_format}#{converter}", params, read_timeout: read_timeout, ) Result.new(response) end
Private Instance Methods
config()
click to toggle source
# File lib/convert_api/task.rb, line 72 def config ConvertApi.config end
detect_format(params)
click to toggle source
# File lib/convert_api/task.rb, line 64 def detect_format(params) return DEFAULT_URL_FORMAT if params[:Url] resource = params[:File] || Array(params[:Files]).first FormatDetector.new(resource).run end
files_batch(values)
click to toggle source
# File lib/convert_api/task.rb, line 52 def files_batch(values) files = Array(values).map { |file| FileParam.build(file) } # upload files in parallel files .select { |file| file.is_a?(UploadIO) } .map { |upload_io| Thread.new { upload_io.file_id } } .map(&:join) files end
normalize_params(params)
click to toggle source
# File lib/convert_api/task.rb, line 31 def normalize_params(params) result = {} symbolize_keys(params).each do |key, value| case key when :File result[:File] = FileParam.build(value) when :Files result[:Files] = files_batch(value) else result[key] = value end end result end
symbolize_keys(hash)
click to toggle source
# File lib/convert_api/task.rb, line 48 def symbolize_keys(hash) hash.map { |k, v| [k.to_sym, v] }.to_h end