Class: Mushy::WriteFile

Inherits:
Flux
  • Object
show all
Defined in:
lib/mushy/fluxs/write_file.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



5
6
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
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mushy/fluxs/write_file.rb', line 5

def self.details
  {
    name: 'WriteFile',
    title: 'Write File',
    description: 'Write a file.',
    fluxGroup: { name: 'Files' },
    config: file_saving_config.merge({
              data: {
                      description: 'The text to write. You can use Liquid templating here to pull data from the event, or write hardcoded data.',
                      type:        'text',
                      value:       '{{data}}',
                    },
            }),
    examples: {
      "Example" => {
                     description: 'Using this Flux to write the contents of a text file. Details about the file are returned.',
                     input: {
                              file: "data.csv",
                              content: "a,b,c\nd,e,f",
                            },
                     config: {
                               file: '{{file}}',
                               data: '{{content}}'
                             },
                     result: {
                               file: {
                                 inode: "439540",
                                 hard_links: 1,
                                 owner: "pi",
                                 group: "pi",
                                 size: 3,
                                 date: {
                                   year: 2021,
                                   month: 10,
                                   day: 7,
                                   hour: 15,
                                   minute: 41,
                                   second: 14,
                                   nanosecond: 163590058,
                                   utc_offset: -18000,
                                   weekday: 4,
                                   day_of_month: 7,
                                   day_of_year: 280,
                                   string: "2021-10-07 15:41:14 -0500",
                                   epoch_integer: 1633639274,
                                   epoch_float: 1633639274.1635902,
                                   seconds_ago: 0.018665617
                                },
                                 name: "file.csv",
                                 type: "-",
                                 owner_permission: "rw-",
                                 group_permission: "r--",
                                 other_permission: "r--",
                                 directory: "/home/pi/Desktop/mushy",
                                 path: "/home/pi/Desktop/mushy/file.csv"
                              }
                            }
                   },
    }
  }
end

.file_saving_configObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mushy/fluxs/write_file.rb', line 67

def self.file_saving_config
  {
    name: {
            description: 'The name of the file.',
            type:        'text',
            value:       'file.csv',
          },
    directory: {
                 description: 'The directory in which to write the file. Leave blank for the current directory.',
                 shrink:      true,
                 type:        'text',
                 value:       '',
               },
    }
end

.get_file_from(config) ⇒ Object



83
84
85
86
87
# File 'lib/mushy/fluxs/write_file.rb', line 83

def self.get_file_from config
  file = config[:name]
  file = File.join(config[:directory], file) if config[:directory].to_s != ''
  file
end

Instance Method Details

#process(event, config) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/mushy/fluxs/write_file.rb', line 89

def process event, config
  file = self.class.get_file_from config

  File.open(file, 'w') { |f| f.write config[:data] }

  {
    file: Mushy::Ls.new.process({}, { path: file })[0]
  }
end