Class: AutoselHttpProxy::Setting

Inherits:
Object
  • Object
show all
Defined in:
lib/autosel_http_proxy.rb

Constant Summary collapse

@@proxy_url =
nil
@@proxy_setting =
nil

Class Method Summary collapse

Class Method Details

.create_conf(filename) ⇒ Object



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

def self.create_conf(filename)
  File.open(filename, 'w', 0600) {|io|
    io.puts <<EOF
PROXY_LIST =
  [{:if => 'hostname_found? "hostname.example.com"',
 :proxy_host => 'proxy.example.com',
 :proxy_port => '8080',
 :proxy_user => nil,
 :proxy_pass => nil,},]
EOF
  }
end

.hostname_found?(hostname) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
# File 'lib/autosel_http_proxy.rb', line 18

def self.hostname_found?(hostname)
  begin
    Resolv.getaddress(hostname)
    true
  rescue
    false
  end
end

.initObject



85
86
87
88
89
# File 'lib/autosel_http_proxy.rb', line 85

def self.init
  load_conf PROXY_CONFFILE
  init_proxy_setting PROXY_LIST
  set_proxy_env!
end

.init_proxy_setting(list) ⇒ Object



27
28
29
30
31
32
# File 'lib/autosel_http_proxy.rb', line 27

def self.init_proxy_setting(list)
  list.each{|a|
    return @@proxy_setting = a if eval a[:if]
  }
  @@proxy_setting = nil
end

.load_conf(filename) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/autosel_http_proxy.rb', line 67

def self.load_conf(filename)
  unless File.exist?(filename)
    puts "*** \"#{filename}\" created."
    puts "*** Please config it."
    create_conf filename
    raise ConfigNotFound
  end

  load filename

  if PROXY_LIST[0][:if] ==
      'hostname_found? "hostname.example.com"'
    puts "*** \"#{filename}\" isn't configured."
    puts "*** Please config it."
    raise ConfigNotConfigured
  end
end

.proxy_settingObject



16
# File 'lib/autosel_http_proxy.rb', line 16

def self.proxy_setting() @@proxy_setting end

.proxy_urlObject



15
# File 'lib/autosel_http_proxy.rb', line 15

def self.proxy_url() @@proxy_url end

.set_proxy_env!Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/autosel_http_proxy.rb', line 34

def self.set_proxy_env!
  return nil unless proxy_setting
  unless proxy_setting[:proxy_host]
    puts "*** Need \":proxy_host\" in \"#{PROXY_CONFFILE}\"."
    raise NeedHostname
  end

  url = "http://"
  if proxy_setting[:proxy_user] and proxy_setting[:proxy_pass]
    url += "#{proxy_setting[:proxy_user]}:#{proxy_setting[:proxy_pass]}@"
  end
  url += proxy_setting[:proxy_host] if proxy_setting[:proxy_host]
  url += ":" + proxy_setting[:proxy_port] if proxy_setting[:proxy_port]

  ENV['http_proxy'] = url
  ENV['https_proxy'] = url
  ENV['ftp_proxy'] = url
  @@proxy_url = url
end