Class: Connectwise::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/connectwise/connection.rb,
lib/connectwise/connectwise.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: '', company_name: '', integrator_login_id: '', integrator_password: '', custom_api_mapping: {}) ⇒ Connection

Returns a new instance of Connection.



6
7
8
9
10
11
# File 'lib/connectwise/connection.rb', line 6

def initialize(host: '', company_name: '', integrator_login_id: '', integrator_password: '', custom_api_mapping: {})
  @custom_api_mapping = custom_api_mapping
  @host = host
  @credentials = {CompanyId: company_name, IntegratorLoginId: , IntegratorPassword: integrator_password}
  HTTPI.adapter = :net_http
end

Instance Attribute Details

#custom_api_mappingObject (readonly)

Returns the value of attribute custom_api_mapping.



3
4
5
# File 'lib/connectwise/connection.rb', line 3

def custom_api_mapping
  @custom_api_mapping
end

#hostObject (readonly)

Returns the value of attribute host.



3
4
5
# File 'lib/connectwise/connection.rb', line 3

def host
  @host
end

#logObject

Returns the value of attribute log.



4
5
6
# File 'lib/connectwise/connection.rb', line 4

def log
  @log
end

Instance Method Details

#call(api, action, message, options: {}, &err_handler) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/connectwise/connection.rb', line 13

def call(api, action, message, options: {}, &err_handler)
  err_handler ||= proc {|err| raise err}

  client = Savon.client(default_options.merge(options).merge(wsdl: wsdl_url(api)))
  response = client.call action, message: credentials.merge(message)
  response.body["#{action}_response".to_sym]["#{action}_result".to_sym]
rescue Savon::SOAPFault, Savon::UnknownOperationError, SocketError, URI::InvalidURIError => err
  begin
    case err.message
    when /username or password is incorrect/i
      raise BadCredentialsError.new 'Login or Password are incorrect'
    when /hostname nor servname/i
      raise UnknownHostError.new "The host (#{@host}) is not reachable"
    when /cannot find company.*connectwise config/i
      raise UnknownCompanyError.new "The company (#{@credentials[:CompanyId]}) cannot be found by Connectwise"
    else
      raise ConnectionError.new "An unknown error occurred when contacting Connectwise : \n#{err.message}"
    end
  rescue BadCredentialsError, UnknownHostError, UnknownCompanyError, ConnectionError => err
    err_handler.call(err)
  end
end