Class: Quickbooks::Connection

Inherits:
Object
  • Object
show all
Includes:
QBHelpers
Defined in:
lib/quickbooks/connection.rb

Overview

Connection is used internally by Quickbooks::Base to automatically manage a the communication with quickbooks. Currently the only connection mode supported is WIN32OlE, which means your application must be running on the same computer as Quickbooks.

Quickbooks does not allow the user to close the company or exit the program until all outside applications are disconnected from its API. Therefore, this class includes an at_exit hook that automatically closes all open connections for you when your program exits.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application_name = 'RubyApplication', file = '', user = '', password = '', connection_type = 'localQBD', connection_mode = 'DoNotCare') ⇒ Connection

Initializes an instance of Quickbooks::Session.

  • application_name is required.

  • file is optional, in which case the connection will be made to the currently open company file in the Quickbooks application.

  • user and password may be required if you have specified a specific file to open.

  • connection_type can be one of: [‘unknown’, ‘localQBD’, ‘remoteQBD’, ‘localQBDLaunchUI’, ‘remoteQBOE’]

  • connection_mode can be one of: [‘SingleUser’, ‘MultiUser’, ‘DoNotCare’]



37
38
39
40
41
42
43
44
45
# File 'lib/quickbooks/connection.rb', line 37

def initialize(application_name='RubyApplication', file='', user='', password='', connection_type='localQBD', connection_mode='DoNotCare')
  @file = file
  @user = user
  @password = password
  @application_name = application_name
  @quickbooks = Ole.new('QBXMLRP2.RequestProcessor', 'QBXMLRP2 1.0 Type Library')
  @connection_type = @quickbooks.get_variable(connection_type)
  @connection_mode = @quickbooks.get_variable('qbFileOpen'+connection_mode)
end

Class Method Details

.connectionsObject



16
17
18
# File 'lib/quickbooks/connection.rb', line 16

def connections
  @connections || (@connections = [])
end

.connections=(v) ⇒ Object



19
20
21
22
# File 'lib/quickbooks/connection.rb', line 19

def connections=(v)
  # raise ArgumentError, "Quickbooks::Connection.connections can only contain Quickbooks::Connection objects, but contains #{v.collect {|c| c.class.name}.uniq.join(', ')} objects" unless v.collect {|c| c.class.name}.uniq == ['Quickbooks::Connection']
  @connections = v
end

Instance Method Details

#closeObject

Close the connection to Quickbooks. Automatically ends the session, if there is one.



95
96
97
98
99
100
101
102
103
# File 'lib/quickbooks/connection.rb', line 95

def close
  end_session
  if connected? && connection.CloseConnection
    @connected = false
    @connection = nil
    Quickbooks::Connection.connections = Quickbooks::Connection.connections - [self]
  end
  return !@connected # Returns false if CloseConnection failed.
end

#connected?Boolean

Returns true if there is an open connection to Quickbooks, false if not. Use session? to determine an open session.

Returns:

  • (Boolean)


48
49
50
# File 'lib/quickbooks/connection.rb', line 48

def connected?
  @connected ||= false
end

#connectionObject

Returns the active connection to Quickbooks, or creates a new one if none exists.



66
67
68
69
70
71
72
73
74
# File 'lib/quickbooks/connection.rb', line 66

def connection
  @connection || begin
    @connection = @quickbooks.ole
    @connection.OpenConnection2('',@application_name,@connection_type)
    Quickbooks::Connection.connections << self
    @connected = true
    @connection
  end
end

#end_sessionObject

End the current Quickbooks session. After ending a session, a new session may be reopened if desired.



84
85
86
87
88
89
90
91
92
# File 'lib/quickbooks/connection.rb', line 84

def end_session
  if !@session.nil?
    connection.EndSession(@session)
    @session = nil
    true
  else
    false
  end
end

#send_xml(xml) ⇒ Object

Sends a request to Quickbooks. This request should be a valid QBXML request. Use Quickbooks::Qbxml::Request to generate valid requests.



58
59
60
61
62
63
# File 'lib/quickbooks/connection.rb', line 58

def send_xml(xml)
  @quickbooks.ProcessRequest(session, xml)
rescue => e
  warn "ERROR processing request:\n#{xml}"
  raise # Reraises the original error, only this way we got the xml output
end

#sessionObject

Begin a session to Quickbooks.



77
78
79
80
81
# File 'lib/quickbooks/connection.rb', line 77

def session
  @session || begin
    @session = connection.BeginSession(@file,@connection_mode)
  end
end

#session?Boolean

Returns true if there is an active session. Use connected? to determine whether you are connected or not.

Returns:

  • (Boolean)


53
54
55
# File 'lib/quickbooks/connection.rb', line 53

def session?
  !@session.nil?
end