Class: PubSubModelSync::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/pub_sub_model_sync/payload.rb

Defined Under Namespace

Classes: MissingInfo

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, info, headers = {}) ⇒ Payload

Returns a new instance of Payload.

Parameters:

  • data (Hash: { any value })

    :

  • info (Hash)

    : klass: (String, required) Notification class name action: (Symbol, required) Notification action name mode: (:model|:klass, default :model): :model for instance and :klass for class notifications

  • headers (Hash) (defaults to: {})

    : key (String): identifier of the payload, default:

    <klass/action>: when class message
    <klass/action/model.id>: when model message
    

    ordering_key (String): messages with the same key are processed in the same order they

    were delivered, default:
       <klass>: when class message
       <klass/id>: when model message
    

    topic_name (String|Array<String>): Specific topic name to be used when delivering the

    message (default Config.topic_name)
    

    forced_ordering_key (String, optional): Will force to use this value as the ordering_key,

    even withing transactions. Default nil.
    

    cache (Boolean | Hash, Default false) Cache settings

    true: Skip publishing similar payloads
    Hash<required: Array<Symbol>>: Same as true and enables payload optimization to exclude
      unchanged non important attributes. Sample: { required: i[id email] }
    

    — READ ONLY —- app_key: (string) Subscriber-Key of the application who delivered the notification internal_key: (String) “<klass>/<action>” uuid: Unique notification identifier



33
34
35
36
37
38
39
# File 'lib/pub_sub_model_sync/payload.rb', line 33

def initialize(data, info, headers = {})
  @data = data.deep_symbolize_keys
  @info = info.deep_symbolize_keys
  @headers = headers.deep_symbolize_keys
  build_headers
  validate!
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/pub_sub_model_sync/payload.rb', line 6

def data
  @data
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/pub_sub_model_sync/payload.rb', line 6

def headers
  @headers
end

#infoObject (readonly)

Returns the value of attribute info.



6
7
8
# File 'lib/pub_sub_model_sync/payload.rb', line 6

def info
  @info
end

Class Method Details

.from_payload_data(data) ⇒ Object

convert payload data into Payload

Parameters:

  • data (Hash)

    : payload data (:data, :info, :headers)



109
110
111
112
# File 'lib/pub_sub_model_sync/payload.rb', line 109

def self.from_payload_data(data)
  data = data.symbolize_keys
  new(data[:data], data[:info] || data[:attributes], data[:headers])
end

Instance Method Details

#actionObject



50
51
52
# File 'lib/pub_sub_model_sync/payload.rb', line 50

def action
  info[:action].to_sym
end

#cache_settingsObject

Attributes to always be delivered after cache optimization



103
104
105
# File 'lib/pub_sub_model_sync/payload.rb', line 103

def cache_settings
  headers[:cache]
end

#exclude_data_attrs(attr_keys) ⇒ Object

Parameters:

  • attr_keys (Array<Symbol>)

    List of attributes to be excluded from payload



98
99
100
# File 'lib/pub_sub_model_sync/payload.rb', line 98

def exclude_data_attrs(attr_keys)
  @data = data.except(*attr_keys)
end

#klassObject



46
47
48
# File 'lib/pub_sub_model_sync/payload.rb', line 46

def klass
  info[:klass].to_s
end

#modeObject



54
55
56
# File 'lib/pub_sub_model_sync/payload.rb', line 54

def mode
  (info[:mode] || :model).to_sym
end

#ordering_keyObject



62
63
64
# File 'lib/pub_sub_model_sync/payload.rb', line 62

def ordering_key
  headers[:ordering_key]
end

#processObject

Process payload data

(If error will call on_error_processing callback)


75
76
77
78
# File 'lib/pub_sub_model_sync/payload.rb', line 75

def process
  publisher = PubSubModelSync::MessageProcessor.new(self)
  publisher.process
end

#process!Object

Process payload data

(If error will raise exception and wont call on_error_processing callback)


68
69
70
71
# File 'lib/pub_sub_model_sync/payload.rb', line 68

def process!
  publisher = PubSubModelSync::MessageProcessor.new(self)
  publisher.process!
end

#publishObject

Publish payload to pubsub

(If error will call on_error_publish callback)


88
89
90
# File 'lib/pub_sub_model_sync/payload.rb', line 88

def publish
  PubSubModelSync::MessagePublisher.publish(self)
end

#publish!Object

Publish payload to pubsub

(If error will raise exception and wont call on_error_publish callback)


82
83
84
# File 'lib/pub_sub_model_sync/payload.rb', line 82

def publish!
  PubSubModelSync::MessagePublisher.publish!(self)
end

#retry_publish!Object

allows to retry publishing a failed payload



93
94
95
# File 'lib/pub_sub_model_sync/payload.rb', line 93

def retry_publish!
  PubSubModelSync::MessagePublisher.connector_publish(self)
end

#to_hObject

Returns Hash: payload data.

Returns:

  • Hash: payload data



42
43
44
# File 'lib/pub_sub_model_sync/payload.rb', line 42

def to_h
  { data: data.clone, info: info.clone, headers: headers.clone }
end

#uuidObject



58
59
60
# File 'lib/pub_sub_model_sync/payload.rb', line 58

def uuid
  headers[:uuid]
end