class Question
Public Class Methods
new(q_num, url)
click to toggle source
# File lib/ParsePapers.rb, line 8 def initialize(q_num, url) @page = Nokogiri::HTML(open(url)) @q_num = q_num @url = url end
Public Instance Methods
answer_image()
click to toggle source
# File lib/ParsePapers.rb, line 23 def answer_image html = all("answers")[q_num - 1].join("") return html_2_image(html) end
description()
click to toggle source
# File lib/ParsePapers.rb, line 38 def description info = (all("info")[q_num - 1]).first return get_detail("Description", info) end
exam_notes_image()
click to toggle source
# File lib/ParsePapers.rb, line 28 def exam_notes_image html = all("exam note")[q_num - 1].join("") return html_2_image(html) end
marks()
click to toggle source
# File lib/ParsePapers.rb, line 43 def marks info = (all("info")[q_num - 1]).first return get_detail("Marks", info).to_i end
q_num()
click to toggle source
# File lib/ParsePapers.rb, line 19 def q_num @q_num end
question_image()
click to toggle source
# File lib/ParsePapers.rb, line 14 def question_image html = all("questions")[q_num - 1].join("") return html_2_image(html) end
source()
click to toggle source
# File lib/ParsePapers.rb, line 33 def source info = (all("info")[q_num - 1]).first return get_detail("Question source", info) end
topic()
click to toggle source
# File lib/ParsePapers.rb, line 48 def topic info = (all("info")[q_num - 1]).first return get_detail("Topic", info) end
type()
click to toggle source
# File lib/ParsePapers.rb, line 53 def type info = (all("info")[q_num - 1]).first return get_detail("Type", info) end
Private Instance Methods
all(type)
click to toggle source
# File lib/ParsePapers.rb, line 68 def all(type) types = ["questions", "answers", "exam note", "info"] hash = Hash[types.map.with_index.to_a] # => {"a"=>0, "b"=>1, "c"=>2} next_type = types[((hash[type]).to_i + 1)] objects = [] objects = [] find_starts_of(type).each_with_index do |i, index| unless i == find_starts_of(type).last then object = [] for x in i..(find_starts_of(type)[index + 1] - 1) object.push(@page.css("table")[x]) end unless parse_type(type) == "N" then object.map! { |x| x.to_html} end objects.push object else if parse_type(type) == "N" then object = [] object.push(@page.css("table")[i]) objects.push object else object = [] for x in i..(find_starts_of(next_type).first - 1) object.push(@page.css("table")[x]) unless x == nil end object.map! { |x| x.to_html} objects.push object end end end return objects end
find_starts_of(type)
click to toggle source
# File lib/ParsePapers.rb, line 129 def find_starts_of(type) g = 0 test = [] objects = [] @page.css("table").each_with_index do |object, index| if is_start_of(type, object) then objects.push index end end return objects end
get_detail(detail, info)
click to toggle source
# File lib/ParsePapers.rb, line 141 def get_detail(detail, info) info.css(".indent1new").each do |x| if /#{detail}:/.match(x.content) y = x.content y.slice! "#{detail}: " return y end end end
html_2_image(html)
click to toggle source
# File lib/ParsePapers.rb, line 61 def html_2_image(html) x = "<head><link href='http://content.doublestruck.eu/style/ds.css' rel='stylesheet' type='text/css'></head><body> #{html} </body>" b = Net::HTTP.post_form(URI.parse('http://api.page2images.com/html2image'), {'p2i_html'=> x, 'p2i_key' => '0c9242f0d0aeafa3', 'p2i_url' => @url, 'p2i_screen' => "705x0", 'p2i_size' => "705x0"} ) return JSON.parse(b.body)["image_url"] end
is_start_of(type, table)
click to toggle source
# File lib/ParsePapers.rb, line 111 def is_start_of(type, table) unless table.css("b") == nil then a = table.css("b").to_a objects = [] a.each do |x| if /#{parse_type(type)}[1-9]/.match(x.content) != nil then objects.push(x) end end if objects.count > 0 then return true else return false end else return false end end
parse_type(type)
click to toggle source
# File lib/ParsePapers.rb, line 151 def parse_type(type) case type when "question", "a question", "questions" type = "Q" when "marking" , "answer" , "markings" , "answers" type = "M" when "exam note", "exam notes" , "exam_note" type = "E" when "note" , "info" type = "N" else raise "invalid type in parse_types" end return type end