Method: NolijWeb::Handler#submit_document

Defined in:
lib/nolij_web/handler.rb

#submit_document(local_file, options = {}, &block) ⇒ Object

Submit a file. A local file path is required. required options: :folder_id additional options: :user_code, :wfma_code, :index_code, :dept_code, :custom_name



51
52
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
83
84
# File 'lib/nolij_web/handler.rb', line 51

def submit_document(local_file, options = {}, &block)
  folder_id = options[:folder_id]
  folder_id = if folder_id.kind_of?(Numeric) || folder_id.kind_of?(String)
                folder_id.to_s
              else
                nil
              end
  raise AttributeMissingError, 'Folder ID is required to submit a document.' unless folder_id.is_a?(String) && !folder_id.empty?

  local_file ||= ''
  file = begin
      File.new(local_file)
    rescue Errno::ENOENT
    end

  raise AttributeMissingError, 'Valid file or local filepath is required to submit a document.' unless file.is_a?(File)
  options[:file_name] = File.basename(file.path)
  allowed_query_params_keys = [:user_code, :wfma_code, :index_code, :dept_code, :custom_name, :file_name]
  formatted_query = query_str(:allowed_query_params_keys => allowed_query_params_keys , :query_params => options)
  relative_path = [@@doc_handler_path, folder_id].join('/') + formatted_query

  # TODO custom attributes?
  form_params = {}
  form_params[:ocrwords] = options[:ocr_words] if options.has_key?(:oci_words)
  form_params[:my_file] = file

  #upload file to nolijweb
  response = @connection.post relative_path, form_params, (options[:headers] || {}), &block

  # return Nolij file id
   = Nokogiri.XML(response)
  document_id = .xpath('//documentmeta/@documentid').first
  return document_id.value if document_id
end