Class: Orefine::CSVUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/orefine.rb

Class Method Summary collapse

Class Method Details

.add_column(csv, field, value) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/orefine.rb', line 186

def add_column(csv, field, value)
  self.perform_operation(csv, %Q{
  [
    {
"op": "core/column-addition",
"engineConfig": {
  "facets": [],
  "mode": "record-based"
},
"newColumnName": "#{field}",
"columnInsertIndex": 0,
"baseColumnName": "email_stripped",
"expression": "grel:\\\"#{value}\\\"",
"onError": "set-to-blank"
    }
  ]
  })
end

.clear_all_csvsObject



7
8
9
10
11
12
13
# File 'lib/orefine.rb', line 7

def clear_all_csvs
  Refine.["projects"]
        .select { |k, v| v["name"].start_with?('csv_') }
        .keys
        .map { |project_id| Refine.new("project_id" => project_id) }
        .map(&:delete_project)
end

.common_facet(flag = true) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/orefine.rb', line 205

def common_facet(flag = true)
  {
    "invert" =>  false,
    "expression" =>  "value",
    "selectError" =>  false,
    "omitError" =>  false,
    "selectBlank" =>  false,
    "name" =>  "exists",
    "omitBlank" =>  false,
    "columnName" =>  "exists",
    "type" =>  "list",
    "selection" =>  [
      {
        "v" =>  {
          # string vs boolean matters here... be careful
          "v" =>  flag,
          "l" =>  flag,
        }
      }
    ]
  }
end

.create_common_flag(project_a, project_b) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/orefine.rb', line 128

def create_common_flag(project_a, project_b)
  if project_a.get_columns_info.map { |c| c["name"] }.include? 'exists'
    STDERR.puts "'exists' column already exists in csv_a, deleting"
    self.delete_column(project_a, "exists")
  end

  self.perform_operation(project_a, %Q{
  [
    {
"op": "core/column-addition",
"engineConfig": {
  "facets": [],
  "mode": "record-based"
},
"newColumnName": "exists",
"columnInsertIndex": 0,
"baseColumnName": "email_stripped",
"expression": "grel:cell.cross(\\\"#{project_b.project_name}\\\", \\\"email_stripped\\\").cells.length() > 0",
"onError": "set-to-blank"
    }
  ]
  })
end

.delete_column(csv, field) ⇒ Object



175
176
177
178
179
180
181
182
183
184
# File 'lib/orefine.rb', line 175

def delete_column(csv, field)
  self.perform_operation(csv, %Q{
  [
    {
"op": "core/column-removal",
"columnName": "#{field}"
    }
  ]
  })
end

.merge_common_field(csv_a, csv_b, common_field) ⇒ Object



171
172
173
# File 'lib/orefine.rb', line 171

def merge_common_field(csv_a, csv_b, common_field)
  
end

.merge_field(project_a, project_b, field) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/orefine.rb', line 152

def merge_field(project_a, project_b, field)
  self.perform_operation(project_a, %Q{
  [
    {
"op": "core/column-addition",
"engineConfig": {
  "facets": [],
  "mode": "record-based"
},
"newColumnName": "#{field}_merged",
"columnInsertIndex": 0,
"baseColumnName": "email_stripped",
"expression": "grel:cell.cross(\\\"#{project_b.project_name}\\\", \\\"email_stripped\\\").cells[\\\"#{field}\\\"].value[0]",
"onError": "set-to-blank"
    }
  ]
  })
end

.normalize_column_names(projects) ⇒ Object



15
16
17
18
19
# File 'lib/orefine.rb', line 15

def normalize_column_names(projects)
  self.normalize_email_column_name(projects)
  self.normalize_zip_column_name(projects)
  self.normalize_full_name_column_name(projects)
end

.normalize_email_column_content(projects) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/orefine.rb', line 87

def normalize_email_column_content(projects)
  self.perform_operation(projects, %q{
  [
    {
"op": "core/column-addition",
"engineConfig": {
  "facets": [],
  "mode": "record-based"
},
"newColumnName": "email_stripped",
"columnInsertIndex": 0,
"baseColumnName": "email",
"expression": "grel:strip(value.toLowercase())",
"onError": "set-to-blank"
    }
  ]
  })
end

.normalize_email_column_name(projects) ⇒ Object



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
# File 'lib/orefine.rb', line 21

def normalize_email_column_name(projects)
  self.perform_operation(projects, %q{
  [
    {
"op": "core/column-rename",
"oldColumnName": "E-mail",
"newColumnName": "email"
    },
    {
"op": "core/column-rename",
"oldColumnName": "Email Address",
"newColumnName": "email"
    },
    {
"op": "core/column-rename",
"oldColumnName": "Email",
"newColumnName": "email"
    },
    {
"op": "core/column-rename",
"oldColumnName": "[email]",
"newColumnName": "email"
    },
    {
"op": "core/column-rename",
"oldColumnName": "email_stripped",
"newColumnName": "email"
    },  
  ]
  })
end

.normalize_full_name_column_name(projects) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/orefine.rb', line 70

def normalize_full_name_column_name(projects)
  self.perform_operation(projects, %q{
  [
    {
"op": "core/column-rename",
"oldColumnName": "Name",
"newColumnName": "full_name"
    },
    {
"op": "core/column-rename",
"oldColumnName": "Full Name",
"newColumnName": "full_name"
    }
  ]
  })
end

.normalize_zip_column_name(projects) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/orefine.rb', line 53

def normalize_zip_column_name(projects)
  self.perform_operation(projects, %q{
  [
    {
"op": "core/column-rename",
"oldColumnName": "Zip",
"newColumnName": "zip"
    },
    {
"op": "core/column-rename",
"oldColumnName": "[zip]",
"newColumnName": "zip"
    }
  ]
  })
end

.perform_operation(projects, operation) ⇒ Object



228
229
230
231
# File 'lib/orefine.rb', line 228

def perform_operation(projects, operation)
  projects = [projects] if !projects.is_a?(Array)
  projects.each { |p| p.apply_operations(operation) }
end

.split_full_name(projects) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/orefine.rb', line 106

def split_full_name(projects)
  self.perform_operation(projects, %q{
  [
    {
"op": "core/column-split",
"description": "Split column Name by separator",
"engineConfig": {
  "facets": [],
  "mode": "row-based"
},
"columnName": "full_name",
"guessCellType": false,
"removeOriginalColumn": false,
"mode": "separator",
"separator": "(?<=[a-z]) ",
"regex": true,
"maxColumns": 2
    }
  ]
  })
end