Sha256: 606dda69aecccea308957ef6da0455143856369ee3cc93f2ba6cb804cd0b124f

Contents?: true

Size: 1.77 KB

Versions: 46

Compression:

Stored size: 1.77 KB

Contents

module Celerity
  class ViewerConnection

    #
    # Create a new connection to the given host/port
    #

    def self.create(host, port)
      # if the connection fails, we won't spend time loading json
      socket = TCPSocket.new(host, port)
      require "json"
      new(socket)
    end

    def initialize(socket)
      @socket = socket
    end

    #
    # Tells the viewer to render the given HTML, with the given URL as base url.
    #

    def render_html(html, url)
      send_data('method' => 'page_changed', 'html' => html, 'url' => url)
    end

    #
    # Tells the viewer to save a screenshot of the current page to the given path.
    # May not be available on all viewers.
    #

    def save(path)
      send_data('method' => 'save', 'path' => path)
    end


    #
    # Tells the viewer to dump the render tree to the given path.
    # Only available in the Qt viewer.
    #

    def save_render_tree(path)
      send_data('method' => 'save_render_tree', 'path' => path)
    end

    #
    # Get the currently rendered page as a Base64-encoded PNG image.
    # Only available in the Qt viewer.
    #

    def image_data
      send_data('method' => 'image_data')
      data = read_data
      data['image'] || data['error']
    end

    #
    # Close the connection.
    #

    def close
      @socket.close rescue nil
    end

    private

    def send_data(msg)
      json = msg.to_json
      data = "Content-Length: #{json.size}\n\n#{json}"
      @socket.write data
      @socket.flush

      nil
    end

    def read_data
      buf = ''
      until buf =~ /\n\n\z/ || @socket.eof? || @socket.closed?
        buf << @socket.read(1).to_s
      end

      return if buf.empty?

      length = buf[/Content-Length: (\d+)/, 1].to_i
      JSON.parse @socket.read(length)
    end

  end
end

Version data entries

46 entries across 46 versions & 8 rubygems

Version Path
drnic-celerity-0.0.6.17 lib/celerity/viewer_connection.rb
jarib-celerity-0.0.6.18 lib/celerity/viewer_connection.rb
jarib-celerity-0.0.6.19 lib/celerity/viewer_connection.rb
jarib-celerity-0.0.7.1 lib/celerity/viewer_connection.rb
celerity_thingista-0.9.3 lib/celerity/viewer_connection.rb
celerity_thingista-0.9.2 lib/celerity/viewer_connection.rb
frameworks-capybara-0.2.0.rc6 vendor/bundle/ruby/1.8/gems/celerity-0.9.2/lib/celerity/viewer_connection.rb
frameworks-capybara-0.2.0.rc5 vendor/bundle/ruby/1.8/gems/celerity-0.9.2/lib/celerity/viewer_connection.rb
frameworks-capybara-0.2.0.rc4 vendor/bundle/ruby/1.8/gems/celerity-0.9.2/lib/celerity/viewer_connection.rb
frameworks-capybara-0.2.0.rc3 vendor/bundle/ruby/1.8/gems/celerity-0.9.2/lib/celerity/viewer_connection.rb
frameworks-capybara-0.2.0.rc2 vendor/bundle/ruby/1.8/gems/celerity-0.9.2/lib/celerity/viewer_connection.rb
celerity-0.9.2 lib/celerity/viewer_connection.rb
celerity-0.9.1 lib/celerity/viewer_connection.rb
no-click-exception-celerity-0.9.0 lib/celerity/viewer_connection.rb
celerity-0.9.0 lib/celerity/viewer_connection.rb
celerity-0.8.9 lib/celerity/viewer_connection.rb
celerity-0.8.9.pre lib/celerity/viewer_connection.rb
celerity-0.8.8 lib/celerity/viewer_connection.rb
celerity-0.8.7 lib/celerity/viewer_connection.rb
celerity-0.8.6 lib/celerity/viewer_connection.rb