📜 ⬆️ ⬇️

Simple ruby ​​tests for web

To study ruby, an educational project was chosen in which a simple task, writing tests for the web. Due to lack of knowledge and experience, it was decided to use the simplified sinatra framework. As a result, there are only two files in my solution to this problem. The first is mutaAlim.json, in which we have a database in json format.

Consider the structure of this file on the example of several questions:

[ { "id" : 1, "question" : " !", "answers" : [" ", "", "", ""] }, { "id" : 2, "question" : " ?", "answers" : ["", "", "", ""] }, …..(     ) ] 


We have this way in json array of hash elements.
The answers in the array are key answers, in which the first answer is correct.
')
The second file is mutaAlim.rb in which the application code.

Consider it closer. The first lines include the gems sinatra and json:

 require 'sinatra' require 'json' 


To go to the welcome page the following code

 get '/' do erb :index end 


When you go to the address , we will have a page that is described in the same file:

 @@index <!DOCTYPE html> <html> <head> <title></title> </head> <body> <h1>  I  hh  h!</h1> <h1>  ,    !</h1> <h1>     </h1> <h2> </h2> <div> <p>          . </p> </div> <input value=" " name="test_begin" onclick="location.href='/test'" type="button"> </body> </html> 


Simple page with a button to go to the test page. Further in the code we have variables necessary for the application to work:

 #   @@test_hash #  @@kol_testov = 13 #      @@mas_test_number = Array.new(@@kol_testov) #  , -1         @@mas_test_answer #    test_file = File.open('mutaAlim.json',"rb") @@test_hash = JSON.parse(test_file.read) test_file.close #   @@not_answer = "   " #  @@answers #   @@ball #   @@procent_tru_answers #    


As you noticed, I had to declare all variables in the class, otherwise there were errors on the output in erb, which I could not understand and correct. Further, the main element of the application is the code of the test page:

 #   get '/test' do if params[:var] == nil @@mas_test_answer = Array.new(@@kol_testov) { |i| -1} #      @@mas_test_number @@mas_test_number[0] = Random.rand(@@test_hash.length) @@mas_test_number.each_index do |i| begin flag = true buf = Random.rand(@@test_hash.length) 0..i.times do |j| if @@mas_test_number[j]==buf flag = false end end if flag @@mas_test_number[i] = buf end end until flag==true end @@test_number = 0; #       @@mas_random_variants @@mas_random_variants = Array.new(@@kol_testov) @@mas_random_variants.each_index do |i| @@mas_random_variants[i] = Array.new(@@test_hash[@@mas_test_number[i]]["answers"].size) end @@mas_random_variants.each_index do |i| @@mas_random_variants[i][0] = Random.rand(@@test_hash[@@mas_test_number[i]]["answers"].size) @@mas_random_variants[i].each_index do |j| begin flag = true buf = Random.rand(@@test_hash[@@mas_test_number[i]]["answers"].size) 0..j.times do |k| if @@mas_random_variants[i][k]==buf flag = false end end if flag @@mas_random_variants[i][j] = buf end end until flag==true end end else #      @@mas_test_answer @@mas_test_answer[@@test_number] = params[:var].to_i #     if @@test_number != @@kol_testov - 1 @@test_number += 1 end end erb :test end 


By the condition params[:var] == nil we determine that the user is on the page for the first time and “prepare” everything necessary (randomly select tests from the database, randomly “mix” response options and create an array to store the answers), otherwise we write the answer chosen by the user in array The entire functionality of the application is built on get requests, and there is a lot of confusion due to the fact that it was written in the "just to work" mode. For example, if a user clicks on the link http: // localhost: 4567 / test? Var = 0, then in the array of answers to the current question, the value will be set to 0.

Consider the test page code

 @@test <!DOCTYPE html> <html> <head> <title></title> </head> <body> <div> <h2><%= @@test_number +1 %> <h2> <h3> <%= @@test_hash[@@mas_test_number[@@test_number]]["question"] %> </h3> </div> <p> <form method="get" action="/test"> <% @@test_hash[@@mas_test_number[@@test_number]]["answers"].each_index do |i| %> <input type="radio" name="var" value="<%= @@mas_random_variants[@@test_number][i] %>"> <%= @@test_hash[@@mas_test_number[@@test_number]]["answers"][@@mas_random_variants[@@test_number][i]] %> <% end %> <input type="submit" value=""/> </form> </p> <div> <input type="button" name="next_question" value=" " onclick="location.href='/test/pred'"> <% @@mas_test_number.each_index do |i| %> <a href='/test/question/<%= i + 1 %>'><%= i + 1 %> </a> <% end %> <input type="button" name="pred_question" value=" " onclick="location.href='/test/next'"> </div> <input type="button" name="result" value=" " onclick="location.href='/test/result'"> </body> </html> 


As you have noticed, inserting the code on the page is done with erb as follows <%= # %> and <% %> . On the page itself is a question, answer options (radiobutton), links to all questions. test navigation buttons and a test completion button that directs the user to the test results page.

The operation of the navigation buttons and links to the questions:

 #     get '/test/question/:id' do @@test_number = params[:id].to_i - 1 erb :test end #   get '/test/next' do if @@test_number != @@kol_testov - 1 @@test_number += 1 end erb :test end #   get '/test/pred' do if @@test_number != 0 @@test_number -= 1 end erb :test end 


It remains to analyze the code of the test results page that calculates the results.

 #   get '/test/result' do @@ball = 0 @@answers = 0 #   @@mas_test_answer.each do |i| if i == 0 @@ball += 1 end if i != -1 @@answers += 1 end end @@procent_tru_answers = (100/@@kol_testov) * @@ball erb :result end 

And the code in html + erb

 @@result <!DOCTYPE html> <html> <head> <title></title> </head> <body> <h1>    </h1> <div> <table cellspacing="2" border="1" cellpadding="5"> <tr> <td> № </td> <td>  </td> <td>   </td> <td>   </td> <tr> <% @@mas_test_answer.each_index do |i| %> <tr> <td> <%= i+1 %> </td> <td> <%= @@test_hash[@@mas_test_number[i]]["question"] %> </td> <td> <%= if (@@mas_test_answer[i] == -1) @@not_answer else @@test_hash[@@mas_test_number[i]]["answers"][@@mas_test_answer[i]] end %> </td> <td> <%= @@test_hash[@@mas_test_number[i]]["answers"][0] %> </td> <tr> <% end %> </table> </div> <div> <h3>   = <%= @@kol_testov %> </h3> <h3>   = <%= @@answers %> </h3> <h3>    = <%= @@ball %> </h3> <h3>    = <%= @@procent_tru_answers %> %</h3> <div> <div> <p>          . </p> </div> <input value=" " name="test_begin" onclick="location.href='/test'" type="button"> </body> </html> 


I understand that this application is easier to write from scratch and correctly, rather than understand or accompany it. I started to learn ruby ​​language on this application exactly as a sinatra engine, erb markup language and json format, so please do not judge strictly for what I used and how.
In the future, it is planned to transfer from sinatra to ror, from json to a full database, from erb to haml, from single-type questions (radiobutton) to questions 5-6 types, from tests to the game and plus design with layout.

The application in the working form can be seen on the site .
The githaba repository.

Source: https://habr.com/ru/post/226869/


All Articles