Class: Surfliner::MetadataConsumer::Payload

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

Overview

Encapsulates a Surfliner resource message payload.

Defined Under Namespace

Classes: UnknownStatus

Constant Summary collapse

KNOWN_STATUSES =

Expected resource status values.

[:published, :updated, :unpublished, :deleted]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ Payload

Initializes a new Payload from the specified data.

If data is provided as a string, it is parsed as JSON.

Parameters:

  • payload (String, Hash)

    The payload data as received from RabbitMQ.



16
17
18
# File 'lib/surfliner/metadata_consumer/payload.rb', line 16

def initialize(payload)
  @data = normalize(payload)
end

Instance Attribute Details

#dataHash (readonly)

Returns the payload data as a Hash.

Returns:

  • (Hash)

    the payload data as a Hash.



9
10
11
# File 'lib/surfliner/metadata_consumer/payload.rb', line 9

def data
  @data
end

Instance Method Details

#resource_urlString

Returns the URL for the resource.

Returns:

  • (String)

    the URL for the resource.



31
32
33
# File 'lib/surfliner/metadata_consumer/payload.rb', line 31

def resource_url
  @resource_url ||= data.fetch(:resourceUrl)
end

#statusSymbol

Returns the resource status as a symbol.

Returns:

  • (Symbol)

    the resource status as a symbol.

Raises:

  • UnknownStatus if the message does not provide the resource status.



37
38
39
40
41
42
43
44
# File 'lib/surfliner/metadata_consumer/payload.rb', line 37

def status
  @status ||= begin
    status_str = data.fetch(:status) do
      raise(UnknownStatus, "Payload status is not defined in payload: #{data}")
    end
    status_str.to_sym
  end
end

#to_jsonString

Returns The payload data as a JSON string.

Returns:

  • (String)

    The payload data as a JSON string.



21
22
23
# File 'lib/surfliner/metadata_consumer/payload.rb', line 21

def to_json
  data.to_json
end

#to_sString

Returns The payload data as a stringified hash.

Returns:

  • (String)

    The payload data as a stringified hash.



26
27
28
# File 'lib/surfliner/metadata_consumer/payload.rb', line 26

def to_s
  data.to_s
end