Sha256: 93e286449a7e384d852e316baee433b7bd6025168ba58948789a68939f5a35ef

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

# encoding: utf-8
# frozen_string_literal: true
module ProxyPacRb
  # Dump Proxy pac to file system
  class ProxyPacDumper
    private

    attr_reader :type, :dumpers

    public

    def initialize
      @dumpers = Hash.new { ProxyPacStringDumper.new }
      @dumpers[:template] = ProxyPacTemplateDumper.new
      @dumpers[:string] = ProxyPacStringDumper.new
    end

    def dump(proxy_pac, type:)
      dumpers[type].dump(proxy_pac)
    end
  end

  # Dump string to file
  class ProxyPacStringDumper
    private

    attr_reader :default_file_name

    public

    def initialize
      @default_file_name = 'proxy.pac'
    end

    def dump(proxy_pac)
      ::File.write(default_file_name, proxy_pac.content)
    end
  end

  # Dump proxy pac based on template
  class ProxyPacTemplateDumper
    def dump(proxy_pac)
      ::File.write(output_path(proxy_pac.source), proxy_pac.content)
    end

    private

    def in_extension
      '.in'
    end

    def out_extension
      '.out'
    end

    def output_path(path)
      if ::File.exist?(path.gsub(/#{in_extension}*$/, '') + in_extension)
        return path.gsub(/#{in_extension}*$/, '')
      elsif ::File.exist? path
        return path + out_extension
      else
        raise Errno::ENOENT, "Both paths \"#{path.gsub(/#{in_extension}*$/, '') + in_extension}\" and \"#{path}\" do not exist."
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
proxy_pac_rb-2.1.0 lib/proxy_pac_rb/proxy_pac_dumper.rb
proxy_pac_rb-2.0.0 lib/proxy_pac_rb/proxy_pac_dumper.rb
proxy_pac_rb-1.0.0 lib/proxy_pac_rb/proxy_pac_dumper.rb