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

Inherits:
Object
  • Object
show all
Includes:
SimpleEncryption
Defined in:
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.



41
42
43
44
45
46
47
48
# File 'lib/quickbooks/adapters/http_adapter.rb', line 41

def initialize(host, username, password)
  host, port = host.split(/:/,2) if host =~ /:/
  port = nil if port == ''
  @host = host
  @port = port || 56225
  @username = username
  @password = password
end

Instance Method Details

#closeObject



55
56
57
# File 'lib/quickbooks/adapters/http_adapter.rb', line 55

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:



51
52
53
# File 'lib/quickbooks/adapters/http_adapter.rb', line 51

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

#connectionObject

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



82
83
84
85
# File 'lib/quickbooks/adapters/http_adapter.rb', line 82

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

#latest_qbxml_versionObject



77
78
79
# File 'lib/quickbooks/adapters/http_adapter.rb', line 77

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

#qbxml_versionsObject



72
73
74
75
76
# File 'lib/quickbooks/adapters/http_adapter.rb', line 72

def qbxml_versions
  res = try_retry(1, EOFError, :before_retry => lambda {@connection.finish}) { connection.get('/QBXMLVersionsForSession', {'Authorization' => "Basic #{Base64.encode64(@username+':'+@password).gsub(/\n/,'')}"}) }
  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.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/quickbooks/adapters/http_adapter.rb', line 60

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', 'Authorization' => "Basic #{Base64.encode64(@username+':'+@password).gsub(/\n/,'')}"})
  }
  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