Class: ProxyPacRb::Rules::CanBeParsed

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

Overview

A proxy pac needs be parsable

Instance Method Summary collapse

Constructor Details

#initializeCanBeParsed

Returns a new instance of CanBeParsed.



64
65
66
# File 'lib/proxy_pac_rb/proxy_pac_linter.rb', line 64

def initialize
  @compiler = JavascriptCompiler.new
end

Instance Method Details

#lint(proxy_pac) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/proxy_pac_rb/proxy_pac_linter.rb', line 68

def lint(proxy_pac)
  uri = Addressable::URI.parse('http://example.com')

  javascript = compiler.compile(content: proxy_pac.content, environment: Environment.new)

  # Some errors can only be found, when evaluating the source
  # e.g. reference undefined variables
  javascript.FindProxyForURL(uri.to_s, uri.host)

  self
rescue => err
  message = if proxy_pac.type? :string
              %(proxy.pac is only given as string "#{proxy_pac.source}" cannot be parsed:\n#{err.message}".)
            elsif proxy_pac.type? :url
              %(proxy.pac-url "#{proxy_pac.source}" cannot be parsed:\n#{err.message}".)
            else
              %(proxy.pac-file "#{proxy_pac.source}" cannot be parsed:\n#{err.message}".)
            end

  raise LinterError, message
end