Class: ProxyPacRb::Environment

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

Overview

Environment in which a proxy.pac will be evaluated

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Environment


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/proxy_pac_rb/environment.rb', line 14

def initialize(options = {})
  @dns_timeout   = 1
  @days          = { 'MON' => 1, 'TUE' => 2, 'WED' => 3, 'THU' => 4, 'FRI' => 5, 'SAT' => 6, 'SUN' => 0 }
  @months        = { 'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4, 'MAY' => 5, 'JUN' => 6, 'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' => 10, 'NOV' => 11, 'DEC' => 12 }

  @client_ip     = IPAddr.new(options.fetch(:client_ip, '127.0.0.1').to_s).to_s
  @time          = Time.parse(options.fetch(:time, Time.now).to_s).to_s
  @io            = options.fetch(:io, $stderr)

  @javascript_function_templates = ProxyPacJs

  @available_methods = [
    :alert,
    :isPlainHostName,
    :dnsDomainIs,
    :localHostOrDomainIs,
    :isResolvable,
    :isInNet,
    :dnsResolve,
    :dnsDomainLevels,
    :shExpMatch
  ]
end

Instance Attribute Details

#available_methodsObject (readonly)

Returns the value of attribute available_methods


12
13
14
# File 'lib/proxy_pac_rb/environment.rb', line 12

def available_methods
  @available_methods
end

Instance Method Details

#alert(msg) ⇒ Object


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

def alert(msg)
  io.puts msg
end

#dnsDomainIs(host, domain) ⇒ Object


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

def dnsDomainIs(host, domain)
  host.end_with? domain
end

#dnsDomainLevels(host) ⇒ Object


69
70
71
# File 'lib/proxy_pac_rb/environment.rb', line 69

def dnsDomainLevels(host)
  host.scan('.').size
end

#dnsResolve(host) ⇒ Object


65
66
67
# File 'lib/proxy_pac_rb/environment.rb', line 65

def dnsResolve(host)
  resolve_host(host)
end

#isInNet(host, base_ip, mask) ⇒ Object

Raises:

  • (ArgumentError)

58
59
60
61
62
63
# File 'lib/proxy_pac_rb/environment.rb', line 58

def isInNet(host, base_ip, mask)
  raise ArgumentError, '<base ip> needs to be defined' if base_ip.nil? || base_ip.empty?
  raise ArgumentError, '<mask> needs to be defined' if mask.nil? || mask.empty?

  IPAddr.new(base_ip).mask(mask).include? resolve_host(host)
end

#isPlainHostName(host) ⇒ Object


42
43
44
# File 'lib/proxy_pac_rb/environment.rb', line 42

def isPlainHostName(host)
  !host.include? '.'
end

#isResolvable(host) ⇒ Object


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

def isResolvable(host)
  !resolve_host(host).blank?
end

#localHostOrDomainIs(host, hostdom) ⇒ Object


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

def localHostOrDomainIs(host, hostdom)
  host == hostdom || hostdom.include?(host)
end

#prepare(string) ⇒ Object


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/proxy_pac_rb/environment.rb', line 103

def prepare(string)
  str = []
  str << string.to_s.chomp

  if client_ip
    str << "\n"
    str << javascript_function_templates.my_ip_address_template(client_ip)
  end

  if time
    str << javascript_function_templates.time_variables
    str << "\n"
    str << javascript_function_templates.week_day_range_template(time)
    str << "\n"
    str << javascript_function_templates.week_day_range_template(time)
    str << "\n"
    str << javascript_function_templates.date_range_template(time)
    str << "\n"
    str << javascript_function_templates.time_range_template(time)
  end

  str.join("\n")
end

#shExpMatch(str, shexp) ⇒ Object


73
74
75
# File 'lib/proxy_pac_rb/environment.rb', line 73

def shExpMatch(str, shexp)
  ::File.fnmatch(shexp, str)
end