-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
197 lines (158 loc) · 6.31 KB
/
app.rb
File metadata and controls
197 lines (158 loc) · 6.31 KB
1
2
3
4
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
require 'sinatra'
require 'json'
require 'csv'
require 'rubyXL'
require 'rubyXL/convenience_methods'
require 'redis'
require 'redis-rack'
REDIS = Redis.new(url: "redis://localhost:6379/0")
use Rack::Session::Redis,
redis_server: "redis://localhost:6379/0",
expire_after: 3600,
key: 'my_sinatra_session'
set :bind, '0.0.0.0'
def file_path # defines the file path using built-in ruby FILE object
File.join(settings.root, 'size_run.json')
end
def read_data # reads the file path using JSON
JSON.parse(File.read(file_path))
end
def write_data(data) # whatever data is thrown into the funnel is being written to the file using the FILE object
File.write(file_path, JSON.pretty_generate(data))
end
get '/' do
message = session.delete(:message) # Retrieve and clear message
upload_success = session.delete(:upload_success) || false # Retrieve and reset upload status
erb :index, locals: { message: message, upload_success: upload_success }
end
post '/upload' do
puts "ENTERING :UPLOAD"
puts "DEBUG: RECEIVED PARAMS - #{params.inspect}"
puts "DEBUG: REQUEST CONTENT TYPE - #{request.content_type}"
unless params[:file]
session[:message] = "No file selected for upload"
session[:upload_success] = false
redirect '/'
end
file = params[:file][:tempfile]
filename = params[:file][:filename].gsub("\0", "").encode('UTF-8', invalid: :replace, undef: :replace, replace: '_')
# file_extension = File.extname(params[:file][:filename]).downcase
REDIS.set("uploaded_file", file.read, ex: 3600)
REDIS.set("uploaded_filename", filename)
uploaded_data = []
puts "Uploaded Data Sample: #{uploaded_data.first.inspect}" if uploaded_data.any?
begin
workbook = RubyXL::Parser.parse(file)
worksheet = workbook[0]
worksheet.each_with_index do |row, index|
next if index == 0 # skips first row if it contains headers
next if row.nil? # skips empty rows
item_code = row[0]&.value
product_name = row[5]&.value # added for testing
color_name = row[6]&.value # added for testing
size_card = row[11]&.value
next if item_code.nil? || size_card.nil? || product_name.nil? || color_name.nil?
uploaded_data << { 'item code' => item_code, 'product name' => product_name, 'color name' => color_name, 'size_card' => size_card } # iterates through the worksheet and assigns data from columns to uploaded_data
end
rescue StandardError => e
session[:message] = "Error processing file: #{e.message}"
session[:upload_success] = false
redirect '/'
end
session[:uploaded_data] = uploaded_data.to_json
data = read_data # reads size_run.json
uploaded_data.each do |row|
size_card = row['size_card']
data['size_cards'][size_card] ||= []
end
write_data(data)
session[:message] = "File uploaded successfully. Size cards and SKUs updated."
session[:upload_success] = true
redirect '/'
end
post '/export' do
puts "ENTERING EXPORT"
puts "DEBUG: RECEIVED PARAMS - #{params.inspect}"
file_data = REDIS.get("uploaded_file")
temp_file = Tempfile.new(["exported_", ".xlsx"])
temp_file.binmode
temp_file.write(file_data)
temp_file.rewind
data = read_data # Read stored size mappings from size_run.json
workbook = RubyXL::Parser.parse(temp_file.path)
worksheet = workbook.add_worksheet("BARCODES")
headers = ["SKU", "Product", "Color", "Size", "Variant Position", "Color Code", "Barcode", "Quantity", "Comments"]
headers.each_with_index do |header, col_index|
worksheet.add_cell(0, col_index, header)
worksheet.change_row_horizontal_alignment(0, 'center')
worksheet.change_column_width(0, 15) # sku
worksheet.change_column_width(1, 25) # products
worksheet.change_column_width(2, 25) # color name
worksheet.change_column_width(3, 10) # sizes
worksheet.change_column_width(4, 15) # variant positioning
worksheet.change_column_width(5, 15) # color codes
worksheet.change_column_width(6, 25) # product barcodes
worksheet.change_column_width(7, 10) # quantities
worksheet.change_column_width(8, 25) # comments
worksheet.change_column_horizontal_alignment(4, 'center')
worksheet.change_column_horizontal_alignment(5, 'center')
worksheet.change_column_horizontal_alignment(8, 'center')
end
index = 1 # Start at row 1 for data
uploaded_data = JSON.parse(session[:uploaded_data] || "[]")
puts "Uploaded Data Sample: #{uploaded_data.first.inspect}" if uploaded_data.any?
sku_groups = uploaded_data.group_by { |row| row['item code'] }
sku_groups.each_with_index do |(item_code, rows), group_index|
next if item_code.nil?
order = 1 #Resets order for each sku
color_code = item_code[-4..] || "N/A"
use_grey = group_index.even?
rows.each do |row|
size_card = row['size_card']
product_name = row['product name']
color_name = row['color name']
sizes = data["size_cards"][size_card] || []
if sizes.empty?
worksheet.add_cell(index, 0, item_code)
worksheet.add_cell(index, 1, product_name)
worksheet.add_cell(index, 2, color_name)
worksheet.add_cell(index, 3, size_card)
worksheet.add_cell(index, 4, "No Sizes found")
worksheet.add_cell(index, 5, order)
worksheet.add_cell(index, 6, color_code)
if use_grey
(0..8).each do |col|
worksheet[index][col]&.change_fill('d9d9d9')
end
end
index += 1
else
sizes.each do |size|
product_code = "#{item_code}-(#{size})"
worksheet.add_cell(index, 0, item_code)
worksheet.add_cell(index, 1, product_name)
worksheet.add_cell(index, 2, color_name)
worksheet.add_cell(index, 3, size)
worksheet.add_cell(index, 4, order)
worksheet.add_cell(index, 5, color_code)
worksheet.add_cell(index, 6, product_code)
if use_grey
(0..8).each do |col|
worksheet[index][col]&.change_fill('d9d9d9')
end
end
order += 1
index += 1
end
end
end
end
Dir.mkdir('public') unless Dir.exist?('public')
file_path = "./public/exported_skus.xlsx"
workbook.write(file_path)
send_file file_path, filename: "exported_skus.xlsx", type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
attachment "exported_file.xlsx"
ensure
temp_file.close
temp_file.unlink
end