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, Message, Timetable

Constant Summary collapse

VERSION =
'0.1.3'
@@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')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

color_str, get_page_doc, input_num, input_passwd, quit, to_abs_url

Constructor Details

#initializeKuport

Returns a new instance of Kuport.



40
41
42
43
44
# File 'lib/kuport.rb', line 40

def initialize
  @agent = Mechanize.new
  @agent.html_parser = self.class
  cookies_load
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



38
39
40
# File 'lib/kuport.rb', line 38

def agent
  @agent
end

Class Method Details

.module_url(*parts) ⇒ Object



46
47
48
# File 'lib/kuport.rb', line 46

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



152
153
154
# File 'lib/kuport.rb', line 152

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

#cookies_clearObject



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

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

#cookies_loadObject



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

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

#cookies_saveObject



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

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

#download(url_or_json, file_path = nil) ⇒ Object

url_or_json is “http://.…”, path:, or [path:, …] If url_or_json is URL, need file_path



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/kuport.rb', line 134

def download(url_or_json, file_path=nil)
  if url_or_json.url?
    download_file(file_path, url_or_json)
    return
  end

  json = JSON.parse(url_or_json, {symbolize_names: true})
  if Array === json
    json.each{|link| download_file(link[:name], link[:path])}
  else
    download_file(json[:name], json[:path])
  end

rescue JSON::ParserError
  raise DownloadError.new("JSON parse error '#{url_or_json}'")
end

#download_file(file_path, url) ⇒ Object



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

def download_file(file_path, url)
  File.write(file_path, agent.get(url).content)
rescue
  DownloadError.new(file_path)
end

#get_module(symb) ⇒ Object



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

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

#loggedin?Boolean

Returns:

  • (Boolean)


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

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

#login(id) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/kuport.rb', line 93

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


75
76
77
78
79
80
# File 'lib/kuport.rb', line 75

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

#login_passwd(id, passwd) ⇒ Object



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

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

Raises:

  • (NotImplementedError)


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

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


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

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

#messagesObject



106
107
108
# File 'lib/kuport.rb', line 106

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

#messages_backnoObject

Raises:

  • (NotImplementedError)


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

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

#messages_readObject



110
111
112
# File 'lib/kuport.rb', line 110

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

#timetableObject



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

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