Class: Webservice::ResponseHandler::Tabular

Inherits:
Object
  • Object
show all
Defined in:
lib/webservice/base/response_handler.rb

Overview

Tabular helper/support class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, rows) ⇒ Tabular

Returns a new instance of Tabular.



200
201
202
203
# File 'lib/webservice/base/response_handler.rb', line 200

def initialize( headers, rows )
  @headers = headers
  @rows    = rows
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



197
198
199
# File 'lib/webservice/base/response_handler.rb', line 197

def headers
  @headers
end

#rowsObject (readonly)

Returns the value of attribute rows.



198
199
200
# File 'lib/webservice/base/response_handler.rb', line 198

def rows
  @rows
end

Instance Method Details

#to_csv(opts = {}) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/webservice/base/response_handler.rb', line 205

def to_csv( opts={} )
   ## allow changing of column/value separator (col_sep) - why? why not?
   ##   :col_sep => "\t"
   ##   :col_sep => ";"

   pp self

   CSV.generate( headers: true ) do |csv|
    csv << headers
    rows.each do |row|
      csv << row
    end
  end
end

#to_html_table(opts = {}) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/webservice/base/response_handler.rb', line 221

def to_html_table( opts={} )

  ## todo/fix: html escape values - why? why not??

  pp self

  buf = ""
  buf << "<table>\n"
  buf << "  <tr>"
  headers.each do |header|
    buf << "<th>#{header}</th>"
  end
  buf << "</tr>\n"

  rows.each do |row|
    buf << "  <tr>"
    row.each do |value|
      buf << "<td>#{value}</td>"
    end
    buf << "</tr>\n"
  end
  buf << "</table>\n"
  buf
end