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.



44
45
46
47
48
# File 'lib/connect_wise_web_reports/report.rb', line 44

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
# File 'lib/connect_wise_web_reports/report.rb', line 12

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

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

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

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

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

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

  # 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))


51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/connect_wise_web_reports/report.rb', line 51

def fetch
  response = ConnectWiseWebReports.agent(options).get(url)
  doc = Nokogiri::XML(response.content)

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

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

  return self.records
end

#next_pageObject



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

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)


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

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 ###



82
83
84
85
86
87
88
89
# File 'lib/connect_wise_web_reports/report.rb', line 82

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



69
70
71
72
73
74
75
76
77
# File 'lib/connect_wise_web_reports/report.rb', line 69

def parse_records(rows)
  self.records = []

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

#previous_pageObject



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

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)


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

def previous_page?
  self.page > 1
end

#urlObject



65
66
67
# File 'lib/connect_wise_web_reports/report.rb', line 65

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