2013-04-20 125 views
0

我有一個Ruby腳本Ruby腳本錯誤

#!/usr/bin/ruby 
require 'rubygems' 
require 'mechanize' 
require 'nokogiri' 
require 'highline/import' 
require 'stringio' 

#Change based on Semester 
$term = '09' 
$year = '2012' 
$frequency = 4 #Number of Seconds between check requests 

$agent = Mechanize.new 
$agent.redirect_ok = true 
$agent.user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.11 Safari/535.19" 
$agent.verify_mode = OpenSSL::SSL::VERIFY_NONE 

#Uber simway to colorize outputin 
class String 
def color(c) 
    colors = { 
     :black => 30, 
     :red  => 31, 
     :green => 32, 
     :yellow => 33, 
     :blue => 34, 
     :magenta => 35, 
     :cyan => 36, 
     :white => 37 
    } 
    return "\e[#{colors[c] || c}m#{self}\e[0m" 
end 
end 

#Logins, Gets the Courses, Returns Courses Obj with Name/URL/Tools for each 
def login(username, password) 

#Login to the system! 
page = $agent.get("https://auth.vt.edu/login?service=https://webapps.banner.vt.edu/banner-cas-prod/authorized/banner/SelfService") 
login = page.forms.first 
login.set_fields({ 
    :username => username, 
    :password => password 
}) 
if (login.submit().body.match(/Invalid username or password/)) then 
    return false 
else 
    return true 
end 
end 

#Gets Course Information 
def getCourse(crn) 
begin 
    courseDetails = Nokogiri::HTML($agent.get(
     "https://banweb.banner.vt.edu/ssb/prod/HZSKVTSC.P_ProcComments?CRN=#{crn}&TERM=#{$term}&YEAR=#{$year}" 
    ).body) 
rescue 
    return false #Failed to get course 
end 

#Flatten table to make it easier to work with 
course = {} 
dataSet = false 

course[:title] = courseDetails.css('td.title').last.text.gsub(/-\ +/, '') 
course[:crn] = crn 

courseDetails.css('table table tr').each_with_index do |row| 
    #If we have a dataSet 
    case dataSet 
     when :rowA 
      [ :i, :days, :end, :begin, :end, :exam].each_with_index do |el, i| 
       if row.css('td')[i] then 
        course[el] = row.css('td')[i].text 
       end 
      end 
     when :rowB 
      [ :instructor, :type, :status, :seats, :capacity ].each_with_index do |el, i| 
       course[el] = row.css('td')[i].text 
      end 
    end 

    dataSet = false 
    #Is there a dataset? 
    row.css('td').each do |cell| 
     case cell.text 
      when "Days" 
       dataSet = :rowA 
      when "Instructor" 
       dataSet = :rowB 
     end 
    end 
end 

return course 
end 

#Registers you for the given CRN, returns true if successful, false if not 
def registerCrn(crn) 
#Follow Path 
$agent.get("https://banweb.banner.vt.edu/ssb/prod/twbkwbis.P_GenMenu?name=bmenu.P_MainMnu") 
reg = $agent.get("https://banweb.banner.vt.edu/ssb/prod/hzskstat.P_DispRegStatPage") 
dropAdd = reg.link_with(:href => "/ssb/prod/bwskfreg.P_AddDropCrse?term_in=#{$year}#{$term}").click 

#Fill in CRN Box and Submit 
crnEntry = dropAdd.form_with(:action => '/ssb/prod/bwckcoms.P_Regs') 
crnEntry.fields_with(:id => 'crn_id1').first.value = crn 
crnEntry['CRN_IN'] = crn 
add = crnEntry.submit(crnEntry.button_with(:value => 'Submit Changes')).body 

if add =~ /#{crn}/ && !(add =~ /Registration Errors/) then 
    return true 
else 
    return false 
end 
end 

#Main loop that checks the availaibility of each courses and fires to registerCrn on availaibility 
def checkCourses(courses) 

requestCount = 0 
startTime = Time.new 
loop do 
    system("clear") 

    requestCount += 1 
    nowTime = Time.new 

    puts "Checking Availaibility of CRNs".color(:yellow) 
    puts "--------------------------------\n" 
    puts "Started:\t#{startTime.asctime}".color(:magenta) 
    puts "Now: \t#{nowTime.asctime}".color(:cyan) 
    puts "Request:\t#{requestCount} (Once every #{$frequency} seconds)".color(:green) 
    puts "--------------------------------\n\n" 

    courses.each_with_index do |c, i| 

     puts "#{c[:crn]} - #{c[:title]}".color(:blue) 
     course = getCourse(c[:crn]) 
     next unless course #If throws error 

     puts "Availaibility: #{course[:seats]}/#{course[:capacity]}".color(:red) 

     if (course[:seats] =~ /Full/) then 
     else 
      if (registerCrn(c[:crn])) then 
       puts "CRN #{c[:crn]} Registration Sucessfull" 
       courses.slice!(i) 

      else 
       puts "Couldn't Register" 
      end 

     end 

     print "\n" 
    end 

    sleep $frequency 
end 
end 

#Add courses to be checked 
def addCourses 
crns = [] 

loop do 
    system("clear") 
    puts "Your CRNs:".color(:red) 
    crns.each do |crn| 
     puts " -> #{crn[:title]} (CRN: #{crn[:crn]})".color(:magenta) 
    end 

    #Prompt for CRN 
    alt = (crns.length > 0) ? " (or just type 'start') " : " " 
    input = ask("\nEnter a CRN to add it#{alt}".color(:green) + ":: ") { |q| q.echo = true } 

    #Validate CRN to be 5 Digits 
    if (input =~ /^\d{5}$/) then 

     #Display CRN Info 
     c = getCourse(input.to_s) 
     puts "\nCourse: #{c[:title]} - #{c[:crn]}".color(:red) 
     puts "--> Time: #{c[:begin]}-#{c[:end]} on #{c[:days]}".color(:cyan) 
     puts "--> Teacher: #{c[:instructor]}".color(:cyan) 
     puts "--> Type: #{c[:type]} || Status: #{c[:status]}".color(:cyan) 
     puts "--> Availability: #{c[:seats]}/#{c[:capacity]}\n".color(:cyan) 

     #Add Class Prompt 
     add = ask("Add This Class? (yes/no)".color(:yellow) + ":: ") { |q| q.echo = true } 
     crns.push(c) if (add =~ /yes/) 

    elsif (input == "start") then 
     checkCourses(crns) 
    end 
end 
end 


def main 
system("clear") 
puts "Welcome to CourseAdd by mil".color(:blue) 

username = ask("PID ".color(:green) + ":: ") { |q| q.echo = true } 
password = ask("Password ".color(:green) + ":: ") { |q| q.echo = "*" } 

system("clear") 
if login(username, password) then 
    addCourses 
else 
    puts "Invalid PID/Password" 
    exit 
end 
end 

main 

但是當我運行紅寶石Untitled.rb它給我這個錯誤。

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- mechanize (LoadError) 
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require' 
from /Users/user/Desktop/Untitled.rb:3 

這是什麼意思,我該如何解決?我不確定是否需要通過IDE或終端工作。我是全新的紅寶石,所以我真的不知道問題可能是什麼。

回答

2

您需要安裝機械化。在您的終端中輸入:

gem install mechanize 

安裝完成後重試腳本。如果你有其他缺失的寶石,你可以使用相同的命令來安裝它們。

gem install <gem name> 
+0

我這樣做然後重新運行該程序,並得到相同的錯誤。但它說我沒有權限安裝機械化,所以我沒有sudo gem安裝機械化,輸入我的密碼,並安裝它。然後我用相同的錯誤重新運行它。 – 2013-04-20 23:19:52

+0

''寶石列表'顯示機械寶石? – Intrepidd 2013-04-20 23:42:59

+0

***當地的寶石*** MIME類型(1.22) 淨-HTTP-digest_auth(1.3) 淨HTTP永久性(2.8) – 2013-04-21 00:36:04