Class: SourceCodesController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- SourceCodesController
- Defined in:
- app/controllers/source_codes_controller.rb
Instance Method Summary collapse
- #check ⇒ Object
- #create ⇒ Object
- #download ⇒ Object
- #index ⇒ Object
- #load ⇒ Object
- #load_demo ⇒ Object
- #load_local ⇒ Object
- #run ⇒ Object
- #to_blocks ⇒ Object
- #write ⇒ Object
Instance Method Details
#check ⇒ Object
49 50 51 |
# File 'app/controllers/source_codes_controller.rb', line 49 def check render json: SourceCode.new(source_code_params).check_syntax end |
#create ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'app/controllers/source_codes_controller.rb', line 53 def create sc = SourceCode.create!(source_code_params) session[:source_code] = { id: sc.id, digest: sc.digest, } render json: { source_code: { id: sc.id } } end |
#download ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'app/controllers/source_codes_controller.rb', line 62 def download send_data(source_code.data, filename: url_encode_filename(source_code.filename), disposition: 'attachment', type: 'text/plain; charset=utf-8') destroy_source_code_and_delete_session(source_code) end |
#index ⇒ Object
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 |
# File 'app/controllers/source_codes_controller.rb', line 7 def index res = { localPrograms: [], demoPrograms: [], } if standalone? local_program_paths.each do |path| # TODO: XMLからタイトルを抽出する # TODO: XMLからキャラクターの画像を抽出する filename = rb_basename(path) res[:localPrograms] << { title: filename, filename: filename, } end end # TODO: XMLから情報を抽出する res[:demoPrograms] << { title: '車のおいかけっこ', filename: 'car_chase.rb', imageUrl: '/smalruby/assets/car2.png', } res[:demoPrograms] << { title: 'クリックスターだにゃ~', filename: 'star.rb', imageUrl: '/smalruby/assets/cat1.png', } res[:demoPrograms] << { title: 'ピンポンゲーム', filename: 'pong.rb', imageUrl: '/smalruby/assets/cat2.png', } res[:demoPrograms] << { title: 'ライトをぴかっとさせるでよ', filename: 'hardware_led.rb', imageUrl: '/smalruby/assets/frog1.png', } render json: res end |
#load ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/controllers/source_codes_controller.rb', line 84 def load f = params[:source_code][:file] info = get_file_info(f) if /\Atext\/plain/ =~ info[:type] info[:data] = NKF.nkf('-w', f.read) else info[:error] = 'Rubyのプログラムではありません' end render json: { source_code: info }, content_type: request.format end |
#load_demo ⇒ Object
104 105 106 107 108 109 110 |
# File 'app/controllers/source_codes_controller.rb', line 104 def load_demo filename = source_code_params[:filename] program_path = demo_program_paths.find { |path| rb_basename(path) == filename } load_local_file(program_path, source_code_params[:remix]) end |
#load_local ⇒ Object
96 97 98 99 100 101 102 |
# File 'app/controllers/source_codes_controller.rb', line 96 def load_local filename = source_code_params[:filename] program_path = local_program_paths.find { |path| rb_basename(path) == filename } load_local_file(program_path, source_code_params[:remix]) end |
#run ⇒ Object
112 113 114 115 116 117 118 119 120 121 |
# File 'app/controllers/source_codes_controller.rb', line 112 def run source_code = SourceCode.new(source_code_params) if session[:username] s = "~/#{session[:username]}/#{source_code.filename}" else s = "~/#{source_code.filename}" end path = Pathname(s). render json: source_code.run(path) end |
#to_blocks ⇒ Object
123 124 125 126 127 128 129 130 131 132 |
# File 'app/controllers/source_codes_controller.rb', line 123 def to_blocks source_code = SourceCode.new(source_code_params) render text: source_code.to_blocks rescue if Rails.env == 'development' raise else head :bad_request end end |
#write ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/controllers/source_codes_controller.rb', line 71 def write res = { source_code: { filename: source_code.filename } } write_source_code(source_code) destroy_source_code_and_delete_session(source_code) render json: res rescue => e res[:source_code][:error] = e. render json: res end |