Class: Timberline::Envelope

Inherits:
Object
  • Object
show all
Defined in:
lib/timberline/envelope.rb

Overview

An Envelope in Timberline is what gets passed along on the queue. The message itself - the part that the workers should intend to operate on - is stored in the ‘contents` field of the Envelope. Any other data on the envelope is considered metadata. Metadata is mostly used by Timberline itself, but is also exposed to the end user in case they have any need for it.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnvelope

Instantiates an Envelope with no metadata and nil contents.



33
34
35
# File 'lib/timberline/envelope.rb', line 33

def initialize
  @metadata = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Passes any missing methods on to the metadata hash to provide better access.

Examples:

Easily read from metadata

some_envelope.origin_queue # returns metadata["origin_queue"]

Easily write to metadata

some_envelope.origin_queue = "test_queue" # sets metadata["origin_queue"] to "test_queue"


54
55
56
57
58
59
60
61
# File 'lib/timberline/envelope.rb', line 54

def method_missing(method_name, *args)
  method_name = method_name.to_s
  if method_name[-1] == "="
    assign_var(method_name[0, method_name.size - 1], args.first)
  else
    return [method_name]
  end
end

Instance Attribute Details

#contents#to_json

the contents of the envelope; the message to be passed on the queue

Returns:

  • (#to_json)

    the current value of contents



12
13
14
# File 'lib/timberline/envelope.rb', line 12

def contents
  @contents
end

#metadataHash

the metadata information associated with the envelope

Returns:

  • (Hash)

    the current value of metadata



12
13
14
# File 'lib/timberline/envelope.rb', line 12

def 
  @metadata
end

Class Method Details

.from_json(json_string) ⇒ Envelope

Given a JSON string representing an envelope, build an Envelope object with the appropriate data.

Parameters:

  • json_string (String)

    the JSON string to parse

Returns:



20
21
22
23
24
25
# File 'lib/timberline/envelope.rb', line 20

def self.from_json(json_string)
  envelope = Envelope.new
  envelope.instance_variable_set("@metadata", JSON.parse(json_string))
  envelope.contents = envelope..delete("contents")
  envelope
end

Instance Method Details

#to_sString

Builds a JSON string version of the envelope.

Returns:

  • (String)

    a JSON representation of the envelope

Raises:



42
43
44
45
46
# File 'lib/timberline/envelope.rb', line 42

def to_s
  raise MissingContentException if contents.nil? || contents.empty?

  JSON.unparse(build_envelope_hash)
end