class Kuport
TODO make test
Constants
- VERSION
Public Class Methods
filter(key, pattern)
click to toggle source
return Hash
# File lib/kuport.rb, line 171 def self.filter(key, pattern) JSON.load(STDIN.read).select{|m| m[key].to_s.match?(pattern)} end
module_url(*parts)
click to toggle source
# File lib/kuport.rb, line 54 def self.module_url(*parts) URI.join(@@base_url, @@base_module, *parts) end
new(**opts)
click to toggle source
# File lib/kuport.rb, line 36 def initialize(**opts) agent.html_parser = self.class cookies_load # If proxy is blank, don't use proxy proxy = opts.key?(:proxy) ? opts[:proxy] : Kuport.get_proxy_env_var if proxy.blank? Kuport.clear_proxy_env_var else agent.set_proxy(*Kuport.parse_proxy_str(proxy)) end end
parse(text, url = nil, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_HTML, &block)
click to toggle source
html parser for Mechanize. force encode to UTF-8
# File lib/kuport.rb, line 176 def self.parse(text, url = nil, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_HTML, &block) Nokogiri::HTML::Document.parse(text.toutf8, url, 'UTF-8', options, &block) end
Public Instance Methods
agent()
click to toggle source
# File lib/kuport.rb, line 50 def agent @agent ||= Mechanize.new end
download_file(file_path, url)
click to toggle source
# File lib/kuport.rb, line 134 def download_file(file_path, url) File.write(file_path, agent.get(url).content) rescue DownloadError.new(file_path) end
download_with_json(json_str)
click to toggle source
json is {name:, path:} or [{name:, path:}, …]
# File lib/kuport.rb, line 141 def download_with_json(json_str) h = JSON.parse(json_str, {symbolize_names: true}) find_links(h).each do |h| download_file(h[:name], h[:path]) puts h[:name] end rescue JSON::ParserError raise DownloadError.new("JSON parse error '#{json}'") end
find_links(j)
click to toggle source
Takeout pair of :name and ;path recursively
# File lib/kuport.rb, line 153 def find_links(j) links = [] if j.is_a?(Array) j.each{|v| links.push(*find_links(v))} elsif j.is_a?(Hash) link = j.take_with_keys(:name, :path) links.push(link) if link j.each do |_,v| links.push(*find_links(v)) if v.is_a?(Array) || v.is_a?(Hash) end end return links end
get_module(symb)
click to toggle source
# File lib/kuport.rb, line 58 def get_module(symb) agent.get(self.class.module_url(@@modules[symb])) end
loggedin?()
click to toggle source
# File lib/kuport.rb, line 79 def loggedin? nil != get_module(:login).form.has_value?('Logout') end
login(id)
click to toggle source
# File lib/kuport.rb, line 101 def login(id) return true if login_cookie Kuport.quit('Please student id', 4) if id.blank? 3.times do return true if login_passwd(id, Kuport.input_passwd) warn 'Login failed' end warn 'Login error' return false end
login_passwd(id, passwd)
click to toggle source
# File lib/kuport.rb, line 90 def login_passwd(id, passwd) get_module(:login).form_with(name: 'login_form') do |f| f.login = id f.passwd = passwd end.submit return false unless loggedin? cookies_save true end
materials()
click to toggle source
# File lib/kuport.rb, line 130 def materials @materials ||= Materials.new(get_module(:materials)) end
messages()
click to toggle source
# File lib/kuport.rb, line 114 def messages @messages ||= Message.parse_page(agent, get_module(:messages)) end
messages_backno()
click to toggle source
# File lib/kuport.rb, line 122 def messages_backno raise NotImplementedError.new('Kuport#messages_backno') # TODO end
messages_read()
click to toggle source
# File lib/kuport.rb, line 118 def messages_read @messages_read ||= Message.parse_page(agent, get_module(:messages_read)) end
timetable()
click to toggle source
# File lib/kuport.rb, line 126 def timetable @timetable ||= Timetable.new(get_module(:timetable)) end