Class: ConnectWiseWebReports::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/connect_wise_web_reports/report.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Report

Returns a new instance of Report.



46
47
48
49
50
# File 'lib/connect_wise_web_reports/report.rb', line 46

def initialize(name, options = {})
  @name = name
  @options = ConnectWiseWebReports::DEFAULT_OPTIONS.merge(options)
  @records = fetch
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/connect_wise_web_reports/report.rb', line 5

def name
  @name
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/connect_wise_web_reports/report.rb', line 5

def options
  @options
end

#recordsObject

Returns the value of attribute records.



5
6
7
# File 'lib/connect_wise_web_reports/report.rb', line 5

def records
  @records
end

Class Method Details

.url(name, options = {}) ⇒ String

Generate the Web Report request url.

Parameters:

  • name (String)
  • options (Hash) (defaults to: {})

Returns:

  • (String)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/connect_wise_web_reports/report.rb', line 12

def self.url(name, options = {})
  # Report
  params = "r=#{name}"

  # API credentials
  params += "&c=#{CGI.escape options[:company_id].to_s}"
  params += "&u=#{CGI.escape options[:integrator_id].to_s}"
  params += "&p=#{CGI.escape options[:integrator_password].to_s}"

  #order
  params += "&o=#{CGI.escape options[:order_by].to_s}" unless options[:order_by].nil?

  # pagination
  params += "&l=#{CGI.escape options[:limit].to_s}" unless options[:limit].nil?
  params += "&s=#{CGI.escape options[:skip].to_s}" unless options[:skip].nil?

  # timeout
  params += "&t=#{CGI.escape options[:timeout].to_s}" unless options[:timeout].nil?

  # fields
  unless options[:fields].nil? || options[:fields].empty?
    params += "&f=#{options[:fields].map { |f| CGI.escape f }.join('&f=')}"
  end

  # conditions
  params += "&q=#{CGI.escape options[:conditions]}" unless options[:conditions].blank?

  url = "https://#{options[:host]}/#{options[:version]}/webreport/?#{params}"

  ConnectWiseWebReports.logger.info url

  return url
end

Instance Method Details

#fetchArray(Hash)

Returns:

  • (Array(Hash))


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/connect_wise_web_reports/report.rb', line 53

def fetch
  begin
    response = ConnectWiseWebReports.agent(options).get(url)
  rescue StandardError => e
    raise ConnectionError.new(e.message)
  end

  doc = Nokogiri::XML(response.content)

  # raise errors if we got them
  unless doc.xpath('//error/message').empty?
    raise ConnectionError.new(doc.xpath('//error/message').first.children.first.text)
  end

  self.parse_records doc.xpath('//results/row')

  return self.records
end

#next_pageObject



106
107
108
109
110
111
# File 'lib/connect_wise_web_reports/report.rb', line 106

def next_page
  self.options[:skip] = 0 if self.options[:skip].nil?
  self.options[:skip] += self.options[:limit]

  return self.fetch
end

#next_page?Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
# File 'lib/connect_wise_web_reports/report.rb', line 98

def next_page?
  if self.options[:limit].nil? || self.options[:limit].zero? || self.records.count < self.options[:limit]
    return false
  else
    return true
  end
end

#pageObject

Pagination ###



89
90
91
92
93
94
95
96
# File 'lib/connect_wise_web_reports/report.rb', line 89

def page
  if self.options[:limit].nil?
    return 1
  else
    self.options[:skip] = 0 if self.options[:skip].nil?
    return (self.options[:skip] / self.options[:limit]) + 1
  end
end

#parse_records(rows) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/connect_wise_web_reports/report.rb', line 76

def parse_records(rows)
  self.records = []

  rows.each do |row|
    record = Hash.from_xml(row.to_s)['row']
    record.delete 'result_number'
    self.records << record
  end
end

#previous_pageObject



117
118
119
120
121
122
# File 'lib/connect_wise_web_reports/report.rb', line 117

def previous_page
  self.options[:skip] -= self.options[:limit]
  self.options[:skip] = [0, self.options[:skip]].max # ensure we don't skip negative

  return self.fetch
end

#previous_page?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/connect_wise_web_reports/report.rb', line 113

def previous_page?
  self.page > 1
end

#urlObject



72
73
74
# File 'lib/connect_wise_web_reports/report.rb', line 72

def url
  Report.url(self.name, self.options)
end