lib/QuickBaseClient.rb in quickbase_client-1.0.24 vs lib/QuickBaseClient.rb in quickbase_client-1.0.25

- old
+ new

@@ -13,10 +13,11 @@ require 'rexml/document' require 'net/https' require 'json' require 'QuickBaseMisc' +require 'csv' begin require 'httpclient' USING_HTTPCLIENT = true rescue LoadError @@ -60,11 +61,11 @@ attr_reader :username, :users, :value, :validFieldProperties, :validFieldTypes, :variables attr_reader :varname, :version, :vid, :view, :withembeddedtables attr_reader :eventSubscribers, :logger attr_writer :cacheSchemas, :apptoken, :escapeBR, :fvlist, :httpConnection, :ignoreCR, :ignoreLF, :ignoreTAB - attr_writer :printRequestsAndResponses, :qbhost, :stopOnError, :ticket, :udata, :rdr, :xsl, :encoding + attr_writer :printRequestsAndResponses, :qbhost, :stopOnError, :ticket, :udata, :rdr, :xsl, :encoding, :usertoken =begin rdoc 'Plumbing' methods: These methods implement the core functionality to make the API_ wrapper methods and the 'Helper' methods work. @@ -88,32 +89,31 @@ showTrace = false, org = "www", apptoken = nil, debugHTTPConnection = false, domain = "quickbase", - proxy_options = nil + proxy_options = nil, + usertoken = nil ) begin @org = org ? org : "www" @domain = domain ? domain : "quickbase" @apptoken = apptoken + @usertoken = usertoken @printRequestsAndResponses = printRequestsAndResponses @stopOnError = stopOnError @escapeBR = @ignoreCR = @ignoreLF = @ignoreTAB = true toggleTraceInfo( showTrace ) if showTrace setHTTPConnectionAndqbhost( useSSL, org, domain, proxy_options ) debugHTTPConnection() if debugHTTPConnection @standardRequestHeaders = { "Content-Type" => "application/xml" } - if username and password - authenticate( username, password ) if appname and @errcode == "0" findDBByname( appname ) if @dbid and @errcode == "0" getDBInfo( @dbid ) getSchema( @dbid ) end - end end rescue Net::HTTPBadRequest => @lastError rescue Net::HTTPBadResponse => @lastError rescue Net::HTTPHeaderSyntaxError => @lastError rescue StandardError => @lastError @@ -143,11 +143,12 @@ options["showTrace"], options["org"], options["apptoken"], options["debugHTTPConnection"], options["domain"], - options["proxy_options"]) + options["proxy_options"], + options["usertoken"]) end # Initializes the connection to QuickBase. def setHTTPConnection( useSSL, org = "www", domain = "quickbase", proxy_options = nil ) @useSSL = useSSL @@ -290,25 +291,20 @@ def getDBforRequestURL( api_Request ) @dbidForRequestURL = "/db/#{@dbid}" case api_Request when :getAppDTMInfo @dbidForRequestURL = "/db/main?a=#{:getAppDTMInfo}&dbid=#{@dbid}" - when :authenticate, :createDatabase, :deleteAppZip, :dumpAppZip, :getUserInfo, :findDBByname, :getOneTimeTicket, :getFileUploadToken, :grantedDBs, :installAppZip, :obStatus, :signOut + when :authenticate, :createDatabase, :deleteAppZip, :dumpAppZip, :getUserInfo, :findDBByname, :GrantedDBs, :getOneTimeTicket, :getFileUploadToken, :grantedDBs, :installAppZip, :obStatus, :signOut @dbidForRequestURL = "/db/main" end end # Returns the request XML for either a ticket or a username and password. # The XML includes a apptoken if one has been set. def getAuthenticationXMLforRequest( api_Request ) @authenticationXML = "" - if @ticket - @authenticationXML = toXML( :ticket, @ticket ) - elsif @username and @password - @authenticationXML = toXML( :username, @username ) + toXML( :password, @password ) - end - @authenticationXML << toXML( :apptoken, @apptoken ) if @apptoken + @authenticationXML = toXML( :usertoken, @usertoken ) end # Returns whether a request will return HTML rather than XML. def isHTMLRequest?( api_Request ) ret = false @@ -441,10 +437,23 @@ e = getResponseElements( path ) e.each{ |e| @fieldValue << e.text if e and e.is_a?( REXML::Element ) and e.has_text? } @fieldValue end + #Gets a dbid at an Xpath in the XML from specified dbName of Quickbase + def getResponsePathValueByDBName ( path, dbName) + @fieldValue = "" + if path and @responseXMLdoc + e = @responseXMLdoc.root.elements[ path.to_s ] + end + e.each { |e| + if e and e.is_a?( REXML::Element ) and e.dbinfo.dbname == dbName + return e.dbinfo.dbid + end + } + @fieldValue + end # Gets an array of elements at an Xpath in the XML from QuickBase. def getResponseElements( path ) if path and @responseXMLdoc @responseElements = @responseXMLdoc.get_elements( path.to_s ) end @@ -1018,12 +1027,12 @@ begin httpclient = HTTPClient.new resp = httpclient.get("https://www.quickbase.com/db/#{dbid}") location = resp.header['Location'][0] location.sub!("https://","") - parts = location.split(/\./) - @realm = parts[0] + parts = location.split(/\./) + @realm = parts[0] rescue StandardError => error @realm = nil end else raise "Please get the HTTPClient gem: gem install httpclient" @@ -1493,24 +1502,12 @@ milliseconds end # Converts a string into an array, given a field separator. # '"' followed by the field separator are treated the same way as just the field separator. - def splitString( string, fieldSeparator = "," ) - ra = [] - string.chomp! - if string.include?( "\"" ) - a=string.split( "\"#{fieldSeparator}" ) - a.each{ |b| c=b.split( "#{fieldSeparator}\"" ) - c.each{ |d| - ra << d - } - } - else - ra = string.split( fieldSeparator ) - end - ra + def splitString(string, fieldSeparator = ",") + CSV.parse(string, col_sep: fieldSeparator).shift end # Returns the URL-encoded version of a non-printing character. def escapeXML( char ) if @xmlEscapes.nil? @@ -2337,12 +2334,12 @@ # API_FindDBByname def findDBByname( dbname ) @dbname = dbname xmlRequestData = toXML( :dbname, @dbname ) - sendRequest( :findDBByname, xmlRequestData ) - @dbid = getResponseValue( :dbid ) + sendRequest( :GrantedDBs, xmlRequestData ) + @dbid = getResponsePathValueByDBName( 'databases', dbname ) return self if @chainAPIcalls @dbid end alias findDBByName findDBByname @@ -3084,13 +3081,14 @@ # ----------------------------------------------------------------------------------------------- # Use this if you aren't sure whether a particular record already exists or not def addOrEditRecord( dbid, fvlist, rid = nil, disprec = nil, fform = nil, ignoreError = nil, update_id = nil, key = nil ) if rid or key - editRecord( dbid, rid, fvlist, disprec, fform, ignoreError, update_id, key ) + record = editRecord( dbid, rid, fvlist, disprec, fform, ignoreError, update_id, key ) if !@requestSucceeded addRecord( dbid, fvlist, disprec, fform, ignoreError, update_id ) end + record else addRecord( dbid, fvlist, disprec, fform, ignoreError, update_id ) end end