Sha256: 922d4b353834c66b5d5aac0402814bc9a16288fab65299a9aa4e927d016c5719

Contents?: true

Size: 1.32 KB

Versions: 7

Compression:

Stored size: 1.32 KB

Contents

module Ey::Core::Subscribable
  def self.included(klass)
    klass.send(:attribute, :read_channel)
  end

  def read_channel_uri
    self.read_channel && Addressable::URI.parse(self.read_channel)
  end

  def subscribe(&block)
    return false unless read_channel_uri

    gem 'faye', '~> 1.1'
    require 'faye' # soft dependency

    uri = read_channel_uri

    resource = self

    url          = uri.omit(:query).to_s
    token        = uri.query_values["token"]
    subscription = uri.query_values["subscription"]

    EM.run do
      client = Faye::Client.new(url)
      client.set_header("Authorization", "Token #{token}")

      deferred = client.subscribe(subscription) do |message|
        block.call(JSON.load(message))
      end

      deferred.callback do
        block.call({"meta" => true, "created_at" => Time.now,"message" => "successfully connected to log streaming service\n"})
      end

      deferred.errback do |error|
        block.call({"meta" => true, "created_at" => Time.now, "message" => "failed to stream output: #{error.inspect}\n"})
        EM.stop_event_loop
      end

      EventMachine::PeriodicTimer.new(5) do
        if resource.reload.ready?
          block.call({"meta" => true, "created_at" => Time.now, "message" => "#{resource} finished"})
          EM.stop_event_loop
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ey-core-3.1.9 lib/ey-core/subscribable.rb
ey-core-3.1.8 lib/ey-core/subscribable.rb
ey-core-3.1.7 lib/ey-core/subscribable.rb
ey-core-3.1.6 lib/ey-core/subscribable.rb
ey-core-3.1.5 lib/ey-core/subscribable.rb
ey-core-3.1.4 lib/ey-core/subscribable.rb
ey-core-3.1.3 lib/ey-core/subscribable.rb