Class: Pubnub::Event

Inherits:
Object show all
Includes:
EFormatter, Signature
Defined in:
lib/pubnub/event.rb,
lib/pubnub/event/formatter.rb,
lib/pubnub/event/signature.rb

Overview

Event class lib/pubnub/event.rb

Direct Known Subclasses

SingleEvent, SubscribeEvent

Defined Under Namespace

Modules: EFormatter, Signature

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EFormatter

#format_channels, #format_envelopes, #format_group

Constructor Details

#initialize(options, app) ⇒ Event

Returns a new instance of Event.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pubnub/event.rb', line 20

def initialize(options, app)
  @app = app
  @given_options = options
  env = app.env.clone
  env.delete(:state)
  create_variables_from_options(env.merge(options))
  @origin = @app.current_origin
  format_channels if enable_format_channels?
  format_group if enable_format_group?
  set_timestamp
  validate!
  telemetry = @app.telemetry_for(@telemetry_name)
  @compressed_body = nil
  @current_telemetry = telemetry ? telemetry.round(3) : nil
  Pubnub.logger.debug('Pubnub::Event') { "Initialized #{self.class}" }
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



9
10
11
# File 'lib/pubnub/event.rb', line 9

def callback
  @callback
end

#channelObject (readonly) Also known as: channels

Returns the value of attribute channel.



9
10
11
# File 'lib/pubnub/event.rb', line 9

def channel
  @channel
end

#channel_groupObject (readonly) Also known as: channel_groups

Returns the value of attribute channel_group.



9
10
11
# File 'lib/pubnub/event.rb', line 9

def channel_group
  @channel_group
end

#fresh_cloneObject (readonly)

Returns the value of attribute fresh_clone.



9
10
11
# File 'lib/pubnub/event.rb', line 9

def fresh_clone
  @fresh_clone
end

#given_optionsObject (readonly)

Returns the value of attribute given_options.



9
10
11
# File 'lib/pubnub/event.rb', line 9

def given_options
  @given_options
end

#groupObject (readonly)

Returns the value of attribute group.



9
10
11
# File 'lib/pubnub/event.rb', line 9

def group
  @group
end

#idle_timeoutObject (readonly)

Returns the value of attribute idle_timeout.



9
10
11
# File 'lib/pubnub/event.rb', line 9

def idle_timeout
  @idle_timeout
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



9
10
11
# File 'lib/pubnub/event.rb', line 9

def open_timeout
  @open_timeout
end

#originObject (readonly)

Returns the value of attribute origin.



9
10
11
# File 'lib/pubnub/event.rb', line 9

def origin
  @origin
end

#presence_callbackObject (readonly)

Returns the value of attribute presence_callback.



9
10
11
# File 'lib/pubnub/event.rb', line 9

def presence_callback
  @presence_callback
end

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



9
10
11
# File 'lib/pubnub/event.rb', line 9

def read_timeout
  @read_timeout
end

#sslObject (readonly)

Returns the value of attribute ssl.



9
10
11
# File 'lib/pubnub/event.rb', line 9

def ssl
  @ssl
end

#stateObject (readonly)

Returns the value of attribute state.



9
10
11
# File 'lib/pubnub/event.rb', line 9

def state
  @state
end

#wildcard_channelObject (readonly)

Returns the value of attribute wildcard_channel.



9
10
11
# File 'lib/pubnub/event.rb', line 9

def wildcard_channel
  @wildcard_channel
end

#with_presenceObject (readonly)

Returns the value of attribute with_presence.



9
10
11
# File 'lib/pubnub/event.rb', line 9

def with_presence
  @with_presence
end

Instance Method Details

#finalized?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/pubnub/event.rb', line 95

def finalized?
  @finalized || @stop
end

#fireObject



37
38
39
40
41
42
43
44
45
# File 'lib/pubnub/event.rb', line 37

def fire
  Pubnub.logger.debug('Pubnub::Event') { "Fired event #{self.class}" }

  response = send_request

  envelopes = fire_callbacks(handle(response, uri))
  finalize_event(envelopes)
  envelopes
end

#send_request(compressed_body = '', header = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pubnub/event.rb', line 47

def send_request(compressed_body = '', header = {})
  Pubnub.logger.debug('Pubnub::Event') { '#send_request called' }

  @compressed_body = compressed_body
  sender = request_dispatcher
  Pubnub.logger.debug('Pubnub::Event') { '#send_request got sender' }

  telemetry_time_start = ::Time.now.to_f
  response = case operation_http_method
             when 'get'
               sender.get(uri.to_s, header: header)
             when 'post'
               sender.post(uri.to_s, body: compressed_body, header: header)
             when 'patch'
               sender.patch(uri.to_s, body: compressed_body, header: header)
             else
               sender.delete(uri.to_s, header: header)
             end

  begin
    @app.record_telemetry(@telemetry_name, telemetry_time_start, ::Time.now.to_f)
  rescue StandardError => e
    Pubnub.logger.warn('Pubnub::Event') { "Couldn't record telemetry because of #{e}\n#{e.backtrace.join("\n")}" }
  end

  response
rescue StandardError => e
  Pubnub.logger.error('Pubnub::Event') { e }
  e
end

#sync?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/pubnub/event.rb', line 99

def sync?
  @http_sync ? true : false
end

#uri(memo = true) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pubnub/event.rb', line 78

def uri(memo = true)
  unless is_a? SubscribeEvent
    return @uri = uri(false) if memo
    return @uri if @uri
  end

  sa_signature = super_admin_signature(operation_http_method, @compressed_body) unless parameters.include?(:signature)

  uri = @ssl ? 'https://' : 'http://'
  uri += @origin
  uri += path
  uri += '?' + Formatter.params_hash_to_url_params(parameters)
  uri += "&signature=#{sa_signature}" if sa_signature
  Pubnub.logger.debug('Pubnub::Event') { "Requested URI: #{uri}" }
  URI uri
end