Sha256: 348c34a161accb8db18d900817c6cb1218642987c26d702339addc768151b6b5

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'childprocess'
require 'socket'

module BrowserMob
  module Proxy

    class Server
      attr_reader :port

      def initialize(path, opts = {})
        unless File.exist?(path)
          raise Errno::ENOENT, path
        end

        unless File.executable?(path)
          raise Errno::EACCES, "not executable: #{path}"
        end

        @path = path
        @port = Integer(opts[:port] || 8080)

        @process = ChildProcess.new(path, "--port", port.to_s)
        @process.io.inherit! if opts[:log]
      end

      def start
        @process.start
        sleep 0.1 until listening? && initialized?

        pid = Process.pid
        at_exit { stop if Process.pid == pid }

        self
      end

      def url
        "http://localhost:#{port}"
      end

      def create_proxy
        Client.from url
      end

      def stop
        @process.stop if @process.alive?
      end

      private


      def listening?
        TCPSocket.new("127.0.0.1", port).close
        true
      rescue
        false
      end

      def initialized?
        RestClient.get("#{url}/proxy")
        true
      rescue RestClient::Exception
        false
      end
    end # Server

  end # Proxy
end # BrowserMob

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
browsermob-proxy-0.1.8.rc1 lib/browsermob/proxy/server.rb