Skip to content

Instantly share code, notes, and snippets.

@igjit
Created July 31, 2018 22:31
Show Gist options
  • Save igjit/e5ea26887bd23bdc2e4834793db7f30e to your computer and use it in GitHub Desktop.
Save igjit/e5ea26887bd23bdc2e4834793db7f30e to your computer and use it in GitHub Desktop.
プログラミング言語の基礎概念の演習システム http://www.fos.kuis.kyoto-u.ac.jp/~igarashi/CoPL/index.cgi に回答をpostするスクリプト
require 'net/http'
require 'uri'
require 'nokogiri'
URL = 'http://www.fos.kuis.kyoto-u.ac.jp/~igarashi/CoPL/index.cgi'
DEFAULT_FORM_DATA = {
command: 'answer',
no: 0
}
DEFAULT_GAME = 'Nat'
def post_sandbox(user, derivation, game = DEFAULT_GAME)
uri = URI.parse(URL)
req = Net::HTTP::Post.new(uri.request_uri)
req.add_field('Cookie', "loginas=#{user};")
form_data = DEFAULT_FORM_DATA.merge(game: game, derivation: derivation)
req.set_form_data(form_data)
Net::HTTP.new(uri.host).request(req)
end
def parse_result(html)
doc = Nokogiri::HTML.parse(html)
{
title: doc.at_css('h1')&.inner_text&.strip,
message: doc.at_css('#main > pre')&.inner_text&.strip
}
end
def main
raise("usage: #{$0} user derivation_path [game]") if ARGV.size < 2
user, derivation_path, game = ARGV
game ||= DEFAULT_GAME
derivation = File.open(derivation_path, &:read)
res = post_sandbox(user, derivation, game)
raise res.code unless res.is_a? Net::HTTPOK
puts parse_result(res.body).values.join("\n")
end
if __FILE__ == $0
main
end
@igjit
Copy link
Author

igjit commented Aug 1, 2018

Usage

$ cat sample.copl
S(S(Z)) plus Z is S(S(Z)) by P-Succ {
  S(Z) plus Z is S(Z) by P-Succ {
    Z plus Z is Z by P-Zero {}
  }
}
$ ruby copl_sandbox.rb your_name sample.copl Nat
正しい導出です.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment