Class: Quickbooks::OleAdapter::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/quickbooks/adapters/ole_adapter.rb

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::Connection.

  • 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’]



79
80
81
82
83
84
85
86
87
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 79

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



58
59
60
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 58

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

.connections=(v) ⇒ Object



61
62
63
64
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 61

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.



137
138
139
140
141
142
143
144
145
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 137

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)


90
91
92
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 90

def connected?
  @connected ||= false
end

#connectionObject

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



108
109
110
111
112
113
114
115
116
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 108

def connection
  @connection || begin
    @connection = @quickbooks.ole
    @connection.OpenConnection2('',@application_name,@connection_type)
    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.



126
127
128
129
130
131
132
133
134
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 126

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 Qbxml::Request to generate valid requests.



100
101
102
103
104
105
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 100

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.



119
120
121
122
123
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 119

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)


95
96
97
# File 'lib/quickbooks/adapters/ole_adapter.rb', line 95

def session?
  !@session.nil?
end