Module: WifiLogin

Defined in:
lib/wifi_login.rb,
lib/wifi_login/cli.rb,
lib/wifi_login/version.rb,
lib/wifi_login/provider.rb,
lib/wifi_login/providers/docomo.rb

Defined Under Namespace

Modules: Provider, Providers Classes: CLI, Error

Constant Summary collapse

AIRPORT_CMD =
'/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I'
VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.credential(ssid) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/wifi_login.rb', line 44

def self.credential(ssid)
  pit = Pit.get(ssid)
  id = pit['id']
  password = pit['password']
  unless (id && password)
    raise WifiLogin::Error.new("credential is not found. please run `pit set #{ssid}`.") 
  end
  [id, password]
end

.in_rbenv?Boolean

Returns:

  • (Boolean)


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

def self.in_rbenv?
  ENV.has_key?('RBENV_ROOT')
end

.loginObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wifi_login.rb', line 18

def self.
  ssid = self.ssid
  puts "detected SSID: #{ssid}"
  provider = provider(ssid)
  id, password = credential(ssid)
  begin
    provider(ssid).(id, password)
  rescue SocketError => e
    puts e.message
    retry_count ||= 0
    raise if (retry_count += 1) > 6
    sleep 10
    retry
  end
end

.provider(ssid) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/wifi_login.rb', line 34

def self.provider(ssid)
  provider_name = WifiLogin::Providers.constants.find {|provider|
    provider.downcase.to_s == ssid.downcase
  }
  unless provider_name
    raise WifiLogin::Error.new("provider is not found for SSID: #{ssid}")
  end
  WifiLogin::Providers.const_get(provider_name).new
end

.providersObject



14
15
16
# File 'lib/wifi_login.rb', line 14

def self.providers
  @@providers ||= {}
end

.ssidObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wifi_login.rb', line 54

def self.ssid
  status, stdout, stderr = systemu AIRPORT_CMD
  if status.exitstatus != 0
    raise WifiLogin::Error.new("faild to exec #{cmd}")
  end

  ssid_line = stdout.each_line.find {|line| line.match(/^\s+SSID:/) }
  unless ssid_line
    raise WifiLogin::Error.new("not found SSID (disable wi-fi?)")
  end
  ssid_line.chomp.split(': ')[1]
end