Class: ConnectWiseWebReports::Report
- Inherits:
-
Object
- Object
- ConnectWiseWebReports::Report
- Defined in:
- lib/connect_wise_web_reports/report.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#records ⇒ Object
Returns the value of attribute records.
Class Method Summary collapse
-
.url(name, options = {}) ⇒ String
Generate the Web Report request url.
Instance Method Summary collapse
- #fetch ⇒ Array(Hash)
-
#initialize(name, options = {}) ⇒ Report
constructor
A new instance of Report.
- #next_page ⇒ Object
- #next_page? ⇒ Boolean
-
#page ⇒ Object
Pagination ###.
- #parse_records(rows) ⇒ Object
- #previous_page ⇒ Object
- #previous_page? ⇒ Boolean
- #url ⇒ Object
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, = {}) @name = name @options = ConnectWiseWebReports::DEFAULT_OPTIONS.merge() @records = fetch end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/connect_wise_web_reports/report.rb', line 5 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/connect_wise_web_reports/report.rb', line 5 def @options end |
#records ⇒ Object
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.
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, = {}) # Report params = "r=#{name}" # API credentials params += "&c=#{CGI.escape [:company_id].to_s}" params += "&u=#{CGI.escape [:integrator_id].to_s}" params += "&p=#{CGI.escape [:integrator_password].to_s}" #order params += "&o=#{CGI.escape [:order_by].to_s}" unless [:order_by].nil? # pagination params += "&l=#{CGI.escape [:limit].to_s}" unless [:limit].nil? params += "&s=#{CGI.escape [:skip].to_s}" unless [:skip].nil? # timeout params += "&t=#{CGI.escape [:timeout].to_s}" unless [:timeout].nil? # fields unless [:fields].nil? || [:fields].empty? params += "&f=#{[:fields].map { |f| CGI.escape f }.join('&f=')}" end # conditions params += "&q=#{CGI.escape [:conditions]}" unless [:conditions].blank? url = "https://#{[:host]}/#{[:version]}/webreport/?#{params}" ConnectWiseWebReports.logger.info url return url end |
Instance Method Details
#fetch ⇒ 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().get(url) rescue StandardError => e raise ConnectionError.new(e.) 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_page ⇒ Object
106 107 108 109 110 111 |
# File 'lib/connect_wise_web_reports/report.rb', line 106 def next_page self.[:skip] = 0 if self.[:skip].nil? self.[:skip] += self.[:limit] return self.fetch end |
#next_page? ⇒ Boolean
98 99 100 101 102 103 104 |
# File 'lib/connect_wise_web_reports/report.rb', line 98 def next_page? if self.[:limit].nil? || self.[:limit].zero? || self.records.count < self.[:limit] return false else return true end end |
#page ⇒ Object
Pagination ###
89 90 91 92 93 94 95 96 |
# File 'lib/connect_wise_web_reports/report.rb', line 89 def page if self.[:limit].nil? return 1 else self.[:skip] = 0 if self.[:skip].nil? return (self.[:skip] / self.[: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_page ⇒ Object
117 118 119 120 121 122 |
# File 'lib/connect_wise_web_reports/report.rb', line 117 def previous_page self.[:skip] -= self.[:limit] self.[:skip] = [0, self.[:skip]].max # ensure we don't skip negative return self.fetch end |
#previous_page? ⇒ Boolean
113 114 115 |
# File 'lib/connect_wise_web_reports/report.rb', line 113 def previous_page? self.page > 1 end |