From 943355d9c7661bca892c44ca58d3ff1ed54e4e1c Mon Sep 17 00:00:00 2001 From: "leo.selig" Date: Fri, 12 Feb 2016 11:01:24 +0100 Subject: [PATCH] Add #from_proforma_xml to exercise (only meta data import) - method only considers title and description for now - sets up relation to execution environment with id 1, just to pass validation for now (leoselig/codeocean#1) --- Gemfile | 1 + Gemfile.lock | 1 + app/models/exercise.rb | 9 +++++++++ 3 files changed, 11 insertions(+) diff --git a/Gemfile b/Gemfile index e77ae876..5c6bbd31 100644 --- a/Gemfile +++ b/Gemfile @@ -35,6 +35,7 @@ gem 'uglifier', '>= 1.3.0' gem 'will_paginate', '~> 3.0' gem 'tubesock' gem 'faye-websocket' +gem 'nokogiri' group :development do gem 'better_errors', platform: :ruby diff --git a/Gemfile.lock b/Gemfile.lock index 268f8596..9dda22e1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -361,6 +361,7 @@ DEPENDENCIES jquery-turbolinks kramdown newrelic_rpm + nokogiri nyan-cat-formatter pg pry diff --git a/app/models/exercise.rb b/app/models/exercise.rb index 658a8fb5..9eabb874 100644 --- a/app/models/exercise.rb +++ b/app/models/exercise.rb @@ -1,3 +1,4 @@ +require 'nokogiri' require File.expand_path('../../../lib/active_model/validations/boolean_presence_validator', __FILE__) class Exercise < ActiveRecord::Base @@ -105,6 +106,14 @@ class Exercise < ActiveRecord::Base exercise end + def from_proforma_xml(xml_string) + # how to extract the proforma functionality into a different module in rails? + xml = Nokogiri::XML(xml_string) + self.title = xml.xpath('/root/p:task/p:meta-data/p:title/text()')[0].content + self.description = xml.xpath('/root/p:task/p:description/text()')[0].content, + self.execution_environment_id = 1 + end + def generate_token self.token ||= SecureRandom.hex(4) end