Class: ConnectStoopid::ReportingClient

Inherits:
Object
  • Object
show all
Defined in:
lib/connect-stoopid/reporting-client.rb

Instance Method Summary collapse

Constructor Details

#initialize(psa_address, company, username, password, options = {}) ⇒ ReportingClient

Parameters: psa_address – The hostname of your ConnectWise PSA, ie. con.companyconnect.net company – Company id used when logging into ConnectWise username – ConnectWise Integration username password – ConnectWise Integration password options – Override the default ReportingClient options



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/connect-stoopid/reporting-client.rb', line 17

def initialize(psa_address, company, username, password, options = {})
	@wsdl     = "https://#{psa_address}/v4_6_release/apis/1.5/ReportingApi.asmx?wsdl"
	@company  = company
	@username = username
	@password = password

	@client_options = {
		:client_logging => true,
		:client_logging_level => :error,
		:soap_version => 2,
		:soap_logging => false,
		:soap_logging_level => :fatal
	}
	@client_options.merge!(options)

	@soap_client = Savon.client({
		:wsdl => @wsdl,
		:soap_version => @client_options[:soap_version],
		:log => @client_options[:soap_logging],
		:log_level => @client_options[:soap_logging_level]
	})
end

Instance Method Details

#get_all_report_fields(options = {}) ⇒ Object

Parameters: options: Key value pairs to add to the Savon SOAP request For this request, options must include “reportName” => “Some_Report” Returns: [String]



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/connect-stoopid/reporting-client.rb', line 89

def get_all_report_fields(options = {})
	log_client_message("Getting list of available fields for a given report type", :debug)

	request_options = base_soap_hash
	request_options.merge!(options)

	begin
		response = @soap_client.call(:get_report_fields, :message => request_options)
	rescue Savon::SOAPFault => error
		log_client_message("SOAP Fault\nError Message:\n#{error}", :error)
	else
		if response.success?
			report_fields = []
			xml_doc = REXML::Document.new(response.to_xml)
			REXML::XPath.each(xml_doc, "//FieldInfo") do |field|
				report_fields << field.attributes["Name"].to_s
			end
			return report_fields.sort
		end
	end
end

#get_all_report_types(options = {"includeFields" => "false"}) ⇒ Object

Parameters: options: Key value pairs to add to the Savon SOAP request For this request, options must include “includeFields” => “true” (or “false”) Defaults to “includeFields” => “false” Returns: If the “includeFields” option is set to “false”, returns an array of strings containing all report names in alphabetical order If set to “true”, returns an array of hashes of format => “Activity”, :fields => [“field1”, “field2”, …]



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/connect-stoopid/reporting-client.rb', line 49

def get_all_report_types(options = {"includeFields" => "false"})
	log_client_message("Getting list of report types", :debug)

	request_options = base_soap_hash
	request_options.merge!(options)
	
	begin
		response = @soap_client.call(:get_reports, :message => request_options)
	rescue Savon::SOAPFault => error
		log_client_message("SOAP Fault\nError Message:\n#{error}", :error)
	else
		if response.success?
			report_types = []
			xml_doc = REXML::Document.new(response.to_xml)
			if options["includeFields"] == "false"
				REXML::XPath.each(xml_doc, "//Report") do |report|
					report_types << report.attributes["Name"].to_s
				end
				return report_types.sort
			elsif options["includeFields"] == "true"
				REXML::XPath.each(xml_doc, "//Report") do |report|
					report_fields = []
					REXML::XPath.each(report, "Field") do |field|
						report_fields << field.attributes["Name"].to_s
					end
					report_types << {:report_name => report.attributes["Name"], :fields => report_fields}
				end
				return report_types.sort_by! {|x| x[:report_name]}
			end
		end
	end
end

#run_report_count(options = {}) ⇒ Object

Parameters: options: Key value pairs to add to the Savon SOAP request For this request, options must include “reportName” => “Some_Report”, and should probably include “conditions” => “some condition set” Returns: Integer



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/connect-stoopid/reporting-client.rb', line 119

def run_report_count(options = {})
	log_client_message("Getting a count of records per a set of conditions", :debug)

	request_options = base_soap_hash
	request_options.merge!(options)

	begin
		response = @soap_client.call(:run_report_count, :message => request_options)
	rescue Savon::SOAPFault => error
		log_client_message("SOAP Fault\nError Message:\n#{error}", :error)
	else
		if response.success?
			xml_doc = REXML::Document.new(response.to_xml)
			return REXML::XPath.first(xml_doc, "//RunReportCountResult").text.to_i
		end
	end
end

#run_report_query(options = {}) ⇒ Object

Parameters: options: Key value pairs to add to the Savon SOAP request For this request, options must include “reportName” => “Some_Report”, and should probably (read: definitely) include “conditions” => “some condition set” Returns: [=> value1, field2 => value2]



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/connect-stoopid/reporting-client.rb', line 145

def run_report_query(options = {})
	log_client_message("Running full query per a set of conditions", :debug)

	request_options = base_soap_hash
	request_options.merge!(options)

	begin
		response = @soap_client.call(:run_report_query, :message => request_options)
	rescue Savon::SOAPFault => error
		log_client_message("SOAP Fault\nError Message:\n#{error}", :error)
	else
		if response.success?
			xml_doc = REXML::Document.new(response.to_xml)
			rows = []
			REXML::XPath.each(xml_doc, "//ResultRow") do |row|
				row_key_vals = {}
				REXML::XPath.each(row, "Value") do |col|
					row_key_vals[col.attributes["Name"].to_s] = col.text.to_s
				end
				rows << row_key_vals
			end
			return rows
		end
	end
end