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.
44 45 46 47 48 |
# File 'lib/connect_wise_web_reports/report.rb', line 44 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 |
# File 'lib/connect_wise_web_reports/report.rb', line 12 def self.url(name, = {}) # Report params = "r=#{name}" # API credentials params += "&c=#{[:company_id].to_s}" params += "&u=#{[:integrator_id].to_s}" params += "&p=#{[:integrator_password].to_s}" #order params += "&o=#{[:order_by].to_s}" unless [:order_by].nil? # pagination params += "&l=#{[:limit].to_s}" unless [:limit].nil? params += "&s=#{[:skip].to_s}" unless [:skip].nil? # timeout params += "&t=#{[:timeout].to_s}" unless [:timeout].nil? # fields params += "&f=#{[:fields].join('&f=')}" unless [:fields].nil? || [:fields].empty? # 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)
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().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_page ⇒ Object
99 100 101 102 103 104 |
# File 'lib/connect_wise_web_reports/report.rb', line 99 def next_page self.[:skip] = 0 if self.[:skip].nil? self.[:skip] += self.[:limit] return self.fetch end |
#next_page? ⇒ Boolean
91 92 93 94 95 96 97 |
# File 'lib/connect_wise_web_reports/report.rb', line 91 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 ###
82 83 84 85 86 87 88 89 |
# File 'lib/connect_wise_web_reports/report.rb', line 82 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
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_page ⇒ Object
110 111 112 113 114 115 |
# File 'lib/connect_wise_web_reports/report.rb', line 110 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
106 107 108 |
# File 'lib/connect_wise_web_reports/report.rb', line 106 def previous_page? self.page > 1 end |