Class: SourceCodesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/source_codes_controller.rb

Instance Method Summary collapse

Instance Method Details

#checkObject



30
31
32
# File 'app/controllers/source_codes_controller.rb', line 30

def check
  render json: SourceCode.new(source_code_params).check_syntax
end

#createObject



34
35
36
37
38
39
40
41
# File 'app/controllers/source_codes_controller.rb', line 34

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

#downloadObject



43
44
45
46
47
48
49
50
# File 'app/controllers/source_codes_controller.rb', line 43

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

#indexObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/source_codes_controller.rb', line 7

def index
  res = {
    localPrograms: [],
    demoPrograms: [],
  }

  select_and_get_summary = proc { |path|
    s = SourceCode.new(filename: path.basename.to_s, data: path.read)
    next if raspberrypi? && s.include_block?(/^(hardware_|pen_)/)
    s.summary
  }

  if standalone?
    res[:localPrograms] =
      local_program_paths.map(&select_and_get_summary).compact
  end

  res[:demoPrograms] =
    demo_program_paths.map(&select_and_get_summary).compact

  render json: res
end

#loadObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/source_codes_controller.rb', line 65

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] = I18n.t('.not_ruby_program',
                          scope: 'controllers.source_codes')
  end

  render json: { source_code: info }, content_type: request.format
end

#load_demoObject



86
87
88
89
90
91
92
# File 'app/controllers/source_codes_controller.rb', line 86

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_localObject



78
79
80
81
82
83
84
# File 'app/controllers/source_codes_controller.rb', line 78

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

#runObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/controllers/source_codes_controller.rb', line 94

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).expand_path
  env = {}
  if current_preferences["hardware_port"].present?
    env["SMALRUBOT_DEVICE"] = current_preferences["hardware_port"]
  end
  render json: source_code.run(path, env)
end

#to_blocksObject



109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/source_codes_controller.rb', line 109

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

#writeObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/source_codes_controller.rb', line 52

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.message
  render json: res
end