Module: CodeRunner

Defined in:
lib/code-runner.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

SUPPORTED_LANGUAGES =
{
  'python' => 'python', 'py' => 'python',
  'javascript' => 'javascript', 'js' => 'javascript',
  'java' => 'java',
  'cpp' => 'cpp', 'c' => 'c',
  'csharp' => 'csharp', 'cs' => 'csharp',
  'ruby' => 'ruby', 'rb' => 'ruby',
  'php' => 'php',
  'go' => 'go',
  'rust' => 'rust', 'rs' => 'rust',
  'kotlin' => 'kotlin', 'kt' => 'kotlin',
  'swift' => 'swift',
  'typescript' => 'typescript', 'ts' => 'typescript',
  'bash' => 'bash', 'sh' => 'bash',
  'r' => 'r',
  'perl' => 'perl', 'pl' => 'perl',
  'lua' => 'lua',
  'haskell' => 'haskell', 'hs' => 'haskell',
  'elixir' => 'elixir', 'exs' => 'elixir',
  'clojure' => 'clojure', 'clj' => 'clojure',
  'dart' => 'dart',
  'assembly' => 'assembly', 'asm' => 'assembly',
  'fortran' => 'fortran', 'f90' => 'fortran',
  'julia' => 'julia', 'jl' => 'julia',
  'cobol' => 'cobol', 'cbl' => 'cobol',
  'vb' => 'vbnet', 'vba' => 'vbnet',
  'scala' => 'scala',
  'racket' => 'racket', 'rkt' => 'racket',
  'erlang' => 'erlang', 'erl' => 'erlang',
  'fsharp' => 'fsharp', 'fs' => 'fsharp',
  'groovy' => 'groovy', 'gvy' => 'groovy',
  'powershell' => 'powershell', 'ps1' => 'powershell',
  'sql' => 'sql'
}.freeze
API_URL =
"https://emkc.org/api/v2/piston/execute".freeze

Class Method Summary collapse

Class Method Details

.eval_code(code, language, options = {}) ⇒ Object



78
79
80
# File 'lib/code-runner.rb', line 78

def self.eval_code(code, language, options = {})
  run(code, language, options)
end

.execute(code, language, options = {}) ⇒ Object



74
75
76
# File 'lib/code-runner.rb', line 74

def self.execute(code, language, options = {})
  run(code, language, options)
end

.normalize_language(lang) ⇒ Object



96
97
98
# File 'lib/code-runner.rb', line 96

def self.normalize_language(lang)
  SUPPORTED_LANGUAGES[lang.to_s.downcase]
end

.parse_response(data) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/code-runner.rb', line 100

def self.parse_response(data)
  output = data.dig('run', 'output') || ''
  error = data.dig('run', 'stderr') || ''
  compile_output = data.dig('compile', 'output') || ''
  
  output = output.force_encoding('UTF-8') if output.encoding != Encoding::UTF_8
  error = error.force_encoding('UTF-8') if error.encoding != Encoding::UTF_8
  compile_output = compile_output.force_encoding('UTF-8') if compile_output.encoding != Encoding::UTF_8

  {
    success: data.dig('run', 'code') == 0,
    output: output,
    error: error,
    compile_output: compile_output,
    language: data['language'],
    version: data['version'],
    code: data.dig('run', 'code')
  }
end

.run(code, language, options = {}) ⇒ Object

Raises:



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
# File 'lib/code-runner.rb', line 45

def self.run(code, language, options = {})
  piston_lang = normalize_language(language)
  raise Error, "Language '#{language}' not supported" unless piston_lang

  payload = {
    language: piston_lang,
    version: options[:version] || "*",
    files: [{ content: code }],
    stdin: options[:stdin] || "",
    args: options[:args] || [],
    compile_timeout: options[:compile_timeout] || 10000,
    run_timeout: options[:run_timeout] || 5000,
    compile_memory_limit: options[:compile_memory_limit] || -1,
    run_memory_limit: options[:run_memory_limit] || -1
  }

  response = HTTParty.post(API_URL,
    body: payload.to_json,
    headers: { 'Content-Type' => 'application/json' },
    timeout: options[:timeout] || 30
  )

  unless response.success?
    raise Error, "API Error: #{response.code} - #{response.message}"
  end

  parse_response(response.parsed_response)
end

.supported?(language) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/code-runner.rb', line 86

def self.supported?(language)
  !normalize_language(language).nil?
end

.supported_languagesObject



82
83
84
# File 'lib/code-runner.rb', line 82

def self.supported_languages
  SUPPORTED_LANGUAGES.keys.uniq.sort
end

.versionObject



90
91
92
# File 'lib/code-runner.rb', line 90

def self.version
  "1.0.2"
end