lib/jss/api_object/advanced_search.rb
# Export the display fields of the search results to a file.
#
# @param output_file[String,Pathname] The file in which to store the exported results
#
# @param format[Symbol] one of :csv, :tab, or :xml, defaults to :csv
#
# @param overwrite[Boolean] should the output_file be overwrite if it exists? Defaults to false
#
# @return [Pathname] the path to the output file
#
# @note This method only exports the display fields defined in this advanced search for
# the search_result members (computers, mobile_devices, or users)
# It doesn't currently provide the ability to export subsets of info about those objects, as the
# Web UI does (e.g. group memberships, applications, receipts, etc)
#
Export the display fields of the search results to a file.
Export the display fields of the search results to a file .
def export(output_file, format = :csv, overwrite = false)
      raise JSS::InvalidDataError, "Export format must be one of: :#{EXPORT_FORMATS.join ', :'}" unless EXPORT_FORMATS.include? format
      out = Pathname.new output_file
      unless overwrite
        raise JSS::AlreadyExistsError, "The output file already exists: #{out}" if out.exist?
      end
      case format
      when :csv
        require 'csv'
        CSV.open(out.to_s, 'wb') do |csv|
          csv << @result_display_keys
          @search_results.each do |row|
            csv << @result_display_keys.map { |key| row[key] }
          end 
        end 
      when :tab
        tabbed = @result_display_keys.join("\t") + "\n"
        @search_results.each do |row|
          tabbed << @result_display_keys.map { |key| row[key] }.join("\t") + "\n"
        end 
        out.jss_save tabbed.chomp
      else 
        doc = REXML::Document.new '<?xml version="1.0" encoding="ISO-8859-1"?>'
        members = doc.add_element self.class::RESULT_CLASS::RSRC_LIST_KEY.to_s
        @search_results.each do |row|
          member = members.add_element self.class::RESULT_CLASS::RSRC_OBJECT_KEY.to_s
          @result_display_keys.each do |field|
            member.add_element(field.to_s.tr(' ', '_')).text = row[field].empty? ? nil : row[field]
          end 
        end 
        out.jss_save doc.to_s.gsub('><', ">\n<")
      end 
      out
    end
def export ( output_file , format = :csv , overwrite = false ) raise JSS :: InvalidDataError , "Export format must be one of: :#{EXPORT_FORMATS.join ', :'}" unless EXPORT_FORMATS . include? format out = Pathname . new output_file unless overwrite raise JSS :: AlreadyExistsError , "The output file already exists: #{out}" if out . exist? end case format when :csv require 'csv' CSV . open ( out . to_s , 'wb' ) do | csv | csv << @result_display_keys @search_results . each do | row | csv << @result_display_keys . map { | key | row [ key ] } end end when :tab tabbed = @result_display_keys . join ( "\t" ) + "\n" @search_results . each do | row | tabbed << @result_display_keys . map { | key | row [ key ] } . join ( "\t" ) + "\n" end out . jss_save tabbed . chomp else doc = REXML :: Document . new '<?xml version="1.0" encoding="ISO-8859-1"?>' members = doc . add_element self . class :: RESULT_CLASS :: RSRC_LIST_KEY . to_s @search_results . each do | row | member = members . add_element self . class :: RESULT_CLASS :: RSRC_OBJECT_KEY . to_s @result_display_keys . each do | field | member . add_element ( field . to_s . tr ( ' ' , '_' ) ) . text = row [ field ] . empty? ? nil : row [ field ] end end out . jss_save doc . to_s . gsub ( '><' , ">\n<" ) end out end
Export the display fields of the search results to a file.
Export the display fields of the search results to a file .
# each do row # CSV.open # each do row # :xml # ech do field # each do row # case
the path to the output file
the path to the output file
Pathname
output_file
The file in which to store the exported results
The file in which to store the exported results
format
one of :csv, :tab, or :xml, defaults to :csv
one of : csv : tab or : xml defaults to : csv
:csv
overwrite
should the output_file be overwrite if it exists. Defaults to false
should the output_file be overwrite if it exists . Defaults to false
note
This method only exports the display fields defined in this advanced search for the search_result members (computers, mobile_devices, or users) It doesn't currently provide the ability to export subsets of info about those objects, as the Web UI does
This method only exports the display fields defined in this advanced search for the search_result members ( computers mobile_devices or users ) It doesn ' t currently provide the ability to export subsets of info about those objects as the Web UI does