Class: Mushy::ReadCsv

Inherits:
Flux
  • Object
show all
Defined in:
lib/mushy/fluxs/read_csv.rb

Instance Attribute Summary

Attributes inherited from Flux

#config, #flow, #id, #masher, #parent_fluxs, #subscribed_to, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Flux

#convert_this_to_an_array, #convert_to_symbolized_hash, #execute, #execute_single_event, #group_these_results, #guard, #ignore_these_results, inherited, #initialize, #join_these_results, #limit_these_results, #merge_these_results, #model_these_results, #outgoing_split_these_results, #shape_these, #sort_these_results, #standardize_these

Constructor Details

This class inherits a constructor from Mushy::Flux

Class Method Details

.detailsObject



7
8
9
10
11
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
45
46
47
48
49
50
51
52
# File 'lib/mushy/fluxs/read_csv.rb', line 7

def self.details
  {
    name: 'ReadCsv',
    title: 'Read CSV',
    fluxGroup: { name: 'CSV' },
    description: 'Read CSV content into events.',
    config: {
      data: {
             description: 'The data to convert to a CSV.',
             type:        'text',
             value:       '{{data}}',
           },
      headers: {
                 description: 'The CSV contains headers. Defaults to false.',
                 type:        'boolean',
                 shrink:      true,
                 value:       '',
               },
    },
    examples: {
      "With Headers" => {
                          description: 'Using this Flux to read a CSV into many events.',
                          input: {
                                   csv: "first,last,height\njane,doe,short"
                                 },
                          config: {
                                    data: '{{csv}}'
                                  },
                          result: { "first": "jane", "last": "doe", "height": "short" }
                        },
      "No Headers" => {
                        description: 'Using this Flux to read a CSV into many events.',
                        input: {
                                 csv: "john,doe,tall\njane,doe,short"
                               },
                        config: {
                                  data: '{{csv}}'
                                },
                        result: [
                                  { "a": "john", "b": "doe", "c": "tall" },
                                  { "a": "jane", "b": "doe", "c": "short" }
                                ]
                      },
    }
  }
end

Instance Method Details

#process(event, config) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mushy/fluxs/read_csv.rb', line 54

def process event, config
  data = config[:data]

  headers = config[:headers].to_s.strip.downcase == 'true'

  rows = CSV.new data, headers: headers

  rows.map do |row|
    if headers
      row.to_hash
    else
      record = {}
      row.each_with_index { |r, i| record[("a".ord + i).chr] = r }
      record
    end
  end
end