proxy_pac_rb 0.3.4 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +5 -13
  2. data/.gitignore +0 -1
  3. data/.rubocop.yml +4 -0
  4. data/Gemfile +30 -19
  5. data/Gemfile.lock +220 -0
  6. data/README.md +3 -0
  7. data/Rakefile +49 -13
  8. data/bin/pprb +1 -116
  9. data/config/license_finder.yml +12 -0
  10. data/config/rubocop/exclude.yml +8 -0
  11. data/config/rubocop/include.yml +27 -0
  12. data/cucumber.yml +2 -0
  13. data/doc/dependencies/dependencies.db +0 -0
  14. data/features/resolve_proxy.feature +17 -0
  15. data/features/support/aruba.rb +13 -0
  16. data/features/support/env.rb +10 -1
  17. data/features/support/fixtures.rb +15 -0
  18. data/features/support/reporting.rb +2 -0
  19. data/lib/proxy_pac_rb/cli.rb +75 -0
  20. data/lib/proxy_pac_rb/cli_validator.rb +34 -0
  21. data/lib/proxy_pac_rb/encoding.rb +1 -1
  22. data/lib/proxy_pac_rb/environment.rb +10 -8
  23. data/lib/proxy_pac_rb/exceptions.rb +4 -4
  24. data/lib/proxy_pac_rb/file.rb +3 -3
  25. data/lib/proxy_pac_rb/parser.rb +11 -2
  26. data/lib/proxy_pac_rb/proxy_pac_js.rb +5 -5
  27. data/lib/proxy_pac_rb/runtime.rb +6 -5
  28. data/lib/proxy_pac_rb/runtimes/rubyracer.rb +42 -35
  29. data/lib/proxy_pac_rb/runtimes/rubyrhino.rb +27 -22
  30. data/lib/proxy_pac_rb/runtimes.rb +13 -15
  31. data/lib/proxy_pac_rb/version.rb +3 -2
  32. data/lib/proxy_pac_rb.rb +7 -3
  33. data/proxy_pac_rb.gemspec +1 -1
  34. data/script/ci +0 -4
  35. data/script/test_web +2 -1
  36. data/spec/environment_spec.rb +23 -23
  37. data/spec/file_spec.rb +5 -5
  38. data/spec/parser_spec.rb +16 -13
  39. data/spec/support/aruba.rb +50 -0
  40. data/spec/support/rspec.rb +18 -4
  41. data/spec/support/{string.rb → strip.rb} +0 -0
  42. data/tmp/script.rb +0 -2
  43. metadata +37 -29
  44. data/spec/support/environment.rb +0 -17
  45. data/spec/support/filesystem.rb +0 -20
@@ -1,16 +1,16 @@
1
1
  # encoding: utf-8
2
2
  module ProxyPacRb
3
3
  module Exceptions
4
- #raise on error
4
+ # raise on error
5
5
  class ProgramError < StandardError; end
6
6
 
7
- #raise on java script runtime error
7
+ # raise on java script runtime error
8
8
  class RuntimeUnavailable < StandardError; end
9
9
 
10
- #raise on invalid argument
10
+ # raise on invalid argument
11
11
  class InvalidArgument < StandardError; end
12
12
 
13
- #raise on invalid argument
13
+ # raise on invalid argument
14
14
  class UrlInvalid < StandardError; end
15
15
  end
16
16
  end
@@ -1,6 +1,6 @@
1
1
  module ProxyPacRb
2
+ # Proxy pac file
2
3
  class File
3
-
4
4
  private
5
5
 
6
6
  attr_reader :javascript
@@ -13,9 +13,9 @@ module ProxyPacRb
13
13
 
14
14
  def find(url)
15
15
  uri = Addressable::URI.heuristic_parse(url)
16
- fail Exceptions::UrlInvalid, "url is missing host" unless uri.host
16
+ fail Exceptions::UrlInvalid, 'url is missing host' unless uri.host
17
17
 
18
- javascript.call("FindProxyForURL", url, uri.host)
18
+ javascript.call('FindProxyForURL', url, uri.host)
19
19
  end
20
20
  end
21
21
  end
@@ -1,7 +1,16 @@
1
1
  # encoding: utf-8
2
2
  module ProxyPacRb
3
+ # Proxy Pac parser
4
+ #
5
+ # @example Usage
6
+ #
7
+ # parser = Parser.new
8
+ # parser.load('http://example.com/proxy.pac')
9
+ # parser.read('file.pac')
10
+ #
11
+ # string = ''
12
+ # parser.source(string)
3
13
  class Parser
4
-
5
14
  private
6
15
 
7
16
  attr_reader :runtime, :environment
@@ -16,7 +25,7 @@ module ProxyPacRb
16
25
  end
17
26
 
18
27
  def load(url, options = {})
19
- create_file(open(url, { :proxy => false }.merge(options)).read)
28
+ create_file(open(url, { proxy: false }.merge(options)).read)
20
29
  end
21
30
 
22
31
  def read(file)
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
2
  module ProxyPacRb
3
+ # Javascript-Methods for evaluation of proxy.pac
3
4
  class ProxyPacJs
4
5
  class << self
5
6
  def my_ip_address_template(value)
@@ -10,7 +11,6 @@ module ProxyPacRb
10
11
  EOS
11
12
  end
12
13
 
13
-
14
14
  # taken from releases-mozilla-release / netwerk / base / src / ProxyAutoConfig.cpp @ bitbucket.org
15
15
  # https://bitbucket.org/mozilla/releases-mozilla-release/raw/dece38633cf1adcab2071d69fea264580d24cc9e/netwerk/base/src/ProxyAutoConfig.cpp
16
16
  def time_variables
@@ -21,7 +21,7 @@ module ProxyPacRb
21
21
  end
22
22
 
23
23
  def week_day_range_template(value = nil)
24
- value = %Q{"#{value}"} if value
24
+ value = %("#{value}") if value
25
25
 
26
26
  <<-EOS.strip_heredoc
27
27
  function weekdayRange() {
@@ -51,7 +51,7 @@ module ProxyPacRb
51
51
  end
52
52
 
53
53
  def date_range_template(value = nil)
54
- value = %Q{"#{value}"} if value
54
+ value = %("#{value}") if value
55
55
 
56
56
  <<-EOS.strip_heredoc
57
57
  function dateRange() {
@@ -79,7 +79,7 @@ module ProxyPacRb
79
79
  getMonth(arguments[0]));
80
80
  } else if (tmp < 32) {
81
81
  return ((isGMT ? date.getUTCDate() : date.getDate()) == tmp);
82
- } else {
82
+ } else {
83
83
  return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) ==
84
84
  tmp);
85
85
  }
@@ -132,7 +132,7 @@ module ProxyPacRb
132
132
  end
133
133
 
134
134
  def time_range_template(value = nil)
135
- value = %Q{"#{value}"} if value
135
+ value = %("#{value}") if value
136
136
 
137
137
  <<-EOS.strip_heredoc
138
138
  function timeRange() {
@@ -1,29 +1,30 @@
1
1
  module ProxyPacRb
2
2
  # Abstract base class for runtimes
3
3
  class Runtime
4
+ # Context
4
5
  class Context
5
6
  include Encoding
6
7
 
7
8
  attr_accessor :context
8
9
 
9
10
  def include(environment)
10
- environment.available_methods.each do |name|
11
+ environment.available_methods.each do |name|
11
12
  context[name] = environment.method(name)
12
13
  end
13
14
  end
14
15
 
15
- def initialize(runtime, source = "")
16
+ def initialize(_runtime, _source = '')
16
17
  end
17
18
 
18
- def exec(source, options = {})
19
+ def exec(_source, _options = {})
19
20
  fail NotImplementedError
20
21
  end
21
22
 
22
- def eval(source, options = {})
23
+ def eval(_source, _options = {})
23
24
  fail NotImplementedError
24
25
  end
25
26
 
26
- def call(properties, *args)
27
+ def call(_properties, *_args)
27
28
  fail NotImplementedError
28
29
  end
29
30
  end
@@ -1,7 +1,9 @@
1
1
  module ProxyPacRb
2
+ # Ruby Racer Runtime
2
3
  class RubyRacerRuntime < Runtime
4
+ # Context
3
5
  class Context < Runtime::Context
4
- def initialize(runtime, source = "", environment = nil)
6
+ def initialize(_runtime, source = '', _environment = nil)
5
7
  source = encode(source)
6
8
 
7
9
  lock do
@@ -13,24 +15,24 @@ module ProxyPacRb
13
15
  def exec(source, options = {})
14
16
  source = encode(source)
15
17
 
16
- if /\S/ =~ source
17
- eval "(function(){#{source}})()", options
18
- end
18
+ # rubocop:disable Lint/Eval
19
+ eval "(function(){#{source}})()", options if /\S/ =~ source
20
+ # rubocop:enable Lint/Eval
19
21
  end
20
22
 
21
- def eval(source, options = {})
23
+ def eval(source, _options = {})
22
24
  source = encode(source)
23
25
 
24
- if /\S/ =~ source
25
- lock do
26
- begin
27
- unbox context.eval("(#{source})")
28
- rescue ::V8::JSError => e
29
- if e.value["name"] == "SyntaxError"
30
- fail RuntimeError, e.value.to_s
31
- else
32
- raise Exceptions::ProgramError, e.value.to_s
33
- end
26
+ return nil unless /\S/ =~ source
27
+
28
+ lock do
29
+ begin
30
+ unbox context.eval("(#{source})")
31
+ rescue ::V8::JSError => e
32
+ if e.value['name'] == 'SyntaxError'
33
+ raise e.value.to_s
34
+ else
35
+ raise Exceptions::ProgramError, e.value.to_s
34
36
  end
35
37
  end
36
38
  end
@@ -41,8 +43,8 @@ module ProxyPacRb
41
43
  begin
42
44
  unbox context.eval(properties).call(*args)
43
45
  rescue ::V8::JSError => e
44
- if e.value["name"] == "SyntaxError"
45
- raise RuntimeError, e.value.to_s
46
+ if e.value['name'] == 'SyntaxError'
47
+ raise e.value.to_s
46
48
  else
47
49
  raise Exceptions::ProgramError, e.value.to_s
48
50
  end
@@ -57,44 +59,49 @@ module ProxyPacRb
57
59
  when ::V8::Array
58
60
  value.map { |v| unbox(v) }
59
61
  when ::V8::Object
60
- value.inject({}) do |vs, (k, v)|
62
+ # rubocop:disable Style/EachWithObject
63
+ value.reduce({}) do |vs, (k, v)|
61
64
  vs[k] = unbox(v) unless v.is_a?(::V8::Function)
62
65
  vs
63
66
  end
67
+ # rubocop:enable Style/EachWithObject
64
68
  when String
65
- value.respond_to?(:force_encoding) ?
66
- value.force_encoding('UTF-8') :
69
+ if value.respond_to?(:force_encoding)
70
+ value.force_encoding('UTF-8')
71
+ else
67
72
  value
73
+ end
68
74
  else
69
75
  value
70
76
  end
71
77
  end
72
78
 
73
79
  private
74
- def lock
75
- result, exception = nil, nil
76
- V8::C::Locker() do
77
- begin
78
- result = yield
79
- rescue Exception => e
80
- exception = e
81
- end
82
- end
83
80
 
84
- if exception
85
- raise exception
86
- else
87
- result
81
+ def lock
82
+ result, exception = nil, nil
83
+ V8::C::Locker() do
84
+ begin
85
+ result = yield
86
+ rescue => e
87
+ exception = e
88
88
  end
89
89
  end
90
+
91
+ if exception
92
+ fail exception
93
+ else
94
+ result
95
+ end
96
+ end
90
97
  end
91
98
 
92
99
  def name
93
- "therubyracer (V8)"
100
+ 'therubyracer (V8)'
94
101
  end
95
102
 
96
103
  def available?
97
- require "v8"
104
+ require 'v8'
98
105
  true
99
106
  rescue LoadError
100
107
  false
@@ -1,7 +1,9 @@
1
1
  module ProxyPacRb
2
+ # Ruby Rhine Runtime
2
3
  class RubyRhinoRuntime < Runtime
4
+ # Context
3
5
  class Context < Runtime::Context
4
- def initialize(runtime, source = "")
6
+ def initialize(_runtime, source = '')
5
7
  source = encode(source)
6
8
 
7
9
  self.context = ::Rhino::Context.new
@@ -12,20 +14,20 @@ module ProxyPacRb
12
14
  def exec(source, options = {})
13
15
  source = encode(source)
14
16
 
15
- if /\S/ =~ source
16
- eval "(function(){#{source}})()", options
17
- end
17
+ return nil unless /\S/ =~ source
18
+
19
+ # rubocop:disable Lint/Eval
20
+ eval "(function(){#{source}})()", options
21
+ # rubocop:enable Lint/Eval
18
22
  end
19
23
 
20
- def eval(source, options = {})
24
+ def eval(source, _options = {})
21
25
  source = encode(source)
22
26
 
23
- if /\S/ =~ source
24
- unbox context.eval("(#{source})")
25
- end
27
+ unbox context.eval("(#{source})") if /\S/ =~ source
26
28
  rescue ::Rhino::JSError => e
27
29
  if e.message =~ /^syntax error/
28
- raise RuntimeError, e.message
30
+ raise e.message
29
31
  else
30
32
  raise Exceptions::ProgramError, e.message
31
33
  end
@@ -34,19 +36,20 @@ module ProxyPacRb
34
36
  def call(properties, *args)
35
37
  unbox context.eval(properties).call(*args)
36
38
  rescue ::Rhino::JSError => e
37
- if e.message == "syntax error"
38
- raise RuntimeError, e.message
39
+ if e.message == 'syntax error'
40
+ raise e.message
39
41
  else
40
42
  raise Exceptions::ProgramError, e.message
41
43
  end
42
44
  end
43
45
 
44
46
  def unbox(value)
45
- case value = ::Rhino::to_ruby(value)
47
+ case value = ::Rhino.to_ruby(value)
46
48
  when Java::OrgMozillaJavascript::NativeFunction
47
49
  nil
48
50
  when Java::OrgMozillaJavascript::NativeObject
49
- value.inject({}) do |vs, (k, v)|
51
+ # rubocop:disable Style/EachWithObject
52
+ value.reduce({}) do |vs, (k, v)|
50
53
  case v
51
54
  when Java::OrgMozillaJavascript::NativeFunction, ::Rhino::JS::Function
52
55
  nil
@@ -55,6 +58,7 @@ module ProxyPacRb
55
58
  end
56
59
  vs
57
60
  end
61
+ # rubocop:enable Style/EachWithObject
58
62
  when Array
59
63
  value.map { |v| unbox(v) }
60
64
  else
@@ -63,22 +67,23 @@ module ProxyPacRb
63
67
  end
64
68
 
65
69
  private
66
- # Disables bytecode compiling which limits you to 64K scripts
67
- def fix_memory_limit!(cxt)
68
- if cxt.respond_to?(:optimization_level=)
69
- cxt.optimization_level = -1
70
- else
71
- cxt.instance_eval { @native.setOptimizationLevel(-1) }
72
- end
70
+
71
+ # Disables bytecode compiling which limits you to 64K scripts
72
+ def fix_memory_limit!(cxt)
73
+ if cxt.respond_to?(:optimization_level=)
74
+ cxt.optimization_level = -1
75
+ else
76
+ cxt.instance_eval { @native.setOptimizationLevel(-1) }
73
77
  end
78
+ end
74
79
  end
75
80
 
76
81
  def name
77
- "therubyrhino (Rhino)"
82
+ 'therubyrhino (Rhino)'
78
83
  end
79
84
 
80
85
  def available?
81
- require "rhino"
86
+ require 'rhino'
82
87
  true
83
88
  rescue LoadError
84
89
  false
@@ -1,4 +1,5 @@
1
1
  module ProxyPacRb
2
+ # JavaScript Runtimes
2
3
  module Runtimes
3
4
  RubyRacer = RubyRacerRuntime.new
4
5
  RubyRhino = RubyRhinoRuntime.new
@@ -6,8 +7,8 @@ module ProxyPacRb
6
7
  class << self
7
8
  def autodetect
8
9
  from_environment || best_available ||
9
- fail(Exceptions::RuntimeUnavailable, "Could not find a JavaScript runtime. " +
10
- "See https://github.com/sstephenson/execjs for a list of available runtimes.")
10
+ fail(Exceptions::RuntimeUnavailable, 'Could not find a JavaScript runtime. ' \
11
+ 'See https://github.com/sstephenson/execjs for a list of available runtimes.')
11
12
  end
12
13
 
13
14
  def best_available
@@ -15,27 +16,24 @@ module ProxyPacRb
15
16
  end
16
17
 
17
18
  def from_environment
18
- if name = ENV["JS_RUNTIME"]
19
- if runtime = const_get(name)
20
- if runtime.available?
21
- runtime if runtime.available?
22
- else
23
- fail Exceptions::RuntimeUnavailable, "#{runtime.name} runtime is not available on this system"
24
- end
25
- elsif !name.empty?
26
- fail Exceptions::RuntimeUnavailable, "#{name} runtime is not defined"
27
- end
28
- end
19
+ return nil unless ENV['JS_RUNTIME']
20
+
21
+ runtime = const_get(ENV['JS_RUNTIME'])
22
+
23
+ fail Exceptions::RuntimeUnavailable, "#{ENV['JS_RUNTIME']} runtime is not defined" unless runtime
24
+ fail Exceptions::RuntimeUnavailable, "#{runtime.name} runtime is not available on this system" unless runtime.available?
25
+
26
+ runtime
29
27
  end
30
28
 
31
29
  def names
32
- @names ||= constants.inject({}) { |h, name| h.merge(const_get(name) => name) }.values
30
+ @names ||= constants.reduce({}) { |a, e| a.merge(const_get(e) => e) }.values
33
31
  end
34
32
 
35
33
  def runtimes
36
34
  @runtimes ||= [
37
35
  RubyRacer,
38
- RubyRhino,
36
+ RubyRhino
39
37
  ]
40
38
  end
41
39
  end
@@ -1,4 +1,5 @@
1
- #main ProxyPacRb
1
+ # encoding: utf-8
2
+ # ProxyPacRb
2
3
  module ProxyPacRb
3
- VERSION = '0.3.4'
4
+ VERSION = '0.3.6'
4
5
  end