Class: Kuport

Inherits:
Object
  • Object
show all
Extended by:
Helper
Defined in:
lib/kuport.rb,
lib/kuport/helper.rb,
lib/kuport/version.rb

Overview

TODO make test

Direct Known Subclasses

KuportView

Defined Under Namespace

Modules: ClassExtensions, Helper Classes: DownloadError, Materials, Message, Timetable

Constant Summary collapse

VERSION =
'0.1.6'
@@base_url =

Need last slash in url

'https://kuport.sc.kogakuin.ac.jp/ActiveCampus/'.freeze
@@base_module =
'module/'.freeze
@@modules =
{ login: 'Login.php', menu: 'Menu.php', messages: 'Message.php',
  messages_read: 'Message.php?mode=read',
  messages_backno: 'Message.php?mode=backno',
  timetable: 'Jikanwari.php',
  materials: 'Kyozai.php',
}
{messages: '個人宛お知らせ',
messages_read: '個人宛お知らせ(既読)',
timetable: '時間割',
materials: '電子教材',
}
@@cache_dir =
File.join(Dir.home, '.cache', 'kuport').freeze
@@cookies_file =
File.join(@@cache_dir, 'cookies.jar')

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

clear_proxy_env_var, color_str, escape_filename, get_page_doc, get_proxy_env_var, input_num, input_passwd, parse_proxy_str, quit, to_abs_url

Constructor Details

#initialize(**opts) ⇒ Kuport

Returns a new instance of Kuport.



36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

Class Method Details

.filter(key, pattern) ⇒ Object

return Hash



171
172
173
# 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) ⇒ Object



54
55
56
# File 'lib/kuport.rb', line 54

def self.module_url(*parts)
  URI.join(@@base_url, @@base_module, *parts)
end

.parse(text, url = nil, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_HTML, &block) ⇒ Object

html parser for Mechanize. force encode to UTF-8



176
177
178
# 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

Instance Method Details

#agentObject



50
51
52
# File 'lib/kuport.rb', line 50

def agent
  @agent ||= Mechanize.new
end

#cookies_clearObject



74
75
76
77
# File 'lib/kuport.rb', line 74

def cookies_clear
  agent.cookie_jar.clear!
  FileUtils.rm(@@cookies_file) if File.exist?(@@cookies_file)
end

#cookies_loadObject



70
71
72
# File 'lib/kuport.rb', line 70

def cookies_load
  File.open(@@cookies_file, 'r'){|f| agent.cookie_jar.load(f) } rescue nil
end

#cookies_saveObject



66
67
68
# File 'lib/kuport.rb', line 66

def cookies_save
  File.open(@@cookies_file, 'w'){|f| agent.cookie_jar.save(f, {session: true})}
end

#download_file(file_path, url) ⇒ Object



134
135
136
137
138
# 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) ⇒ Object

json is path: or [path:, …]



141
142
143
144
145
146
147
148
149
150
# 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

Takeout pair of :name and ;path recursively



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# 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) ⇒ Object



58
59
60
# File 'lib/kuport.rb', line 58

def get_module(symb)
  agent.get(self.class.module_url(@@modules[symb]))
end

#loggedin?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/kuport.rb', line 79

def loggedin?
  nil != get_module(:login).form.has_value?('Logout')
end

#login(id) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/kuport.rb', line 101

def (id)
  return true if 
  Kuport.quit('Please student id', 4) if id.blank?

  3.times do
    return true if (id, Kuport.input_passwd)
    warn 'Login failed'
  end

  warn 'Login error'
  return false
end


83
84
85
86
87
88
# File 'lib/kuport.rb', line 83

def 
  return false unless cookies_load
  return true if loggedin?
  agent.cookie_jar.clear!
  false
end

#login_passwd(id, passwd) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/kuport.rb', line 90

def (id, passwd)
  get_module(:login).form_with(name: 'login_form') do |f|
    f. = id
    f.passwd = passwd
  end.submit

  return false unless loggedin?
  cookies_save
  true
end

#materialsObject



130
131
132
# File 'lib/kuport.rb', line 130

def materials
  @materials ||= Materials.new(get_module(:materials))
end


62
63
64
# File 'lib/kuport.rb', line 62

def menu_links
  @menu_links ||= get_module(:menu).links.reject{|l| l.text.empty? }
end

#messagesObject



114
115
116
# File 'lib/kuport.rb', line 114

def messages
  @messages ||= Message.parse_page(agent, get_module(:messages))
end

#messages_backnoObject

Raises:

  • (NotImplementedError)


122
123
124
# File 'lib/kuport.rb', line 122

def messages_backno
  raise NotImplementedError.new('Kuport#messages_backno') # TODO
end

#messages_readObject



118
119
120
# File 'lib/kuport.rb', line 118

def messages_read
  @messages_read ||= Message.parse_page(agent, get_module(:messages_read))
end

#timetableObject



126
127
128
# File 'lib/kuport.rb', line 126

def timetable
  @timetable ||= Timetable.new(get_module(:timetable))
end