Class: TitechPubnetAuth

Inherits:
Object show all
Defined in:
lib/titech_pubnet_auth.rb,
lib/titech_pubnet_auth/config.rb,
lib/titech_pubnet_auth/version.rb,
lib/titech_pubnet_auth/bin_routines.rb

Defined Under Namespace

Modules: BinRoutines Classes: Config

Constant Summary collapse

SAMPLE_URI =
URI "http://example.org"
HTTP_PROXY =
{ip: '131.112.125.238', port: 3128}
VERSION =
'1.4.0'

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ TitechPubnetAuth

Returns a new instance of TitechPubnetAuth.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/titech_pubnet_auth.rb', line 16

def initialize(opt = {})
  opt = {open_timeout: 5}.merge(opt)
  @agent, @agent_with_proxy = Mechanize.new, Mechanize.new
  [@agent, @agent_with_proxy].each{|agent|
    agent.follow_meta_refresh = true
    agent.open_timeout = opt[:open_timeout]
  }
  @agent_with_proxy.set_proxy(HTTP_PROXY[:ip], HTTP_PROXY[:port])

  @config = opt[:config] || TitechPubnetAuth::Config.get
  fail "config not found" unless @config
end

Instance Method Details

#auth(sample_uri = SAMPLE_URI) ⇒ Object

called if network_available? and not is_connected?



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/titech_pubnet_auth.rb', line 32

def auth(sample_uri = SAMPLE_URI)
  auth_page = @agent.get(sample_uri)
  return false if auth_page.uri.hostname != 'wlanauth.noc.titech.ac.jp'

  auth_page.form do |form|
    form.buttonClicked = 4
    form.redirect_url = sample_uri
    form.username = @config.username
    form.password = @config.password
  end.submit

  return is_connected?
end

#is_connected?(sample_uri = SAMPLE_URI) ⇒ Boolean

called if network_available?

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/titech_pubnet_auth.rb', line 49

def is_connected?(sample_uri = SAMPLE_URI)
  compare_uri_by_first_part sample_uri, @agent_with_proxy.get(sample_uri).uri
rescue # retry without the proxy
  compare_uri_by_first_part sample_uri, @agent.get(sample_uri).uri
end

#network_available?(sample_uri = SAMPLE_URI) ⇒ Boolean

note: titech-pubnet allows to access portal.titech.ac.jp without authentication.

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/titech_pubnet_auth.rb', line 58

def network_available?(sample_uri = SAMPLE_URI)
  @agent.get('http://portal.titech.ac.jp').to_b
rescue # check another website just to make sure
  @agent.get(sample_uri).to_b rescue return false
end