Class: Timberline::Envelope
- Inherits:
-
Object
- Object
- Timberline::Envelope
- 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
-
#contents ⇒ #to_json
the contents of the envelope; the message to be passed on the queue.
-
#metadata ⇒ Hash
the metadata information associated with the envelope.
Class Method Summary collapse
-
.from_json(json_string) ⇒ Envelope
Given a JSON string representing an envelope, build an Envelope object with the appropriate data.
Instance Method Summary collapse
-
#initialize ⇒ Envelope
constructor
Instantiates an Envelope with no metadata and nil contents.
-
#method_missing(method_name, *args) ⇒ Object
Passes any missing methods on to the metadata hash to provide better access.
-
#to_s ⇒ String
Builds a JSON string version of the envelope.
Constructor Details
#initialize ⇒ Envelope
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.
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
12 13 14 |
# File 'lib/timberline/envelope.rb', line 12 def contents @contents end |
#metadata ⇒ Hash
the metadata information associated with the envelope
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.
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_s ⇒ String
Builds a JSON string version of the envelope.
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 |