Sha256: 0a7cbe22eba7cb33c2d96f6f9c91ae5ac31e75c1ac477c69b591d616e43534c2

Contents?: true

Size: 1.81 KB

Versions: 35

Compression:

Stored size: 1.81 KB

Contents

# Toplevel Pubnub module.
module Pubnub
  # Holds heartbeat functionality
  class Heartbeat < SingleEvent
    include Concurrent::Async
    include Pubnub::Validator::Heartbeat
    include Pubnub::Formatter

    def initialize(options, app)
      @telemetry_name = :l_pres
      @event = :heartbeat
      super
    end

    private

    def current_operation
      Pubnub::Constants::OPERATION_HEARTBEAT
    end

    def path
      '/' + [
        'v2',
        'presence',
        'sub-key',
        @subscribe_key,
        'channel',
        Formatter.channels_for_url(@channel),
        'heartbeat'
      ].join('/')
    end

    def parameters(*_args)
      parameters = super
      parameters[:state] = encode_state(@app.env[:state][@origin]) if @app.env[:state] && @app.env[:state][@origin]
      parameters[:heartbeat] = @heartbeat
      parameters['channel-group'] = @group.join(',') unless @group.blank?
      parameters
    end

    def encode_state(state)
      URI.encode_www_form_component(state.to_json).gsub('+', '%20')
    end

    def valid_envelope(_parsed_response, req_res_objects)
      # {"status": 200, "message": "OK", "service": "Presence"}
      Pubnub::Envelope.new(
        event: @event,
        event_options: @given_options,
        timetoken: nil,
        status: {
          code: req_res_objects[:response].code,
          operation: Pubnub::Constants::OPERATION_HEARTBEAT,
          client_request: req_res_objects[:request],
          server_response: req_res_objects[:response],
          data: nil,
          category: Pubnub::Constants::STATUS_ACK,
          error: false,
          auto_retried: false,

          current_timetoken: nil,
          last_timetoken: nil,
          subscribed_channels: nil,
          subscribed_channel_groups: nil,

          config: get_config
        }
      )
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
pubnub-4.4.0 lib/pubnub/events/heartbeat.rb
pubnub-4.3.0 lib/pubnub/events/heartbeat.rb
pubnub-4.2.7 lib/pubnub/events/heartbeat.rb
pubnub-4.2.6 lib/pubnub/events/heartbeat.rb
pubnub-4.2.5 lib/pubnub/events/heartbeat.rb
pubnub-4.2.4 lib/pubnub/events/heartbeat.rb
pubnub-4.2.3 lib/pubnub/events/heartbeat.rb
pubnub-4.2.2 lib/pubnub/events/heartbeat.rb
pubnub-4.2.1 lib/pubnub/events/heartbeat.rb
pubnub-4.2.0 lib/pubnub/events/heartbeat.rb
pubnub-4.1.6 lib/pubnub/events/heartbeat.rb
pubnub-4.1.5 lib/pubnub/events/heartbeat.rb
pubnub-4.1.2 lib/pubnub/events/heartbeat.rb
pubnub-4.1.0 lib/pubnub/events/heartbeat.rb
pubnub-4.0.28 lib/pubnub/events/heartbeat.rb