Class: Cloudenvoy::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudenvoy/message.rb

Overview

Represents a Pub/Sub message

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, payload: nil, metadata: nil, topic: nil, sub_uri: nil) ⇒ Message

Constructor

Parameters:

  • id (String) (defaults to: nil)

    The message ID

  • payload (Hash, String) (defaults to: nil)

    The message payload

  • metadata (Hash) (defaults to: nil)

    The message attributes

  • topic (String) (defaults to: nil)

    The topic - will be inferred from sub_uri if left blank

  • sub_uri (String) (defaults to: nil)

    The sub_uri this message was sent for



37
38
39
40
41
42
43
# File 'lib/cloudenvoy/message.rb', line 37

def initialize(id: nil, payload: nil, metadata: nil, topic: nil, sub_uri: nil)
  @id = id
  @payload = payload
  @topic = topic
  @metadata =  || {}
  @sub_uri = sub_uri
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/cloudenvoy/message.rb', line 7

def id
  @id
end

#metadataObject

Returns the value of attribute metadata.



7
8
9
# File 'lib/cloudenvoy/message.rb', line 7

def 
  @metadata
end

#payloadObject

Returns the value of attribute payload.



7
8
9
# File 'lib/cloudenvoy/message.rb', line 7

def payload
  @payload
end

#sub_uriObject

Returns the value of attribute sub_uri.



7
8
9
# File 'lib/cloudenvoy/message.rb', line 7

def sub_uri
  @sub_uri
end

#topicString

Return the message topic.

Returns:

  • (String)

    The message topic.



50
51
52
53
54
55
# File 'lib/cloudenvoy/message.rb', line 50

def topic
  return @topic if @topic
  return nil unless sub_uri

  Subscriber.parse_sub_uri(sub_uri)[1]
end

Class Method Details

.from_descriptor(input_payload) ⇒ Cloudenvoy::Message

Return an instantiated message from a Pub/Sub webhook payload.

the message to process.

Parameters:

  • input_payload (Hash)

    The Pub/Sub webhook hash describing

Returns:



18
19
20
21
22
23
24
25
26
# File 'lib/cloudenvoy/message.rb', line 18

def self.from_descriptor(input_payload)
  # Build new message
  new(
    id: input_payload.dig('message', 'message_id'),
    payload: JSON.parse(Base64.decode64(input_payload.dig('message', 'data'))),
    metadata: input_payload.dig('message', 'attributes'),
    sub_uri: input_payload['subscription']
  )
end

Instance Method Details

#==(other) ⇒ Boolean

Equality operator.

Parameters:

  • other (Any)

    The object to compare.

Returns:

  • (Boolean)

    True if the object is equal.



92
93
94
# File 'lib/cloudenvoy/message.rb', line 92

def ==(other)
  other.is_a?(self.class) && other.id == id
end

#subscriberSubscriber

Return the instantiated Subscriber designated to process this message.

Returns:



62
63
64
65
66
67
68
# File 'lib/cloudenvoy/message.rb', line 62

def subscriber
  @subscriber ||= begin
    return nil unless sub_uri && (klass = Subscriber.from_sub_uri(sub_uri))

    klass.new(message: self)
  end
end

#to_hHash

Return a hash description of the message.

Returns:

  • (Hash)

    The message description



75
76
77
78
79
80
81
82
83
# File 'lib/cloudenvoy/message.rb', line 75

def to_h
  {
    id: id,
    payload: payload.dup,
    metadata: .dup,
    topic: topic,
    sub_uri: sub_uri
  }.compact
end