Sha256: 6944bd582df907b3a4c760d3f028dd101894e0f078af42f72a5367e403c5fdd4

Contents?: true

Size: 1.86 KB

Versions: 6

Compression:

Stored size: 1.86 KB

Contents

module Pubnub
  class Publish
    include Pubnub::Event
    include Pubnub::SingleEvent
    include Pubnub::Formatter
    include Pubnub::Validator


    def initialize(options, app)
      super
      @channel = @channel.first
      @event = 'publish'
      @allow_multiple_channels = false

    end

    def validate!
      super
      # check message
      raise ArgumentError.new(:object => self, :message => 'Publish requires :message argument') unless @message

      # check channel/channels
      raise ArgumentError.new(:object => self, :message => 'Publish requires :channel or :channels argument') unless @channel
      raise ArgumentError.new(:object => self, :message => 'Invalid channel(s) format! Should be type of: String, Symbol, or Array of both') unless valid_channel?
    end

    private

    def path(app)
      '/' + [
          'publish',
          @publish_key,
          @subscribe_key,
          if !@auth_key.blank? then @secret_key else '0' end,
          @channel,
          '0',
          format_message(@message)
      ].join('/')
    end

    def timetoken(parsed_response)
      parsed_response[2] if parsed_response.is_a? Array
    end

    def response_message(parsed_response)
      parsed_response[1] if parsed_response.is_a? Array
    end

    def format_envelopes(response, app, error)

      parsed_response = Parser.parse_json(response.body) if Parser.valid_json?(response.body)

      envelopes = Array.new
      envelopes << Envelope.new(
        {
            :message           => @message,
            :published_message => @message,
            :channel           => @channel,
            :response_message  => response_message(parsed_response),
            :timetoken         => timetoken(parsed_response)
        },
        app
      )

      envelopes = add_common_data_to_envelopes(envelopes, response, app, error)

      envelopes

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pubnub-3.5.8 lib/pubnub/events/publish.rb
pubnub-3.5.7 lib/pubnub/events/publish.rb
pubnub-3.5.6 lib/pubnub/events/publish.rb
pubnub-3.5.5 lib/pubnub/events/publish.rb
pubnub-3.5.3 lib/pubnub/events/publish.rb
pubnub-3.5.1 lib/pubnub/events/publish.rb