class Connectwise::Connection

Attributes

custom_api_mapping[R]
host[R]
log[RW]

Public Class Methods

new(host: '', company_name: '', integrator_login_id: '', integrator_password: '', custom_api_mapping: {}) click to toggle source
# 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: integrator_login_id, IntegratorPassword: integrator_password}
  HTTPI.adapter = :net_http
end

Public Instance Methods

call(api, action, message, options: {}, &err_handler) click to toggle source
# 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

Private Instance Methods

api(desired_api) click to toggle source
# File lib/connectwise/connection.rb, line 51
def api(desired_api)
  api = api_mapping.fetch(desired_api) {desired_api}.to_s
  api.extend(Connectwise::Extensions::String)
  api =~ /Api\z/ ? api : "#{api.camelize}Api"
end
api_mapping() click to toggle source
# File lib/connectwise/connection.rb, line 57
def api_mapping
  {
    billing: :generic_billing_transaction,
    config: :configuration,
    device: :managed_device,
    ticket: :service_ticket,
  }.merge(custom_api_mapping)
end
camelize(term) click to toggle source
# File lib/connectwise/connectwise.rb, line 60
def camelize(term)
  string = term.to_s.sub(/^[a-z\d]*/) { $&.capitalize }
  string.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
end
credentials() click to toggle source
# File lib/connectwise/connection.rb, line 47
def credentials
  {credentials: @credentials}
end
default_options() click to toggle source
# File lib/connectwise/connection.rb, line 37
def default_options
  defaults = { convert_request_keys_to: :none, ssl_verify_mode: :none, follow_redirects: true }
  defaults = defaults.merge({ log: true, pretty_print_xml: true }) if @log
  defaults
end
wsdl_url(api_name) click to toggle source
# File lib/connectwise/connection.rb, line 43
def wsdl_url(api_name)
  "https://#{host}/v4_6_release/apis/1.5/#{api(api_name)}.asmx?wsdl"
end