Class: DataCollector::Output

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/data_collector/output.rb,
lib/data_collector/output/rpc.rb,
lib/data_collector/output/generic.rb

Defined Under Namespace

Classes: Generic, Rpc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Output

Returns a new instance of Output.



23
24
25
26
# File 'lib/data_collector/output.rb', line 23

def initialize(data = {})
  @data = HashWithIndifferentAccess.new(data)
  @logger = Logger.new(STDOUT)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



21
22
23
# File 'lib/data_collector/output.rb', line 21

def data
  @data
end

Instance Method Details

#<<(input_data) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/data_collector/output.rb', line 65

def <<(input_data)
  if input_data.is_a?(Hash)
    input_data.each do |k, v|
      self[k] = input_data[k]
    end
  elsif input_data.is_a?(Array)
    @data["datap"] = [] unless @data.has_key?("datap")
    d = @data["datap"].flatten.compact
    d += input_data
    @data["datap"] = d.compact.flatten
  end
end

#[](k, v = nil) ⇒ Object



36
37
38
# File 'lib/data_collector/output.rb', line 36

def [](k, v = nil)
  @data[k]
end

#[]=(k, v = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/data_collector/output.rb', line 40

def []=(k, v = nil)
  unless v.nil?
    if @data.has_key?(k)
      if @data[k].is_a?(Array) then
        if v.is_a?(Array)
          @data[k] += v
        else
          @data[k] << v
        end
      else
        if v.is_a?(Array) # merge with array
          @data[k] = [@data[k]] + v
        else
          #@data[k] = [@data[k], v]
          @data[k] = v
        end
      end
    else
      @data[k] = v
    end
  end

  @data
end

#clearObject



111
112
113
114
115
# File 'lib/data_collector/output.rb', line 111

def clear
  @data = {}
  # GC.start(full_mark: true, immediate_sweep: true)
  GC.start
end

#crushObject



106
107
108
109
# File 'lib/data_collector/output.rb', line 106

def crush
  data = @data
  @data = deep_compact(data)
end

#each(&block) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/data_collector/output.rb', line 28

def each(&block)
  if block_given?
    @data.each(&block) if @data
  else
    to_enum(:each)
  end
end

#flattenObject



98
99
100
101
102
103
104
# File 'lib/data_collector/output.rb', line 98

def flatten()
  out = Hash.new
  @data.each do |m|
    out[m[0]] = m[1]
  end
  out
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/data_collector/output.rb', line 82

def has_key?(key)
  @data.key?(key)
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/data_collector/output.rb', line 86

def include?(key)
  @data.key?(key)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/data_collector/output.rb', line 78

def key?(key)
  @data.key?(key)
end

#keysObject



90
91
92
# File 'lib/data_collector/output.rb', line 90

def keys
  @data.keys
end

#rawObject



94
95
96
# File 'lib/data_collector/output.rb', line 94

def raw
  @data
end

#to_json(options = {}) ⇒ Object



247
248
249
250
251
252
253
254
255
# File 'lib/data_collector/output.rb', line 247

def to_json(options = {})
  if options.key?(:template)
    result = to_s(options[:template])
  else
    result = @data
  end

  result.to_json
end

#to_jsonfile(jsondata, jsonfile) ⇒ Object



188
189
190
191
192
# File 'lib/data_collector/output.rb', line 188

def to_jsonfile(jsondata, jsonfile)
  raise '[DEPRECATED] `to_jsonfile` deprecated. Please use `to_uri("file://abc.json", {template: "template.erb", content_type: "application/json"})` instead'
rescue Exception => e
  raise "unable to save to jsonfile: #{e.message}"
end

#to_queue(uri, options = {}) ⇒ Object



261
262
263
# File 'lib/data_collector/output.rb', line 261

def to_queue(uri, options = {})
  raise "to be implemented"
end

#to_rpc(uri, options = {}) ⇒ Object



257
258
259
# File 'lib/data_collector/output.rb', line 257

def to_rpc(uri, options = {})
  DataCollector::Output::Rpc.new(uri, options)
end

#to_s(erb_file = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/data_collector/output.rb', line 117

def to_s(erb_file = nil)
  data = @data

  return data.to_s if erb_file.nil?

  def print(data, symbol, to_symbol = nil)
    tag = to_symbol ? to_symbol.to_s : symbol.to_s

    if data.with_indifferent_access[symbol]
      if data.with_indifferent_access[symbol].is_a?(Array)
        r = []
        data.with_indifferent_access[symbol].each do |d|
          r << "<#{tag}>#{CGI.escapeHTML(d.to_s)}</#{tag}>"
        end
        r.join("\n")
      elsif data.with_indifferent_access[symbol].is_a?(Hash)
        r = []
        r << "<#{tag}>"
        data.with_indifferent_access[symbol].keys.each do |k|
          r << print(data.with_indifferent_access[symbol], k)
        end
        r << "</#{tag}>"
        r.join("\n")
      else
        "<#{tag}>#{CGI.escapeHTML(data.with_indifferent_access[symbol].to_s)}</#{tag}>"
      end
    else
      nil
    end
  rescue Exception => e
    @logger.error("unable to print data '#{symbol}'")
  end

  def no_tag_print(data, symbol)
    if data.with_indifferent_access[symbol]
      if data.with_indifferent_access[symbol].is_a?(Array)
        r = []
        data.with_indifferent_access[symbol].each do |d|
          r << "#{CGI.escapeHTML(d.to_s)}"
        end
        r.join(",\n")
      else
        "#{CGI.escapeHTML(data.with_indifferent_access[symbol].to_s)}"
      end
    else
      nil
    end
  rescue Exception => e
    @logger.error("unable to print (without tag) data '#{symbol}'")
  end

  data[:response_date] = DateTime.now.xmlschema

  #ERB.new(File.read(erb_file), 0, '>').result(binding)
  ERB.new(File.read(erb_file),  trim_mode: '-').result(binding)
rescue Exception => e
  raise "unable to transform to text: #{e.message}"
end

#to_tar_file(erb_file, tar_file_name = nil) ⇒ Object



182
183
184
185
186
# File 'lib/data_collector/output.rb', line 182

def to_tar_file(erb_file, tar_file_name = nil)
  raise '[DEPRECATED] `to_tar_file` deprecated. Please use `to_uri("file://abc.xml", {content_type: "application/xml", tar: true})` instead'
rescue Exception => e
  raise "unable to save to file: #{e.message}"
end

#to_tmp_file(erb_file, records_dir) ⇒ Object



176
177
178
179
180
# File 'lib/data_collector/output.rb', line 176

def to_tmp_file(erb_file, records_dir)
  raise '[DEPRECATED] `to_tmp_file` deprecated. Please use `to_uri("file://abc.xml", {template: "template.erb", content_type: "application/xml"})` instead'
rescue Exception => e
  raise "unable to save to file: #{e.message}"
end

#to_uri(destination, options = {}) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/data_collector/output.rb', line 194

def to_uri(destination, options = {})
  destination = CGI.unescapeHTML(destination)
  @logger.info("writing #{destination}")
  uri = URI(destination)
  begin
    data = nil
    case uri.scheme
    when 'http'
      data = to_http(uri, options)
    when 'https'
      data = to_https(uri, options)
    when 'file'
      data = to_file(uri, options)
    when /amqp/
      if uri.scheme =~ /^rpc/
        data = to_rpc(uri, options)
      else
        data = to_queue(uri, options)
      end
    else
      raise "Do not know how to process #{source}"
    end

    data = data.nil? ? 'no data found' : data

    if block_given?
      yield data
    else
      data
    end
  rescue => e
    @logger.info(e.message)
    puts e.backtrace.join("\n")
    nil
  end
end

#to_xml(options = {}) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/data_collector/output.rb', line 231

def to_xml(options = {})
  if options.key?(:template)
    result = to_s(options[:template])
    xml_result = Nokogiri::XML(result, nil, 'UTF-8') do |config|
      config.noblanks
    end
  else
    xml_root = options[:root] || 'data'
    xml_result = Nokogiri::XML(@data.to_xml(root: xml_root), nil, 'UTF-8') do |config|
      config.noblanks
    end
  end

  xml_result.to_s
end