Class: YamWow::Response
- Inherits:
-
Object
- Object
- YamWow::Response
- Defined in:
- lib/yamwow/response.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#reference_data ⇒ Object
Returns the value of attribute reference_data.
Instance Method Summary collapse
-
#initialize(data, reference_data = nil) ⇒ Response
constructor
A new instance of Response.
- #parse_template(template) ⇒ Object
- #to_csv(options = {}) ⇒ Object
Constructor Details
#initialize(data, reference_data = nil) ⇒ Response
Returns a new instance of Response.
9 10 11 12 |
# File 'lib/yamwow/response.rb', line 9 def initialize(data, reference_data=nil) @data = data @reference_data = reference_data end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
7 8 9 |
# File 'lib/yamwow/response.rb', line 7 def data @data end |
#reference_data ⇒ Object
Returns the value of attribute reference_data.
7 8 9 |
# File 'lib/yamwow/response.rb', line 7 def reference_data @reference_data end |
Instance Method Details
#parse_template(template) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/yamwow/response.rb', line 33 def parse_template(template) keys = template.scan(/(\{(.+?)\})/m) results = flatten_data.map do |h| t = "#{template}" keys.each { |k| t.gsub! k[0], h[k[1]].to_s.gsub("\n", ' ') } t end results.join end |
#to_csv(options = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/yamwow/response.rb', line 14 def to_csv(={}) hashes = flatten_data columns = [:include] unless columns keys = Set.new hashes.each { |h| keys += h.keys } columns = keys.sort end exclude_columns = [:exclude] || [] columns = columns.reject { |c1| exclude_columns.index { |c2| c1 =~ c2 } } CSV.generate do |csv| csv << columns hashes.each { |h| csv << columns.map { |k| h[k] } } end end |