This repository has been archived by the owner on Mar 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.rb
116 lines (101 loc) · 3.23 KB
/
root.rb
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
require 'sinatra'
require 'json'
require_relative 'csv_file'
require_relative 'auth'
my_csv = nil;
test_file_name = Dir.pwd + '/test_file.csv'
current_headers, current_table = nil, nil
get '/' do
"Hello! Let's <a href=""/auth"">authorizing</a> and start working!"
end
get '/auth' do
erb :'authorization'
end
post '/auth' do
puts params
a = Auth.new(params[:login], params[:password])
if !a.check_authorization
"Sorry, we don't know you:( <br> " +
"Maybe you are Ilya?) If that, <a href='/auth'>try again</a> with your name as login, password is your initials in lowercase:P"
else
my_csv = CSV_file.new(test_file_name, ';')
current_headers, current_table = my_csv.get_file
redirect '/what_to_do'
end
end
get '/what_to_do' do
"<strong>Now you can get some JSON data: </strong><br><br>" +
"1. To open some csv with ; separator go to /open_file?path=your path <br>" +
"2. To get some column put /get_column_json?column_name=your column name or index <br>" +
"3. To get some row put /get_row_json?row_number=your row_number <br>" +
"4. To get range put /get_range_json?row1={range left corner row_number};col1={range left corner col_number};row2={range right corner row_number};col2={range right corner col_number}; <br><br>" +
"or go to <a href=""/csv_table_view"">csv table view</a>.."
end
get '/open_file' do
if params.has_key?('path')
my_csv = CSV_file.new(params[:path], ';')
'Opened failed, sorry:(' if my_csv == nil
"Opened file #{params[:path]} success" if my_csv != nil
my_csv.get_file
else
"No 'path' parameter. Puts path=<path to file> to the get query"
end
end
get '/get_column_json' do
if params.has_key?('column_name')
puts params[:column_name]
my_csv.get_column(params[:column_name])
else
"No 'column_name' parameter. Puts column_name=<column name or index> to the get query"
end
end
get '/get_row_json' do
if params.has_key?('row_number')
puts params[:row_number]
my_csv.get_row(params[:row_number])
else
"No 'row_number' parameter. Puts row_number=<row_number> to the get query"
end
end
get '/get_range_json' do
puts params
if params.has_key?('row1') and
params.has_key?('col1') and
params.has_key?('row2') and
params.has_key?('col2')
my_csv.get_range(params[:row1], params[:col1], params[:row2], params[:col2])
else
"Something wrong with parameters:( We need row1, col1, row2, col2 values in the get query"
end
end
get '/get_row' do
if params.count == 0
erb :'get_row'
else
current_headers, current_table = my_csv.get_row(params[:row_number])
redirect '/csv_table_view'
end
end
get '/get_column' do
if params.count == 0
erb :'get_column'
else
current_headers, current_table = my_csv.get_column(params[:column_name])
redirect '/csv_table_view'
end
end
get '/get_range' do
if params.count == 0
erb :'get_range'
else
puts params
current_headers, current_table = my_csv.get_range(params[:row1], params[:col1], params[:row2], params[:col2])
redirect '/csv_table_view'
end
end
get '/csv_table_view' do
if params[:type] == 'all'
current_headers, current_table = my_csv.get_file
end
erb :'csv_table_view', locals: {:csv_headers => current_headers, :csv_table => current_table}
end