Sha256: 4b98a1ef734f033d84599d4fac5dc1723b69e8a6d7c0165a7df06ca78f201529

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require 'broker/client/quickbase_client'

module Broker
 # module QB
    class Session
      
      attr_reader :client, :app, :ext
    
      def initialize(opt={})
        @app = Broker.lookup_appname(opt[:app])
        @ext = Broker.options[:file_ext]
      
        credentials = {
          "username" => Broker.secrets['USERNAME'],
          "password" => Broker.secrets['PASSWORD'],
          "appname"  => @app,
          "org"      => Broker.secrets['ORG'],
          "apptoken" => opt[:token] || Broker.secrets['TOKEN']
        }
        
        begin
          @client = QuickBase::Client.init(credentials)
        rescue => e
          puts e.message
          #raise ArgumentError
        ensure
          # We successfully logged into quickbase, but supplied an invalid app name
          if @client && @client.errcode == "32"
            @client.signOut
            raise ArgumentError
          end
        end
      end
      
      def fire_event
        raise NotImplementedError
      end
      
      def sign_out
        @client.signOut
      end
      
      def qb_ready?(name)
        app_name = Broker.lookup_appname(name)
        unless app_name == @client.dbname
          return app_name && change_app(app_name, name)
        end
        true
      end
    
      def field_names(table)
        table &&= table.to_s
        db = Broker.tables[@app]['tables'][table]
        db && @client.getFieldNames(db, "", true)
      end
      
      private
      
      def change_app(app_name, app_key)
        @app = app_key.to_s
        @client.findDBByname(app_name)
        @client.dbname == app_name
      end
    end
 # end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
broker-0.1.1 lib/broker/session.rb