Class: Quickbooks::Adapters::HttpAdapter::Connection

Inherits:
Object
  • Object
show all
Includes:
SimpleEncryption
Defined in:
lib/quickbooks/adapters/tcp_adapter.rb,
lib/quickbooks/adapters/http_adapter.rb

Instance Method Summary collapse

Methods included from SimpleEncryption

#decrypt, #encrypt

Constructor Details

#initialize(host, username, password) ⇒ Connection

Returns a new instance of Connection.



21
22
23
24
25
# File 'lib/quickbooks/adapters/tcp_adapter.rb', line 21

def initialize(ip, port, shared_key)
  @ip = ip
  @port = port
  @shared_key = shared_key
end

Instance Method Details

#closeObject



32
33
34
# File 'lib/quickbooks/adapters/tcp_adapter.rb', line 32

def close
  @connection.finish rescue nil
end

#connected?Boolean

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

Returns:



28
29
30
# File 'lib/quickbooks/adapters/tcp_adapter.rb', line 28

def connected?
  (@connection ||= new_connection).started?
end

#connectionObject

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



59
60
61
62
# File 'lib/quickbooks/adapters/tcp_adapter.rb', line 59

def connection
  @connection.start unless connected?
  @connection
end

#latest_qbxml_versionObject



54
55
56
# File 'lib/quickbooks/adapters/tcp_adapter.rb', line 54

def latest_qbxml_version
  @latest_qbxml_version ||= qbxml_versions.sort.last
end

#qbxml_versionsObject



49
50
51
52
53
# File 'lib/quickbooks/adapters/tcp_adapter.rb', line 49

def qbxml_versions
  res = try_retry(1, EOFError, :before_retry => lambda {@connection.finish}) { connection.get('/QBXMLVersionsForSession') }
  response = decrypt(res.body)
  return response.chomp.split(/,/)
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.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/quickbooks/adapters/tcp_adapter.rb', line 37

def send_xml(xml)
  # TODO: request.basic_auth url.user, url.password
  res = try_retry(1, EOFError, :before_retry => lambda {@connection.finish}) {
    connection.post('/ProcessRequest', encrypt(xml), {'Content-Type' => 'application/xml'})
  }
  response = decrypt(res.body)
  return response
# rescue => e
  # Rescue from the error: I guess we have to return a pretend response that says the network failed.
  # raise "Failsafe not yet implemented!: #{e.inspect}"
end