Class: QuickBase::Client::FieldValuePairXML

Inherits:
Object
  • Object
show all
Defined in:
lib/QuickBaseClient.rb

Overview

Encapsulates field values to be set and file uploads to be made during addRecord() and editRecord() calls.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parentClass, name, fid, filename, value) ⇒ FieldValuePairXML

Returns a new instance of FieldValuePairXML.



1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
# File 'lib/QuickBaseClient.rb', line 1000

def initialize( parentClass, name, fid, filename, value )

   @parentClass = parentClass

   name = name.downcase if name
   name = name.gsub( /\W/, "_" )  if name
 
   if filename or value
      @xml = "<field "
      if name
         @xml << "name='#{name}'"
      elsif fid
         @xml << "fid='#{fid}'"
      else
         raise "FieldValuePairXML::initialize: must specify 'name' or 'fid'"
      end
      if filename
         @xml << " filename='#{verifyFilename(filename)}'"
      end
      if value
         if filename
            value = encodeFileContentsForUpload( value )
         else
            value = @parentClass.encodeXML( value )
         end
         @xml << ">#{value}</field>"
      elsif filename
         value = encodeFileContentsForUpload( filename )
         @xml << ">#{value}</field>"
      else
         @xml << "/>"
      end
   else
      raise "FieldValuePairXML::initialize: must specify 'filename' or 'value'"
   end
end

Instance Attribute Details

#parentClassObject (readonly)

Returns the value of attribute parentClass.



998
999
1000
# File 'lib/QuickBaseClient.rb', line 998

def parentClass
  @parentClass
end

Instance Method Details

#encodeFileContentsForUpload(fileNameOrFileContents) ⇒ Object



1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
# File 'lib/QuickBaseClient.rb', line 1046

def encodeFileContentsForUpload( fileNameOrFileContents )
   if fileNameOrFileContents
      if FileTest.readable?( fileNameOrFileContents )
         f = File.new( fileNameOrFileContents, "r" )
         if f
             encodedFileContents = ""
             f.binmode
             buffer = f.read(60)
             while buffer
               encodedFileContents << [buffer].pack('m').tr( "\r\n", '' )
               buffer = f.read(60)
             end
             f.close
             return encodedFileContents
        end
     elsif fileNameOrFileContents.is_a?( String )
        encodedFileContents = ""
        buffer = fileNameOrFileContents.slice!(0,60)
        while buffer and buffer.length > 0 
           buffer = buffer.to_s
           encodedFileContents << [buffer].pack('m').tr( "\r\n", '' )
           buffer = fileNameOrFileContents.slice!(0,60)
        end
        return encodedFileContents
     end
  end
  nil
end

#to_sObject



1075
1076
1077
# File 'lib/QuickBaseClient.rb', line 1075

def to_s
   @xml
end

#verifyFilename(filename) ⇒ Object



1037
1038
1039
1040
1041
1042
1043
1044
# File 'lib/QuickBaseClient.rb', line 1037

def verifyFilename( filename )
   if filename
      filename.slice!( 0, filename.rindex( '\\' )+1 ) if filename.include?( '\\' )
      filename.slice!( 0, filename.rindex( '/' )+1 ) if filename.include?( '/' )
      filename = @parentClass.encodeXML( filename )
   end
   filename
end