Sha256: 5ef7e7ac6bb569632bb1db468f8656417d2fef1ab2c83b36d030a0c996b118fd

Contents?: true

Size: 1.27 KB

Versions: 15

Compression:

Stored size: 1.27 KB

Contents

require 'socket'
require 'net/http'

module Spontaneous
  module Publishing
    # a simple UNIX socket capable HTTP client used to send progress events
    # from the publish task to the back server (for forwarding onto clients)
    class EventClient
      def initialize(server_address)
        @server_address = server_address
      end

      def send_event(event_name, event_message)
        socket = open_socket
        return if socket.nil?
        sock = Net::BufferedIO.new(socket)
        request = request(event_name, event_message)
        # Host is a required header, but it doesn't matter to us what it is
        request["Host"] = "localhost"
        request.exec(sock, "1.1", request.path)
      ensure
        sock.close if sock
      end

      def request(event_name, event_message)
        query = ::Rack::Utils.build_nested_query({
          "event" => event_name,
          "data"  => event_message.to_json
        })
        Net::HTTP::Put.new("/@spontaneous/event?#{query}")
      end


      def open_socket
        return nil if @server_address.nil?
        case @server_address
        when /\//
          UNIXSocket.new(@server_address)
        else
          host, port = @server_address.split(":")
          TCPSocket.new(host, port)
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
spontaneous-0.2.0.beta10 lib/spontaneous/publishing/event_client.rb
spontaneous-0.2.0.beta9 lib/spontaneous/publishing/event_client.rb
spontaneous-0.2.0.beta8 lib/spontaneous/publishing/event_client.rb
spontaneous-0.2.0.beta7 lib/spontaneous/publishing/event_client.rb
spontaneous-0.2.0.beta6 lib/spontaneous/publishing/event_client.rb
spontaneous-0.2.0.beta5 lib/spontaneous/publishing/event_client.rb
spontaneous-0.2.0.beta4 lib/spontaneous/publishing/event_client.rb
spontaneous-0.2.0.beta3 lib/spontaneous/publishing/event_client.rb
spontaneous-0.2.0.beta2 lib/spontaneous/publishing/event_client.rb
spontaneous-0.2.0.beta1 lib/spontaneous/publishing/event_client.rb
spontaneous-0.2.0.alpha7 lib/spontaneous/publishing/event_client.rb
spontaneous-0.2.0.alpha6 lib/spontaneous/publishing/event_client.rb
spontaneous-0.2.0.alpha5 lib/spontaneous/publishing/event_client.rb
spontaneous-0.2.0.alpha4 lib/spontaneous/publishing/event_client.rb
spontaneous-0.2.0.alpha3 lib/spontaneous/publishing/event_client.rb